From 422f33d5f3c403382cdcb6132c41edcc901eb6f5 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 27 Oct 2026 09:26:42 +0100 Subject: [PATCH] fix(server): show the audit clean-up's real age limit and lock it before the first pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Settings › Logs › Keep audit showed ServerOptions.AuditRetentionDays, a second copy of the audit clean-up schedule's age limit. Edits made on Jobs › Scheduled never reached it, so the panel could say "kept forever" while entries were being deleted every night. Saving 0 there then changed nothing, because the endpoint only compared against the stale option value. - GET and PUT /server/settings now report the schedule's limit in force (0 while the schedule is off). Saving compares against that value. A value sent back unchanged leaves the server.json reset default and the env lock alone. A new value sets the schedule and becomes the reset default. - ApplyAgeAsync also turns the schedule on when it already holds the days but is off. - CHRONOS_AUDIT_RETENTION_DAYS is now applied before the scheduler's first pass, through a new Scheduler.StartAsync overload with a beforeFirstPass hook. Before, a missed 04:30 run was caught up at start with the stale days, which could delete entries the variable says to keep. While the variable fixes a limit above 0, turning the schedule off is a 409 setting_locked. - Corrected the comments in SchedulerHost, ServerOptions, ServerSettingsStore, LogSettingsDto, the header of settings.js and ARCHITECTURE. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 490ed286e9f32745174b13b1963d6ec91955196b) --- docs/ARCHITECTURE.md | 2 +- src/Chronos.Data/Scheduling/Scheduler.cs | 10 +- src/Chronos.Protocol/Admin.cs | 9 +- .../Admin/web/pages/settings.js | 10 +- src/Chronos.Server/Api/AdminEndpoints.cs | 30 ++++- src/Chronos.Server/Api/ScheduleEndpoints.cs | 12 +- .../Configuration/ServerOptions.cs | 9 +- .../Configuration/ServerSettingsStore.cs | 6 +- src/Chronos.Server/Jobs/SchedulerHost.cs | 39 ++++-- .../Scheduling/SchedulerTests.cs | 32 +++++ .../Hosting/LoggingTests.cs | 4 +- .../Jobs/ScheduleEndpointTests.cs | 121 ++++++++++++++++-- 12 files changed, 232 insertions(+), 52 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 7c5c21be..9c264a0f 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -95,7 +95,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S | Host | Composition root | What it runs | |---|---|---| | Chronos (desktop) | `App/Infrastructure/AppServices` | SQLite index in `%LOCALAPPDATA%\Chronos` (or `.\data` when portable, unless the copy is in a OneDrive folder); `ArchiveService` with WIC first, then managed decoders; `ProcessingService` with Media Foundation, then managed encoders; one `JobEngine` with the handlers of both; thumbnails; `ServerSession`; `SyncHost` while signed in (sync engine and agent, upload and download handlers, metered hold, editors of server sequences); the render node while opted in and signed in; update check and download (`About/UpdateCheck`, `About/UpdateDownload`, `About/UpdateFlow`: the setup, or the portable zip for a portable copy, verified against `SHA256SUMS`, resumable, cancellable, failing after 60 s without data); diagnostics bundle; the `Scheduler` on `DesktopScheduleHost`, started once the archive was reopened (an unplugged one is looked for every minute), which then runs the archive scan once (again when an unplugged archive is back), and whose system schedules replaced the rescan timer, the daily backup loop and the job-history purge (scans follow Settings › Analysis › After a scan at every run); the restore staged in Settings › Advanced (`DatabaseBackups`) | -| Chronos Server | `Server/Hosting/ServerHost.Build` | Options from `server.json` + `CHRONOS_*`; PostgreSQL (or SQLite on Windows); `ArchiveModule` then `ProcessingModule` register job handler providers; `JobEngineHost` (renders only managed formats and MP4 jobs no render node can take, BRIEF §14.7); `RenderNodeService`/`RenderNodeMonitor`; `UploadService`; `VolumeSpace` (free space of the archive, the space path and `/data`, file system and mount from `/proc/self/mountinfo`; tests replace it); `EventHub` (WebSocket); embedded admin panel; LAN discovery beacon; Windows service host; `SchedulerHost` with the system schedules `archive-scan` (every 10 minutes, also once at start), `upload-cleanup` (every 6 hours), `index-backup` (SQLite only, 03:00), `housekeeping` (job history, 04:00) and `audit-cleanup` (04:30; its age limit is Settings › Logs' audit retention, 0 by default: off, the audit log kept), no other timer queues work | +| Chronos Server | `Server/Hosting/ServerHost.Build` | Options from `server.json` + `CHRONOS_*`; PostgreSQL (or SQLite on Windows); `ArchiveModule` then `ProcessingModule` register job handler providers; `JobEngineHost` (renders only managed formats and MP4 jobs no render node can take, BRIEF §14.7); `RenderNodeService`/`RenderNodeMonitor`; `UploadService`; `VolumeSpace` (free space of the archive, the space path and `/data`, file system and mount from `/proc/self/mountinfo`; tests replace it); `EventHub` (WebSocket); embedded admin panel; LAN discovery beacon; Windows service host; `SchedulerHost` with the system schedules `archive-scan` (every 10 minutes, also once at start), `upload-cleanup` (every 6 hours), `index-backup` (SQLite only, 03:00), `housekeeping` (job history, 04:00) and `audit-cleanup` (04:30; Settings › Logs shows and sets its age limit, 0 while it is off; seeded and reset from `AuditRetentionDays`, 0 by default: off, the audit log kept; `CHRONOS_AUDIT_RETENTION_DAYS` fixes it before the first pass), no other timer queues work | | Chronos Uploader | `Uploader/App.xaml.cs` | `ChronosClient`, persisted queue, watch folders; no database | | Chronos Setup | `Setup/Program` | TPK1 payload extraction, registry, shortcuts, file types named in setup's language, silent mode, uninstaller; only what starts a program its log or payload lists is changed: Windows services of those programs stopped once the programs have closed, started again (also after a failure) or deleted through the Service Control Manager (an elevated `/SERVICES … /NAMES` step when needed), sign-in entries of those programs moved to the program an upgrade renamed or removed, shared shortcuts handed to the product that keeps the program; the Server only for all users inside Program Files; "Remove my data" limited to the programs no other installed product uses | diff --git a/src/Chronos.Data/Scheduling/Scheduler.cs b/src/Chronos.Data/Scheduling/Scheduler.cs index fd6f8c79..b945c98d 100644 --- a/src/Chronos.Data/Scheduling/Scheduler.cs +++ b/src/Chronos.Data/Scheduling/Scheduler.cs @@ -90,13 +90,21 @@ public sealed class Scheduler : IAsyncDisposable /// Seeds 's system schedules, settles the runs missed while the host was off (see the type's /// remarks) and starts the loop. The first pass runs before this returns. /// - public async Task StartAsync(IScheduleHost host, CancellationToken cancellationToken = default) + public Task StartAsync(IScheduleHost host, CancellationToken cancellationToken = default) => StartAsync(host, null, cancellationToken); + + /// + /// , calling once the system + /// schedules are seeded and edits are possible, before the first pass: what it changes (a value an environment variable + /// fixes) applies to the runs caught up at start too. + /// + public async Task StartAsync(IScheduleHost host, Func? beforeFirstPass, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(host); if (Host is not null) throw new InvalidOperationException("The scheduler has started already."); var added = await _db.Schedules.EnsureSystemAsync(host.SystemSchedules, Now(), cancellationToken).ConfigureAwait(false); if (added > 0) Log.Info("schedules", $"Added {added} system schedule(s)"); if (Interlocked.CompareExchange(ref _host, host, null) is not null) throw new InvalidOperationException("The scheduler has started already."); + if (beforeFirstPass is not null) await beforeFirstPass(cancellationToken).ConfigureAwait(false); try { await RunDueAsync(cancellationToken).ConfigureAwait(false); diff --git a/src/Chronos.Protocol/Admin.cs b/src/Chronos.Protocol/Admin.cs index 14e7e3f9..0f591f91 100644 --- a/src/Chronos.Protocol/Admin.cs +++ b/src/Chronos.Protocol/Admin.cs @@ -38,10 +38,11 @@ public sealed record ServerSettingsDto( /// The log settings of (owner review 2, R11): the least severe level of the files and of the /// console (trace, debug, information, warning, error, critical, none), the /// days log files are kept, the size at which a file continues in a numbered one and the size of all of them (MB of 2²⁰ -/// bytes, 0 = no limit), the rotation (daily or hourly, UTC), the days audit entries are kept (0 = forever) -/// and the MB of the end of each log file in the diagnostics zip. Their locks use the server.json names (LogLevel, -/// ConsoleLogLevel, LogRetentionDays, LogMaxFileMb, LogMaxTotalMb, LogRotation, -/// AuditRetentionDays, DiagnosticsLogMb). +/// bytes, 0 = no limit), the rotation (daily or hourly, UTC), the days audit entries are kept (the audit +/// clean-up schedule's age limit; 0 = forever, as while that schedule is off) and the MB of the end of each log file in +/// the diagnostics zip. Their locks use the server.json names (LogLevel, ConsoleLogLevel, +/// LogRetentionDays, LogMaxFileMb, LogMaxTotalMb, LogRotation, AuditRetentionDays, +/// DiagnosticsLogMb). /// public sealed record LogSettingsDto( string FileLevel, diff --git a/src/Chronos.Server/Admin/web/pages/settings.js b/src/Chronos.Server/Admin/web/pages/settings.js index 837e588c..a13f1019 100644 --- a/src/Chronos.Server/Admin/web/pages/settings.js +++ b/src/Chronos.Server/Admin/web/pages/settings.js @@ -1,10 +1,10 @@ // Settings (BRIEF §14.8): server name, ports, TLS, discovery, jobs, limits, language, retention, logs. Values fixed by // environment variables are read-only here; ports, TLS and chunk size apply at the next restart. How long job history is -// kept is a parameter of the Job history clean-up schedule (Jobs › Scheduled, owner review 2, R10); the audit retention -// saved here is the Audit log clean-up schedule's age limit. Jobs run one at a time (owner review 1): the parallelism -// (photos at once) and the priority queue apply at once, as do the log levels, retention, size limits and rotation -// (owner review 2, R11). About (version, API, database, uptime) is the last group, read-only (owner review 2); #/about -// opens here on it. +// kept is a parameter of the Job history clean-up schedule (Jobs › Scheduled, owner review 2, R10); Keep audit shows the +// Audit log clean-up schedule's age limit (0 while it is off), and saving a new one sets it. Jobs run one at a time +// (owner review 1): the parallelism (photos at once) and the priority queue apply at once, as do the log levels, +// retention, size limits and rotation (owner review 2, R11). About (version, API, database, uptime) is the last group, +// read-only (owner review 2); #/about opens here on it. import { t, applyStrings, languages } from "../i18n.js"; import { escapeHtml, attr, toast, errorText, nativeName, fingerprint, formatDuration, markInvalid, clearInvalid, invalidText, escapeReverts, routeParams, PROXY_WARNING } from "../ui.js"; import { kindLabel } from "../jobs.js"; diff --git a/src/Chronos.Server/Api/AdminEndpoints.cs b/src/Chronos.Server/Api/AdminEndpoints.cs index d6a9783d..5ba164bb 100644 --- a/src/Chronos.Server/Api/AdminEndpoints.cs +++ b/src/Chronos.Server/Api/AdminEndpoints.cs @@ -19,8 +19,8 @@ public static class AdminEndpoints public static void Map(RouteGroupBuilder api) { api.MapGet("/server/dashboard", DashboardAsync).Requires(Permissions.ManageArchive | Permissions.ManageUsers); - api.MapGet("/server/settings", (HttpContext context, ServerSettingsStore store, ServerOptions options) => - Results.Json(store.Current() with { ProxyWarning = AdminPanel.BypassesProxy(context, options) }, ProtocolJson.Default.ServerSettingsDto)) + api.MapGet("/server/settings", async (HttpContext context, ServerSettingsStore store, ServerOptions options, SchedulerHost schedules) => + Results.Json(await SettingsAsync(context, store, options, schedules), ProtocolJson.Default.ServerSettingsDto)) .Requires(Permissions.ManageArchive); api.MapPut("/server/settings", SaveSettingsAsync).Requires(Permissions.ManageArchive); api.MapGet("/audit", AuditAsync).Requires(Permissions.ManageUsers | Permissions.ManageArchive); @@ -76,7 +76,15 @@ public static class AdminEndpoints private static async Task SaveSettingsAsync(HttpContext context, Caller caller, ServerSettingsDto request, ServerSettingsStore store, ServerOptions options, AuditLog audit, JobEngineHost jobs, SchedulerHost schedules) { - var auditDays = options.AuditRetentionDays; + // Keep audit shows the audit clean-up schedule's age limit (SettingsAsync): sent back as shown it is no change, so + // the default stored in server.json (and an environment variable's lock) is left alone. + var shown = await schedules.AuditRetentionAsync(context.RequestAborted) ?? options.AuditRetentionDays; + int? auditDays = null; + if (request.Logs is { } logs) + { + if (logs.AuditRetentionDays != shown) auditDays = logs.AuditRetentionDays; + else request = request with { Logs = logs with { AuditRetentionDays = options.AuditRetentionDays } }; + } switch (store.Save(request)) { case ("setting_locked", var setting): @@ -87,9 +95,21 @@ public static class AdminEndpoints // A priority queue turned on, or a level raised, may make the running job yield at once. jobs.Engine.Wake(); // The audit retention is the audit clean-up schedule's age limit: a new one goes there (owner review 2, R10, R11). - if (options.AuditRetentionDays != auditDays) await schedules.ApplyAuditRetentionAsync(options.AuditRetentionDays, context.RequestAborted); + if (auditDays is { } days) await schedules.ApplyAuditRetentionAsync(days, context.RequestAborted); await audit.WriteAsync(caller, context, "server_settings", "server", null, request.ServerName); - return Results.Json(store.Current() with { ProxyWarning = AdminPanel.BypassesProxy(context, options) }, ProtocolJson.Default.ServerSettingsDto); + return Results.Json(await SettingsAsync(context, store, options, schedules), ProtocolJson.Default.ServerSettingsDto); + } + + /// + /// The settings as the panel shows them. Keep audit is the audit retention in force, the audit clean-up schedule's age + /// limit (0 while it is off), wherever it was last set; is only what the + /// schedule is reset to (owner review 2, R10, R11). + /// + private static async Task SettingsAsync(HttpContext context, ServerSettingsStore store, ServerOptions options, SchedulerHost schedules) + { + var settings = store.Current(); + var audit = await schedules.AuditRetentionAsync(context.RequestAborted) ?? settings.Logs.AuditRetentionDays; + return settings with { Logs = settings.Logs with { AuditRetentionDays = audit }, ProxyWarning = AdminPanel.BypassesProxy(context, options) }; } /// The audit log, newest first; action may list several actions separated by commas (login,login_failed). diff --git a/src/Chronos.Server/Api/ScheduleEndpoints.cs b/src/Chronos.Server/Api/ScheduleEndpoints.cs index a82a8dee..a244625b 100644 --- a/src/Chronos.Server/Api/ScheduleEndpoints.cs +++ b/src/Chronos.Server/Api/ScheduleEndpoints.cs @@ -13,8 +13,8 @@ namespace Chronos.Server.Api; /// delete (409 schedule_system for a system schedule), run now (409 schedule_pending while its last job is /// queued, running or paused), reset a system schedule (409 schedule_no_defaults otherwise) and the jobs a schedule /// queued, newest first. An invalid field is 400 invalid_schedule with its name as details (name, -/// recurrence, parameters.percent…); an age limit an environment variable fixes is 409 setting_locked. -/// Every change is audited. +/// recurrence, parameters.percent…); changing an age limit an environment variable fixes, or turning its +/// schedule off, is 409 setting_locked. Every change is audited. /// public static class ScheduleEndpoints { @@ -49,9 +49,11 @@ public static class ScheduleEndpoints { var ct = context.RequestAborted; if (await host.Scheduler.GetAsync(id, ct) is not { } current) return ApiErrors.NotFound(context, "schedule"); - // Parameters are replaced as a whole: a missing age limit would fall back to the action's default. - if (host.Host.LockedAgeOf(current.SystemKey) is { } locked && request.Parameters is { } parameters - && ScheduleActions.Normalize(current.Action, parameters).OlderThanDays != locked.Days) + // Parameters are replaced as a whole: a missing age limit would fall back to the action's default. Turning off a + // schedule whose limit is fixed would stop the deletions the variable asks for. + if (host.Host.LockedAgeOf(current.SystemKey) is { } locked + && ((request.Parameters is { } parameters && ScheduleActions.Normalize(current.Action, parameters).OlderThanDays != locked.Days) + || (locked.Days > 0 && request.Enabled == false))) return ApiErrors.Conflict(context, "setting_locked", locked.Setting); var result = await host.Scheduler.UpdateAsync(id, request, ct); if (result.NotFound) return ApiErrors.NotFound(context, "schedule"); diff --git a/src/Chronos.Server/Configuration/ServerOptions.cs b/src/Chronos.Server/Configuration/ServerOptions.cs index 0fe87331..248bde7d 100644 --- a/src/Chronos.Server/Configuration/ServerOptions.cs +++ b/src/Chronos.Server/Configuration/ServerOptions.cs @@ -221,10 +221,11 @@ public sealed class ServerOptions public const int MaxAuditRetentionDays = ScheduleActions.MaxDays; /// - /// Days of audit log the audit-cleanup system schedule keeps (owner review 2, R10, R11); 0, the default, keeps - /// it all. It is that schedule's age limit: saving it in Settings › Logs sets the schedule's parameter, and Reset - /// goes back to it. Set by CHRONOS_AUDIT_RETENTION_DAYS, it is applied to the schedule at every start and cannot - /// be changed in the panel or on the schedule. + /// Days of audit log the audit-cleanup 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 + /// CHRONOS_AUDIT_RETENTION_DAYS, it is applied to the schedule at every start, before any run, and cannot be + /// changed in the panel or on the schedule. /// public int AuditRetentionDays { get; set; } diff --git a/src/Chronos.Server/Configuration/ServerSettingsStore.cs b/src/Chronos.Server/Configuration/ServerSettingsStore.cs index 39a7571f..d54157e5 100644 --- a/src/Chronos.Server/Configuration/ServerSettingsStore.cs +++ b/src/Chronos.Server/Configuration/ServerSettingsStore.cs @@ -15,9 +15,9 @@ namespace Chronos.Server.Configuration; /// 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), and the log settings through /// (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 is what the audit -/// clean-up schedule is reset to, and saving a new one also sets that schedule's age limit (the endpoint does it through -/// SchedulerHost.ApplyAuditRetentionAsync). +/// 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 +/// (SchedulerHost.AuditRetentionAsync, SchedulerHost.ApplyAuditRetentionAsync). /// public sealed class ServerSettingsStore(ServerOptions options, ServerLogging logging) { diff --git a/src/Chronos.Server/Jobs/SchedulerHost.cs b/src/Chronos.Server/Jobs/SchedulerHost.cs index 92603b8f..377d434c 100644 --- a/src/Chronos.Server/Jobs/SchedulerHost.cs +++ b/src/Chronos.Server/Jobs/SchedulerHost.cs @@ -1,3 +1,4 @@ +using Chronos.Core.Model; using Chronos.Data; using Chronos.Data.Scheduling; using Chronos.Protocol; @@ -8,11 +9,14 @@ namespace Chronos.Server.Jobs; /// /// Hosts the server's (owner review 2, R10) on the 's engine with the /// : registered after the engine's host, so it starts after it and stops before it. At start -/// it seeds the system schedules and settles missed runs (), applies the age limits that -/// environment variables fix (), and runs the archive scan once in the -/// background, as the former polling did, so the photos of an adopted folder are indexed without waiting for its first -/// interval (only while that schedule is enabled). The audit retention saved in Settings › Logs becomes the audit -/// clean-up's age limit (). +/// () it seeds the system +/// schedules, puts the age limits that environment variables fix into them () +/// before the first pass, so a run caught up at start already uses them, and settles missed runs; then it runs the archive +/// scan once in the background, as the former polling did, so the photos of an adopted folder are indexed without waiting +/// for its first interval (only while that schedule is enabled). The audit retention in force is +/// the audit clean-up schedule's age limit, 0 while it is off: Settings › Logs shows it () +/// and sets it (); is only what the +/// schedule is seeded and reset with, or what an environment variable fixes. /// public sealed class SchedulerHost : IHostedService { @@ -38,8 +42,7 @@ public sealed class SchedulerHost : IHostedService public async Task StartAsync(CancellationToken cancellationToken) { var started = Scheduler.Options.Time.GetUtcNow(); - await Scheduler.StartAsync(Host, cancellationToken); - await ApplyLockedAgesAsync(cancellationToken); + await Scheduler.StartAsync(Host, ApplyLockedAgesAsync, cancellationToken); var stopping = _stopping.Token; _startScan = Task.Run(() => ScanAtStartAsync(started, stopping), CancellationToken.None); } @@ -54,14 +57,23 @@ public sealed class SchedulerHost : IHostedService } /// - /// Puts the audit retention just saved in Settings › Logs () into the - /// audit clean-up schedule, its one source of truth (owner review 2, R10, R11): enabled with a limit, off at 0, which - /// keeps everything. A schedule already holding the value is left as the user set it. + /// The audit retention in force (owner review 2, R10, R11): the audit clean-up schedule's age limit, 0 while the schedule + /// is off (nothing is deleted); null before the schedule is seeded. + /// + public async Task AuditRetentionAsync(CancellationToken cancellationToken = default) => + await _db.Schedules.GetBySystemKeyAsync(ServerScheduleHost.AuditCleanup, cancellationToken) is { } row ? InForce(row) : null; + + /// + /// Puts the audit retention saved in Settings › Logs into the audit clean-up schedule, the one source of truth (owner + /// review 2, R10, R11): enabled with a limit, off at 0, which keeps everything. /// public Task ApplyAuditRetentionAsync(int days, CancellationToken cancellationToken = default) => ApplyAgeAsync(ServerScheduleHost.AuditCleanup, days, "Settings › Logs", cancellationToken); - /// The age limits environment variables fix, put into their system schedules at every start. + /// The age limit of a system schedule that deletes by age, 0 while it is off. + private static int InForce(Schedule row) => row.Enabled ? ScheduleMapping.Parameters(row).OlderThanDays ?? 0 : 0; + + /// The age limits environment variables fix, put into their system schedules at every start, before the first pass. private async Task ApplyLockedAgesAsync(CancellationToken cancellationToken) { foreach (var system in Host.SystemSchedules) @@ -72,13 +84,14 @@ public sealed class SchedulerHost : IHostedService /// /// Sets the age limit of the system schedule to , enabling it when it - /// has a limit now and disabling it at 0; nothing when it already holds the value. + /// has a limit now and disabling it at 0; nothing when it already holds the value and is on (off at 0). A schedule kept + /// enabled keeps its next run, so a missed run is still caught up, with the new limit. /// private async Task ApplyAgeAsync(string systemKey, int days, string source, CancellationToken cancellationToken) { if (await _db.Schedules.GetBySystemKeyAsync(systemKey, cancellationToken) is not { } row) return; var parameters = ScheduleMapping.Parameters(row); - if (parameters.OlderThanDays == days) return; + if (parameters.OlderThanDays == days && row.Enabled == (days > 0)) return; var result = await Scheduler.UpdateAsync(row.Id, new UpdateScheduleRequest(Parameters: parameters with { OlderThanDays = days }, Enabled: days > 0), cancellationToken); if (result.Problem is { } problem) _logger.LogWarning("{Source} could not be applied to the {Schedule} schedule ({Problem})", source, row.Name, problem); diff --git a/tests/Chronos.Data.Tests/Scheduling/SchedulerTests.cs b/tests/Chronos.Data.Tests/Scheduling/SchedulerTests.cs index 64c12385..64046ea4 100644 --- a/tests/Chronos.Data.Tests/Scheduling/SchedulerTests.cs +++ b/tests/Chronos.Data.Tests/Scheduling/SchedulerTests.cs @@ -156,6 +156,38 @@ public sealed class SchedulerTests(TestContext context) Assert.Equal(0, await after.RunDueAsync(Ct)); } + /// The server fixes an age limit from its environment there: a run caught up at start must already use it. + [Test] + public async Task AnEditBeforeTheFirstPassAppliesToTheRunCaughtUpAtStart() + { + await using var fx = await ArchiveFixture.CreateAsync(context); + await using var engine = Engine(fx); + var clock = new ScheduleClock(T0); + var host = new TestScheduleHost(fx.Archive); + host.Systems.Add(new SystemSchedule("audit-cleanup", "Audit log clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.Parse("daily:04:30"), + new ScheduleParametersDto(OlderThanDays: 90, Steps: ["audit"]))); + await using (await StartAsync(fx.Db, engine, host, clock)) + { + } + // The host was off over the run of 04:30. + clock.Advance(TimeSpan.FromDays(1)); + await using var scheduler = new Scheduler(fx.Db, engine, new SchedulerOptions { Time = clock, Tick = TimeSpan.FromHours(1) }); + var id = Guid.Empty; + await scheduler.StartAsync(host, async ct => + { + var row = (await fx.Db.Schedules.GetBySystemKeyAsync("audit-cleanup", ct))!; + id = row.Id; + Assert.Empty(await fx.Db.Jobs.ListByScheduleAsync(id, cancellationToken: ct)); + var edit = await scheduler.UpdateAsync(id, new UpdateScheduleRequest(Parameters: new ScheduleParametersDto(OlderThanDays: 365, Steps: ["audit"])), ct); + Assert.Null(edit.Problem); + }, Ct); + + var jobs = await fx.Db.Jobs.ListByScheduleAsync(id, cancellationToken: Ct); + Assert.Count(1, jobs); + using var parameters = System.Text.Json.JsonDocument.Parse(jobs[0].ParametersJson!); + Assert.Equal(365, parameters.RootElement.GetProperty("olderThanDays").GetInt32()); + } + [Test] public async Task DisabledSchedulesNeverRunAndGetANewNextRunWhenEnabled() { diff --git a/tests/Chronos.Server.Tests/Hosting/LoggingTests.cs b/tests/Chronos.Server.Tests/Hosting/LoggingTests.cs index 2c5f4762..6065c5ac 100644 --- a/tests/Chronos.Server.Tests/Hosting/LoggingTests.cs +++ b/tests/Chronos.Server.Tests/Hosting/LoggingTests.cs @@ -10,6 +10,7 @@ using Chronos.Core.Diagnostics; using Chronos.Protocol; using Chronos.Server.Configuration; using Chronos.Server.Hosting; +using Chronos.Server.Storage; using Chronos.Testing; using CoreLog = Chronos.Core.Diagnostics.Log; using LogLevel = Microsoft.Extensions.Logging.LogLevel; @@ -83,7 +84,8 @@ public sealed partial class LoggingTests(TestContext context) [Test] public async Task LogSettingsAreSavedAppliedAndValidated() { - await using var server = await TestServer.StartAsync(context); + // With the archive module: its audit step is what the audit retention, the audit clean-up's age limit, deletes with. + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); var admin = await server.AdminAsync(); var settings = await SettingsAsync(admin); Assert.Equal(new LogSettingsDto("warning", "warning", 14, 50, 1024, "daily", 0, 4), settings.Logs); diff --git a/tests/Chronos.Server.Tests/Jobs/ScheduleEndpointTests.cs b/tests/Chronos.Server.Tests/Jobs/ScheduleEndpointTests.cs index 2ad206ce..5ad00a04 100644 --- a/tests/Chronos.Server.Tests/Jobs/ScheduleEndpointTests.cs +++ b/tests/Chronos.Server.Tests/Jobs/ScheduleEndpointTests.cs @@ -1,9 +1,11 @@ using System.Net; using System.Net.Http.Json; +using System.Text.Json; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging.Abstractions; using Chronos.Core.Model; +using Chronos.Data; using Chronos.Data.Jobs; using Chronos.Data.Scheduling; using Chronos.Protocol; @@ -216,9 +218,9 @@ public sealed class ScheduleEndpointTests(TestContext context) } /// - /// Owner review 2, R10 and R11: the audit retention has one source of truth, the audit clean-up schedule's age limit. The - /// field of Settings › Logs sets it (and turns the schedule on, or off at 0), Reset goes back to it, and at 0 the audit - /// step deletes nothing. + /// Owner review 2, R10 and R11: the audit retention has one source of truth, the audit clean-up schedule's age limit. + /// Settings › Logs shows it wherever it was set (0 while the schedule is off) and sets it (turning the schedule on, or + /// off at 0); Reset goes back to the value last saved there, and at 0 the audit step deletes nothing. /// [Test] public async Task TheAuditRetentionOfTheSettingsIsTheAgeLimitOfTheAuditCleanUp() @@ -243,28 +245,58 @@ public sealed class ScheduleEndpointTests(TestContext context) audit = SystemRow(await ListAsync(admin), ServerScheduleHost.AuditCleanup); Assert.Equal(((int?)30, true), (audit.Parameters.OlderThanDays, audit.Enabled)); - // Without an environment variable the schedule stays editable; Reset goes back to the setting. - using (var edit = await admin.PutAsJsonAsync($"/api/v1/schedules/{audit.Id}", new UpdateScheduleRequest(Parameters: new ScheduleParametersDto(Steps: ["audit"], OlderThanDays: 60)), - ProtocolJson.Default.UpdateScheduleRequest)) - Assert.Equal((int?)60, (await edit.ReadAsync(ProtocolJson.Default.ScheduleDto)).Parameters.OlderThanDays); + // Without an environment variable the schedule stays editable, and Settings shows its limit. + await EditAsync(admin, audit.Id, new UpdateScheduleRequest(Parameters: new ScheduleParametersDto(Steps: ["audit"], OlderThanDays: 60))); + Assert.Equal(60, (await SettingsAsync(admin)).Logs.AuditRetentionDays); + // Saved back as shown, it changes neither the schedule nor the value Reset goes back to. + await SaveAuditRetentionAsync(admin, 60); + Assert.Equal(30, server.Options.AuditRetentionDays); + Assert.Equal((int?)60, SystemRow(await ListAsync(admin), ServerScheduleHost.AuditCleanup).Parameters.OlderThanDays); using (var reset = await admin.PostAsync($"/api/v1/schedules/{audit.Id}/reset", null)) Assert.Equal((int?)30, (await reset.ReadAsync(ProtocolJson.Default.ScheduleDto)).Parameters.OlderThanDays); + Assert.Equal(30, (await SettingsAsync(admin)).Logs.AuditRetentionDays); + + // Turned off on the schedule, nothing is deleted: Settings shows 0, and saving 30 there turns it on again. + await EditAsync(admin, audit.Id, new UpdateScheduleRequest(Enabled: false)); + Assert.Equal(0, (await SettingsAsync(admin)).Logs.AuditRetentionDays); + await SaveAuditRetentionAsync(admin, 30); + audit = SystemRow(await ListAsync(admin), ServerScheduleHost.AuditCleanup); + Assert.Equal(((int?)30, true), (audit.Parameters.OlderThanDays, audit.Enabled)); // Back to 0: kept forever, the schedule off. await SaveAuditRetentionAsync(admin, 0); audit = SystemRow(await ListAsync(admin), ServerScheduleHost.AuditCleanup); Assert.Equal(((int?)0, false), (audit.Parameters.OlderThanDays, audit.Enabled)); + + // A limit set on the schedule while the setting is 0 is shown, and saving 0 turns the schedule off. + await EditAsync(admin, audit.Id, new UpdateScheduleRequest(Parameters: new ScheduleParametersDto(Steps: ["audit"], OlderThanDays: 45), Enabled: true)); + Assert.Equal(45, (await SettingsAsync(admin)).Logs.AuditRetentionDays); + await SaveAuditRetentionAsync(admin, 0); + audit = SystemRow(await ListAsync(admin), ServerScheduleHost.AuditCleanup); + Assert.Equal(((int?)0, false), (audit.Parameters.OlderThanDays, audit.Enabled)); } - /// Saves Settings › Logs › Keep audit (days) as the admin panel does. - private static async Task SaveAuditRetentionAsync(HttpClient admin, int days) + private static async Task SettingsAsync(HttpClient admin) { using var read = await admin.GetAsync("/api/v1/server/settings"); - var settings = await read.ReadAsync(ProtocolJson.Default.ServerSettingsDto); + return await read.ReadAsync(ProtocolJson.Default.ServerSettingsDto); + } + + /// Saves Settings › Logs › Keep audit (days) as the admin panel does; the answer shows the value saved. + private static async Task SaveAuditRetentionAsync(HttpClient admin, int days) + { + var settings = await SettingsAsync(admin); // The test server listens on ports the OS chose, which the settings refuse: real ones are sent with the edit. using var saved = await admin.PutAsJsonAsync("/api/v1/server/settings", settings with { HttpPort = 18080, HttpsPort = 18443, Logs = settings.Logs with { AuditRetentionDays = days } }, ProtocolJson.Default.ServerSettingsDto); await saved.ExpectAsync(HttpStatusCode.OK); + Assert.Equal(days, (await saved.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).Logs.AuditRetentionDays); + } + + private static async Task EditAsync(HttpClient admin, Guid id, UpdateScheduleRequest request) + { + using var edit = await admin.PutAsJsonAsync($"/api/v1/schedules/{id}", request, ProtocolJson.Default.UpdateScheduleRequest); + await edit.ExpectAsync(HttpStatusCode.OK); } [Test] @@ -444,10 +476,79 @@ public sealed class ScheduleEndpointTests(TestContext context) using (var changed = await admin.PutAsJsonAsync($"/api/v1/schedules/{audit.Id}", new UpdateScheduleRequest(Parameters: new ScheduleParametersDto(Steps: ["audit"], OlderThanDays: 60)), ProtocolJson.Default.UpdateScheduleRequest)) Assert.Equal("AuditRetentionDays", (await changed.ExpectErrorAsync(HttpStatusCode.Conflict, "setting_locked")).Details); + // Turned off, it would keep what the variable says to delete. + using (var off = await admin.PutAsJsonAsync($"/api/v1/schedules/{audit.Id}", new UpdateScheduleRequest(Enabled: false), ProtocolJson.Default.UpdateScheduleRequest)) + Assert.Equal("AuditRetentionDays", (await off.ExpectErrorAsync(HttpStatusCode.Conflict, "setting_locked")).Details); // Everything else stays editable. using var timing = await admin.PutAsJsonAsync($"/api/v1/schedules/{audit.Id}", new UpdateScheduleRequest(Recurrence: new RecurrenceDto("daily", Time: "05:00"), Parameters: new ScheduleParametersDto(Steps: ["audit", "job-history"], OlderThanDays: 30)), ProtocolJson.Default.UpdateScheduleRequest); await timing.ExpectAsync(HttpStatusCode.OK); + // Settings shows the fixed value, and the other settings still save with it. + Assert.Equal(30, (await SettingsAsync(admin)).Logs.AuditRetentionDays); + await SaveAuditRetentionAsync(admin, 30); + } + + /// + /// A missed audit clean-up caught up at start runs with the retention CHRONOS_AUDIT_RETENTION_DAYS fixes, not with the + /// age limit the schedule held before: the variable is applied before the first pass. + /// + [Test] + public async Task ALockedAuditRetentionAppliesToTheRunCaughtUpAtStart() + { + foreach (var days in new[] { 365, 0 }) + { + var root = context.CreateTempDirectory($"catch-up-{days}"); + void Paths(ServerOptions o) + { + o.DataPath = Path.Combine(root, "data"); + o.ArchivePath = Path.Combine(root, "archive"); + Directory.CreateDirectory(o.ArchivePath); + } + DatabaseOptions database; + await using (var first = await TestServer.StartAsync(context, Paths, ArchiveModule.Register)) + database = first.Options.ToDatabaseOptions(); + + // While the server was off: a 90-day audit clean-up whose run passed an hour ago, and an entry 200 days old. + var old = Guid.CreateVersion7(); + await using (var db = await ChronosDatabase.OpenAsync(database, Ct)) + { + var row = Assert.NotNull(await db.Schedules.GetBySystemKeyAsync(ServerScheduleHost.AuditCleanup, Ct)); + var parameters = ScheduleMapping.Parameters(row) with { OlderThanDays = 90 }; + Assert.True(await db.Schedules.UpdateAsync(row with + { + ParametersJson = ScheduleMapping.ParametersJson(row.Action, parameters), + Enabled = true, + NextRun = DateTimeOffset.UtcNow.AddHours(-1), + }, setNextRun: true, Ct)); + await db.Audit.AppendAsync(new AuditEntry { Id = old, At = DateTimeOffset.UtcNow.AddDays(-200), Action = "user.login" }, Ct); + } + + // CHRONOS_AUDIT_RETENTION_DAYS at the next start. + await using var second = await TestServer.StartAsync(context, o => + { + Paths(o); + o.AuditRetentionDays = days; + o.Locked = [nameof(ServerOptions.AuditRetentionDays)]; + }, ArchiveModule.Register); + var schedule = Assert.NotNull(await second.Database.Schedules.GetBySystemKeyAsync(ServerScheduleHost.AuditCleanup, Ct)); + var queued = await second.Database.Jobs.ListByScheduleAsync(schedule.Id, cancellationToken: Ct); + if (days == 0) + { + // Kept forever: the schedule is off before its missed run could fire. + Assert.False(schedule.Enabled); + Assert.Empty(queued); + } + else + { + Assert.Count(1, queued); + var job = queued[0]; + using (var json = JsonDocument.Parse(job.ParametersJson!)) + Assert.Equal(days, json.RootElement.GetProperty("olderThanDays").GetInt32()); + await JobApiTests.WaitForAsync(await second.AdminAsync(), job.Id, "done"); + } + var kept = (await second.Database.Audit.ListByActionsAsync(null, [], 1000, null, Ct)).Items.Select(e => e.Id); + Assert.Contains(old, kept); + } } [Test]