feat: owner review 2 phase 2: scheduled jobs and log settings (#10)
CI / Build and test (push) Successful in 7m0s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 13m28s
CI / PostgreSQL 16 integration (push) Successful in 5m52s
CI / PostgreSQL 17 integration (push) Successful in 4m50s
CI / Docker image (push) Successful in 1m4s
Release / Server image (push) Successful in 1m35s

- Jobs > Scheduled on the desktop and the admin panel. Every timer that queued work is now a visible system schedule: archive scan, upload clean-up, index backup, job-history and audit clean-ups, desktop housekeeping. New actions: verify, thumbnails, proxies, analysis, maintenance, backup, clean-ups, reports.
- The scheduler: migration 0007, in-house recurrence, compare-and-set claim, catch-up, scan at start.
- Log settings: levels, retention, size parts, total limit, daily or hourly rotation, audit retention, diagnostics share; the server writes Debug/Trace to its files again; logging never fails a job.

Upgrade: CHRONOS_LOG_LEVEL set in an existing container locks both levels; migration 0007 is one-way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #10.
This commit is contained in:
Alby96andClaude Opus 5.5 authored and Alby96 committed 2026-10-27 11:19:26 +01:00
1 parent 1f089888e8
commit af9eeaab9b
349 files changed
+28341 -1304

No files matched your search

@@ -3,7 +3,10 @@ using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
using Chronos.Data;
using Chronos.Data.Scheduling;
using Chronos.Server.Hosting;
using LogFileOptions = Chronos.Core.Diagnostics.LogFileOptions;
using LogRotation = Chronos.Core.Diagnostics.LogRotation;
namespace Chronos.Server.Configuration;
@@ -65,7 +68,16 @@ public sealed class ServerOptions
("CHRONOS_PARALLELISM", nameof(Parallelism), (o, v) => o.Parallelism = Int("CHRONOS_PARALLELISM", v)),
("CHRONOS_PRIORITY_QUEUE", nameof(PriorityQueue), (o, v) => o.PriorityQueue = bool.Parse(v)),
("CHRONOS_DISCOVERY", nameof(Discovery), (o, v) => o.Discovery = bool.Parse(v)),
("CHRONOS_LOG_LEVEL", nameof(LogLevel), (o, v) => o.LogLevel = Enum.Parse<LogLevel>(v, ignoreCase: true)),
// The level of the files and, unless CHRONOS_LOG_CONSOLE_LEVEL (listed after it) sets its own, of the console.
("CHRONOS_LOG_LEVEL", nameof(LogLevel), (o, v) => o.LogLevel = ServerLogging.ParseLevel("CHRONOS_LOG_LEVEL", v)),
("CHRONOS_LOG_LEVEL", nameof(ConsoleLogLevel), (o, v) => o.ConsoleLogLevel = ServerLogging.ParseLevel("CHRONOS_LOG_LEVEL", v)),
("CHRONOS_LOG_CONSOLE_LEVEL", nameof(ConsoleLogLevel), (o, v) => o.ConsoleLogLevel = ServerLogging.ParseLevel("CHRONOS_LOG_CONSOLE_LEVEL", v)),
("CHRONOS_LOG_RETENTION_DAYS", nameof(LogRetentionDays), (o, v) => o.LogRetentionDays = Int("CHRONOS_LOG_RETENTION_DAYS", v)),
("CHRONOS_LOG_MAX_FILE_MB", nameof(LogMaxFileMb), (o, v) => o.LogMaxFileMb = Int("CHRONOS_LOG_MAX_FILE_MB", v)),
("CHRONOS_LOG_MAX_TOTAL_MB", nameof(LogMaxTotalMb), (o, v) => o.LogMaxTotalMb = Int("CHRONOS_LOG_MAX_TOTAL_MB", v)),
("CHRONOS_LOG_ROTATION", nameof(LogRotation), (o, v) => o.LogRotation = ServerLogging.ParseRotation("CHRONOS_LOG_ROTATION", v)),
(AuditRetentionVariable, nameof(AuditRetentionDays), (o, v) => o.AuditRetentionDays = Int(AuditRetentionVariable, v)),
("CHRONOS_DIAGNOSTICS_LOG_MB", nameof(DiagnosticsLogMb), (o, v) => o.DiagnosticsLogMb = Int("CHRONOS_DIAGNOSTICS_LOG_MB", v)),
];
/// <summary>
@@ -76,8 +88,11 @@ public sealed class ServerOptions
/// </summary>
public const string DefaultTrustedProxies = "127.0.0.0/8, ::1/128";
/// <summary>The environment variable that sets <paramref name="setting"/> (of two, the one that wins), or null.</summary>
public static string? VariableOf(string setting) => Variables.LastOrDefault(v => v.Setting == setting).Variable;
/// <summary>The environment variables the server reads (<c>--help</c> lists them all).</summary>
public static IEnumerable<string> EnvironmentVariables => Variables.Select(v => v.Variable).Prepend("CHRONOS_DATA_PATH");
public static IEnumerable<string> EnvironmentVariables => Variables.Select(v => v.Variable).Distinct(StringComparer.Ordinal).Prepend("CHRONOS_DATA_PATH");
public string ServerName { get; set; } = "Chronos Server";
public string ArchivePath { get; set; } = OperatingSystem.IsWindows() ? @"C:\ChronosArchive" : "/archive";
@@ -167,11 +182,53 @@ public sealed class ServerOptions
public bool FormerWorkersVariable { get; private set; }
public bool Discovery { get; set; } = true;
/// <summary>Least severe level written to the console and the log files.</summary>
/// <summary>
/// Least severe level written to the log files (<c>/data/logs</c>); <c>CHRONOS_LOG_LEVEL</c> also sets the console's.
/// Owner review 2, R11: before, the files never took lines below Information whatever this said.
/// </summary>
public LogLevel LogLevel { get; set; } = LogLevel.Information;
/// <summary>Least severe level written to the console (the container's log).</summary>
public LogLevel ConsoleLogLevel { get; set; } = LogLevel.Information;
/// <summary>Log files last written longer ago are deleted.</summary>
public int LogRetentionDays { get; set; } = LogFileOptions.DefaultRetentionDays;
/// <summary>A log file this large (MB of 2²⁰ bytes) continues in a numbered one; 0 = no limit.</summary>
public int LogMaxFileMb { get; set; } = LogFileOptions.DefaultMaxFileMb;
/// <summary>While the log files together are larger (MB), the oldest are deleted, never the current one; 0 = no limit.</summary>
public int LogMaxTotalMb { get; set; } = LogFileOptions.DefaultMaxTotalMb;
/// <summary>One log file per UTC day or per hour.</summary>
public LogRotation LogRotation { get; set; } = LogRotation.Daily;
/// <summary>MB kept from the end of each log file in the diagnostics zip.</summary>
public int DiagnosticsLogMb { get; set; } = (int)(Chronos.Core.Diagnostics.DiagnosticsArchive.DefaultLogTailBytes / LogFileOptions.BytesPerMb);
public long MaxUploadBytes { get; set; } = 2L * 1024 * 1024 * 1024;
public int ChunkSizeBytes { get; set; } = 8 * 1024 * 1024;
/// <summary>
/// Days of job history the <c>housekeeping</c> system schedule keeps when it is created or reset (owner review 2, R10);
/// afterwards the schedule's own value applies (admin panel › Jobs › Scheduled).
/// </summary>
public int JobHistoryDays { get; set; } = 90;
/// <summary>The environment variable that fixes <see cref="AuditRetentionDays"/>.</summary>
public const string AuditRetentionVariable = "CHRONOS_AUDIT_RETENTION_DAYS";
/// <summary>Most days <see cref="AuditRetentionDays"/> accepts: the most a schedule's age limit takes (ten years).</summary>
public const int MaxAuditRetentionDays = ScheduleActions.MaxDays;
/// <summary>
/// Days of audit log the <c>audit-cleanup</c> system schedule keeps when it is created or reset (owner review 2, R10,
/// R11); 0, the default, keeps it all, the schedule off. Afterwards the schedule's own age limit applies: Settings › Logs
/// shows it (0 while the schedule is off), and saving a new one there sets the schedule and stores it here too. Set by
/// <c>CHRONOS_AUDIT_RETENTION_DAYS</c>, it is applied to the schedule at every start, before any run, and cannot be
/// changed in the panel or on the schedule.
/// </summary>
public int AuditRetentionDays { get; set; }
public int TokenLifetimeDays { get; set; } = 30;
/// <summary>Uploads untouched for this long are abandoned: their partial files are deleted.</summary>
@@ -191,9 +248,16 @@ public sealed class ServerOptions
/// <summary>Chunk uploads one user may run at the same time; more are answered 429.</summary>
public int ConcurrentChunksPerUser { get; set; } = 8;
/// <summary>Settings fixed by environment variables (names of the properties above).</summary>
/// <summary>Settings fixed by environment variables (names of the properties above); tests set it directly.</summary>
[JsonIgnore]
public IReadOnlyCollection<string> Locked { get; private set; } = [];
public IReadOnlyCollection<string> Locked { get; internal set; } = [];
/// <summary>
/// The environment variable that fixes each setting of <see cref="Locked"/>, which the admin panel names: of two that
/// set one (<c>CHRONOS_LOG_LEVEL</c> and <c>CHRONOS_LOG_CONSOLE_LEVEL</c>), the one that wins.
/// </summary>
[JsonIgnore]
public IReadOnlyDictionary<string, string> LockedBy { get; internal set; } = new Dictionary<string, string>(StringComparer.Ordinal);
[JsonIgnore]
public string LogPath => Path.Combine(DataPath, "logs");
@@ -239,6 +303,7 @@ public sealed class ServerOptions
options.DataPath = dataPath;
var locked = new List<string>();
var lockedBy = new Dictionary<string, string>(StringComparer.Ordinal);
foreach (var (variable, setting, apply) in Variables)
{
if (Env(variable) is not { } value) continue;
@@ -251,8 +316,11 @@ public sealed class ServerOptions
throw new InvalidOperationException($"{variable} has an invalid value.", ex);
}
locked.Add(setting);
// Listed in the order they apply: a later variable wins.
lockedBy[setting] = variable;
}
options.Locked = [.. locked.Distinct(StringComparer.Ordinal)];
options.LockedBy = lockedBy;
// The admin panel saves only values it accepts: one it cannot change (an environment variable) must be one of them.
options.Parallelism = Math.Clamp(options.Parallelism, 1, MaxParallelism);
options.Validate();
@@ -297,6 +365,10 @@ public sealed class ServerOptions
throw new InvalidOperationException("The chunk size must be between 64 KiB and 64 MiB.");
if (MaxUploadBytes < 1 || TokenLifetimeDays < 1 || JobHistoryDays < 1 || UploadRetentionHours < 1)
throw new InvalidOperationException("Upload size, token lifetime, job history and upload retention must be positive.");
if (ServerLogging.Problem(LogRetentionDays, LogMaxFileMb, LogMaxTotalMb, AuditRetentionDays, DiagnosticsLogMb) is { } log)
throw new InvalidOperationException($"{log} is out of range: {ServerLogging.Ranges}.");
if (!ServerLogging.IsLevel(LogLevel) || !ServerLogging.IsLevel(ConsoleLogLevel) || !Enum.IsDefined(LogRotation))
throw new InvalidOperationException("CHRONOS_LOG_LEVEL, CHRONOS_LOG_CONSOLE_LEVEL or CHRONOS_LOG_ROTATION has an unknown value.");
}
/// <summary>The parsed <see cref="TrustedProxies"/>; a single address is a network of one.</summary>
@@ -5,21 +5,27 @@ using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Jobs;
using Chronos.Protocol;
using Chronos.Server.Hosting;
using LogRotation = Chronos.Core.Diagnostics.LogRotation;
namespace Chronos.Server.Configuration;
/// <summary>
/// The admin panel's view of the configuration (BRIEF §14.8 Settings). Saving writes <c>/data/server.json</c> (only the
/// edited keys; secrets and environment values are never written) and applies what can change while running, the
/// parallelism and the priority queue included (owner review 1); ports, TLS, chunk size and job history wait for a restart.
/// parallelism and the priority queue included (owner review 1), and the log settings through <see cref="ServerLogging"/>
/// (owner review 2, R11); ports, TLS and chunk size wait for a restart. Job history is what the housekeeping system
/// schedule is reset to (owner review 2, R10): the schedule's own value applies. The audit retention kept here is what the
/// audit clean-up schedule is reset to; the endpoint shows that schedule's age limit in its place and puts a new one there
/// (<c>SchedulerHost.AuditRetentionAsync</c>, <c>SchedulerHost.ApplyAuditRetentionAsync</c>).
/// </summary>
public sealed class ServerSettingsStore(ServerOptions options)
public sealed class ServerSettingsStore(ServerOptions options, ServerLogging logging)
{
/// <summary>The kinds of job the server runs, in the order the admin panel lists their levels (downloads and uploads are a client's).</summary>
public static IReadOnlyList<JobKind> Kinds { get; } =
[
JobKind.Render, JobKind.Import, JobKind.Scan, JobKind.Rebuild, JobKind.Verify, JobKind.LightAnalyse, JobKind.DeepAnalyse, JobKind.Thumbs,
JobKind.Proxies, JobKind.Maintenance, JobKind.Duplicates, JobKind.Orphans, JobKind.Adopt, JobKind.Move,
JobKind.Proxies, JobKind.Maintenance, JobKind.Duplicates, JobKind.Orphans, JobKind.Adopt, JobKind.Move, JobKind.Backup, JobKind.Cleanup,
];
private readonly Lock _gate = new();
@@ -36,6 +42,8 @@ public sealed class ServerSettingsStore(ServerOptions options)
/// <summary>Validates and saves; returns the error code and the offending setting, or null on success.</summary>
public (string Code, string Setting)? Save(ServerSettingsDto request)
{
// A panel without the log group keeps the log settings as they are.
if (request.Logs is null) request = request with { Logs = Current().Logs };
if (Validate(request) is { } invalid) return ("invalid_setting", invalid);
lock (_gate)
{
@@ -57,18 +65,31 @@ public sealed class ServerSettingsStore(ServerOptions options)
options.MaxUploadBytes = request.MaxUploadBytes;
options.TokenLifetimeDays = request.TokenLifetimeDays;
options.UploadRetentionHours = request.UploadRetentionHours;
options.JobHistoryDays = request.JobHistoryDays;
options.Parallelism = request.Parallelism;
options.PriorityQueue = request.PriorityQueue;
options.JobPriorities = JobPriorityQueue.Changed(Levels(request.JobPriorities));
var logs = request.Logs;
options.LogLevel = Level(logs.FileLevel);
options.ConsoleLogLevel = Level(logs.ConsoleLevel);
options.LogRetentionDays = logs.RetentionDays;
options.LogMaxFileMb = logs.MaxFileMb;
options.LogMaxTotalMb = logs.MaxTotalMb;
options.LogRotation = Rotation(logs.Rotation);
options.AuditRetentionDays = logs.AuditRetentionDays;
options.DiagnosticsLogMb = logs.DiagnosticsLogMb;
logging.Apply(options);
var restart = request.HttpPort != options.HttpPort || request.HttpsPort != options.HttpsPort
|| !string.Equals(request.TlsMode, options.TlsMode.ToString(), StringComparison.OrdinalIgnoreCase)
|| request.TlsCert != options.TlsCert || request.TlsKey != options.TlsKey
|| request.ChunkSizeBytes != options.ChunkSizeBytes || request.JobHistoryDays != options.JobHistoryDays;
|| request.ChunkSizeBytes != options.ChunkSizeBytes;
_saved = request with
{
ServerName = request.ServerName.Trim(),
JobPriorities = Priorities(PriorityQueue(options)),
Logs = Logs(options),
Locked = [.. options.Locked],
LockedBy = LockedBy(options),
RestartRequired = restart,
ProxyWarning = false,
};
@@ -79,7 +100,20 @@ public sealed class ServerSettingsStore(ServerOptions options)
private static ServerSettingsDto Snapshot(ServerOptions o, bool restartRequired) => new(
o.ServerName, o.HttpPort, o.HttpsPort, o.TlsMode.ToString().ToLowerInvariant(), o.TlsCert, o.TlsKey, o.Language, o.Parallelism, o.PriorityQueue,
Priorities(PriorityQueue(o)), o.Discovery, o.MaxUploadBytes, o.ChunkSizeBytes, o.JobHistoryDays, o.TokenLifetimeDays, o.UploadRetentionHours,
[.. o.Locked], restartRequired, ProxyWarning: false);
Logs(o), [.. o.Locked], LockedBy(o), restartRequired, ProxyWarning: false);
/// <summary>The variable that fixes every locked setting (for one a test locks directly, the variable that would).</summary>
private static Dictionary<string, string> LockedBy(ServerOptions o) => o.Locked.Distinct(StringComparer.Ordinal).ToDictionary(s => s,
s => o.LockedBy.TryGetValue(s, out var variable) ? variable : ServerOptions.VariableOf(s) ?? s, StringComparer.Ordinal);
private static LogSettingsDto Logs(ServerOptions o) => new(ServerLogging.Wire(o.LogLevel), ServerLogging.Wire(o.ConsoleLogLevel), o.LogRetentionDays,
o.LogMaxFileMb, o.LogMaxTotalMb, ServerLogging.Wire(o.LogRotation), o.AuditRetentionDays, o.DiagnosticsLogMb);
/// <summary>A level <see cref="Validate"/> has checked.</summary>
private static LogLevel Level(string wire) => ServerLogging.TryParseLevel(wire, out var level) ? level : LogLevel.Information;
/// <summary>A rotation <see cref="Validate"/> has checked.</summary>
private static LogRotation Rotation(string wire) => ServerLogging.TryParseRotation(wire, out var rotation) ? rotation : LogRotation.Daily;
/// <summary>The level of every kind the server runs in <paramref name="queue"/>.</summary>
private static List<JobPriorityDto> Priorities(JobPriorityQueue queue) =>
@@ -113,7 +147,11 @@ public sealed class ServerSettingsStore(ServerOptions options)
if (r.JobHistoryDays is < 1 or > 3650) return nameof(ServerOptions.JobHistoryDays);
if (r.TokenLifetimeDays is < 1 or > 365) return nameof(ServerOptions.TokenLifetimeDays);
if (r.UploadRetentionHours is < 1 or > 8760) return nameof(ServerOptions.UploadRetentionHours);
return null;
var logs = r.Logs;
if (!ServerLogging.TryParseLevel(logs.FileLevel, out _)) return nameof(ServerOptions.LogLevel);
if (!ServerLogging.TryParseLevel(logs.ConsoleLevel, out _)) return nameof(ServerOptions.ConsoleLogLevel);
if (!ServerLogging.TryParseRotation(logs.Rotation, out _)) return nameof(ServerOptions.LogRotation);
return ServerLogging.Problem(logs.RetentionDays, logs.MaxFileMb, logs.MaxTotalMb, logs.AuditRetentionDays, logs.DiagnosticsLogMb);
}
/// <summary>Settings whose value differs, as server.json keys and JSON values.</summary>
@@ -144,6 +182,15 @@ public sealed class ServerSettingsStore(ServerOptions options)
Add(nameof(ServerOptions.JobHistoryDays), a.JobHistoryDays, b.JobHistoryDays, v => JsonValue.Create(v));
Add(nameof(ServerOptions.TokenLifetimeDays), a.TokenLifetimeDays, b.TokenLifetimeDays, v => JsonValue.Create(v));
Add(nameof(ServerOptions.UploadRetentionHours), a.UploadRetentionHours, b.UploadRetentionHours, v => JsonValue.Create(v));
// Levels and the rotation are stored by name, as server.json reads them ("Debug", "Hourly").
Add(nameof(ServerOptions.LogLevel), Level(a.Logs.FileLevel), Level(b.Logs.FileLevel), v => JsonValue.Create(v.ToString()));
Add(nameof(ServerOptions.ConsoleLogLevel), Level(a.Logs.ConsoleLevel), Level(b.Logs.ConsoleLevel), v => JsonValue.Create(v.ToString()));
Add(nameof(ServerOptions.LogRetentionDays), a.Logs.RetentionDays, b.Logs.RetentionDays, v => JsonValue.Create(v));
Add(nameof(ServerOptions.LogMaxFileMb), a.Logs.MaxFileMb, b.Logs.MaxFileMb, v => JsonValue.Create(v));
Add(nameof(ServerOptions.LogMaxTotalMb), a.Logs.MaxTotalMb, b.Logs.MaxTotalMb, v => JsonValue.Create(v));
Add(nameof(ServerOptions.LogRotation), Rotation(a.Logs.Rotation), Rotation(b.Logs.Rotation), v => JsonValue.Create(v.ToString()));
Add(nameof(ServerOptions.AuditRetentionDays), a.Logs.AuditRetentionDays, b.Logs.AuditRetentionDays, v => JsonValue.Create(v));
Add(nameof(ServerOptions.DiagnosticsLogMb), a.Logs.DiagnosticsLogMb, b.Logs.DiagnosticsLogMb, v => JsonValue.Create(v));
return changes;
}
}