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

@@ -1,9 +1,11 @@
using System.IO;
using Chronos.App.Infrastructure;
using Chronos.App.Jobs;
using Chronos.App.Tests.Uploader;
using Chronos.Core.Model;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Scheduling;
using Chronos.Testing;
namespace Chronos.App.Tests.Infrastructure;
@@ -21,7 +23,7 @@ public sealed class AppServicesTests
[Test]
[WindowsOnly]
public void ExitStopsPollingAndTheEngineWhileTheUiThreadWaits()
public void ExitStopsTheSchedulesAndTheEngineWhileTheUiThreadWaits()
{
var environment = AppEnvironment.ForCapture();
try
@@ -33,11 +35,14 @@ public sealed class AppServicesTests
await new ArchiveService(setup).CreateAsync(root, "Exit");
var services = new AppServices(environment, new AppSettings { Language = "en", LastArchivePath = root }, () => { }, new NoDialogs());
await services.OpenAsync(environment.DatabasePath);
// The archive was reopened and its first rescan queued: polling now waits for the next one.
// The archive was reopened, then the schedules started (owner review 2, R10) and its scan schedule ran at once,
// as the rescans did at start: a scan of it is queued without waiting for the interval.
Assert.True(await services.SchedulerReady, "the schedules started");
var scan = Assert.NotNull(await services.Database!.Schedules.GetBySystemKeyAsync(DesktopScheduleHost.ArchiveScan));
var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);
while ((await services.Database!.Jobs.ListAsync(null, JobKind.Scan)).Items.Count == 0)
while (!(await services.Database!.Jobs.ListAsync(null, JobKind.Scan)).Items.Any(j => j.ScheduleId == scan.Id))
{
Assert.True(DateTime.UtcNow < until, "no rescan queued");
Assert.True(DateTime.UtcNow < until, "no scan at start");
await Task.Delay(50);
}
@@ -51,10 +56,13 @@ public sealed class AppServicesTests
}
}
/// <summary>BRIEF §7: polling follows the archive folder, also one chosen after the start (first run, Settings, relink).</summary>
/// <summary>
/// BRIEF §7, owner review 2 R10: the scheduled scans follow the archive folder, also one chosen after the start (first
/// run, Settings, relink).
/// </summary>
[Test]
[WindowsOnly]
public void RescansFollowTheArchiveFolderChosenAfterTheStart()
public void ScheduledScansFollowTheArchiveFolderChosenAfterTheStart()
{
var environment = AppEnvironment.ForCapture();
try
@@ -67,20 +75,21 @@ public sealed class AppServicesTests
try
{
await services.OpenAsync(environment.DatabasePath);
Assert.Null(await services.RescanAsync());
// The rescans run all the same: Settings › Archive › Rescan every reaches them.
settings.ScanIntervalMinutes = 30;
services.SaveSettings();
Assert.Equal<TimeSpan?>(TimeSpan.FromMinutes(30), services.PollingPeriod);
Assert.True(await services.SchedulerReady, "the schedules started");
Assert.Null(await services.ScheduleHost.GetArchiveAsync(default));
// The scans run all the same, on the schedule seeded from the former Settings › Archive › Rescan every.
var scan = Assert.NotNull(await services.Database!.Schedules.GetBySystemKeyAsync(DesktopScheduleHost.ArchiveScan));
Assert.Equal("every:10m", scan.Recurrence);
Assert.Equal(ScheduleOutcome.SkippedOffline, (await services.Scheduler!.RunNowAsync(scan.Id, null))?.Outcome);
// The first-run dialog creates an archive: the next rescan is of it.
// The first-run dialog creates an archive: the schedules work on it.
var firstRoot = Path.Combine(environment.DataDirectory, "first");
await services.ArchiveSetup.CreateAsync(firstRoot);
var first = Assert.NotNull(await services.RescanAsync());
var first = Assert.NotNull(await services.ScheduleHost.GetArchiveAsync(default));
Assert.Equal("first", first.Name);
// Settings › Archive moves to another archive, which the index lists after the first one: the next rescan
// is of the archive at the folder, no longer of the first one.
// Settings › Archive moves to another archive, which the index lists after the first one: the next scheduled
// scan is of the archive at the folder, no longer of the first one.
var other = Path.Combine(environment.DataDirectory, "zeta");
await new ArchiveService(services.Database!).CreateAsync(other, "zeta");
Assert.Equal("first", (await services.Database!.Archives.ListAsync())[0].Name);
@@ -89,15 +98,57 @@ public sealed class AppServicesTests
var scansOfFirst = await ScansOfAsync(first.Id);
settings.LastArchivePath = other;
services.SaveSettings();
var rescanned = Assert.NotNull(await services.RescanAsync());
var rescanned = Assert.NotNull(await services.ScheduleHost.GetArchiveAsync(default));
Assert.Equal("zeta", rescanned.Name);
Assert.Equal(ScheduleOutcome.Queued, (await services.Scheduler.RunNowAsync(scan.Id, null))?.Outcome);
Assert.True(await ScansOfAsync(rescanned.Id) > 0, "the archive at the folder is rescanned");
Assert.Equal(scansOfFirst, await ScansOfAsync(first.Id));
// And back: the folder decides, whatever the order of the index.
settings.LastArchivePath = firstRoot;
services.SaveSettings();
Assert.Equal(first.Id, (await services.RescanAsync())?.Id);
Assert.Equal(first.Id, (await services.ScheduleHost.GetArchiveAsync(default))?.Id);
}
finally
{
Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(10)), "services stopped");
}
});
}
finally
{
environment.DeleteCaptureData();
}
}
/// <summary>
/// Owner review 2, R10: when the index does not open, the schedules are known not to run at once, so Jobs › Scheduled
/// says so instead of waiting for them with an empty tab.
/// </summary>
[Test]
[WindowsOnly]
public void AnIndexThatDoesNotOpenTellsTheScheduledTabTheSchedulesDoNotRun()
{
var environment = AppEnvironment.ForCapture();
try
{
SingleThreadedContext.Run(async () =>
{
// "File is not a database", and no staged restore to recover it.
Directory.CreateDirectory(Path.GetDirectoryName(environment.DatabasePath)!);
await File.WriteAllBytesAsync(environment.DatabasePath, [.. Enumerable.Repeat((byte)0x5A, 8192)]);
var services = new AppServices(environment, new AppSettings { Language = "en" }, () => { }, new NoDialogs());
try
{
await services.OpenAsync(environment.DatabasePath);
Assert.NotNull(services.OpenError);
var ready = services.SchedulerReady;
Assert.Same(ready, await Task.WhenAny(ready, Task.Delay(TimeSpan.FromSeconds(TestTiming.Budget(10)))));
Assert.False(await ready, "no schedules without an index");
using var schedules = new SchedulesViewModel(() => services.Scheduler, () => services.Database, _ => false, (_, _, _) => { }, _ => Task.CompletedTask);
await schedules.AttachAsync(services.SchedulerReady);
Assert.True(schedules.IsLoaded && schedules.IsUnavailable && !schedules.IsRunning, "the tab shows that the schedules do not run");
}
finally
{
@@ -0,0 +1,77 @@
using System.IO;
using Chronos.App.Infrastructure;
using Chronos.App.Jobs;
using Chronos.Testing;
namespace Chronos.App.Tests.Infrastructure;
/// <summary>Owner review 2, R10: the housekeeping trims the cache to its limit, oldest files first, never what is in use or kept whole.</summary>
public sealed class CacheStoreTests(TestContext context)
{
private static readonly DateTime Start = new(2026, 9, 1, 8, 0, 0, DateTimeKind.Utc);
/// <summary>A cache file of <paramref name="bytes"/> last written <paramref name="day"/> days after <see cref="Start"/>.</summary>
private static string Write(string folder, string name, int bytes, int day)
{
var path = Path.Combine(folder, name);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllBytes(path, new byte[bytes]);
File.SetLastWriteTimeUtc(path, Start.AddDays(day));
return path;
}
[Test]
public void TheOldestFilesGoFirstUntilTheCacheFits()
{
var cache = context.CreateTempDirectory("cache");
CacheStore.Prepare(cache);
var oldest = Write(cache, "spill-1.bin", 400, 1);
var older = Write(cache, Path.Combine("render", "spill-2.bin"), 400, 2);
var recent = Write(cache, "spill-3.bin", 400, 3);
var size = CacheStore.Size(cache);
var result = Assert.NotNull(CacheStore.Trim(cache, size - 500));
// 400 bytes were not enough: the two oldest went, the newest and the marker stay.
Assert.Equal((size, 800L, 2), (result.Before, result.Freed, result.Deleted));
Assert.Equal(size - 800, result.After);
Assert.False(File.Exists(oldest));
Assert.False(File.Exists(older));
Assert.True(File.Exists(recent));
Assert.True(CacheStore.IsOwned(cache), "the marker stays");
// Within the limit: nothing to do.
Assert.Equal(0, CacheStore.Trim(cache, 1L << 30)!.Deleted);
}
[Test]
public void FilesInUseAndKeptFoldersStay()
{
var cache = context.CreateTempDirectory("cache");
CacheStore.Prepare(cache);
var proxies = Write(cache, Path.Combine("proxies", "server", "sequence", "frame.jpg"), 1000, 0);
var busy = Write(cache, "spill-busy.bin", 1000, 1);
var free = Write(cache, "spill-free.bin", 1000, 2);
using (new FileStream(busy, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var result = Assert.NotNull(CacheStore.Trim(cache, 0, CacheTrimStep.Kept));
Assert.Equal((1000L, 1), (result.Freed, result.Deleted));
}
Assert.True(File.Exists(proxies), "a kept folder is never trimmed");
Assert.True(File.Exists(busy), "a file in use is skipped");
Assert.False(File.Exists(free));
}
[Test]
public void AFolderChronosDidNotMakeIsLeftAlone()
{
var folder = context.CreateTempDirectory("photos");
var photo = Write(folder, "IMG_0001.JPG", 1000, 0);
Assert.Null(CacheStore.Trim(folder, 0));
Assert.True(File.Exists(photo));
Assert.Equal((0L, 0L, 0), CacheStore.Trim(Path.Combine(folder, "missing"), 0) is { } none ? (none.Before, none.Freed, none.Deleted) : (-1L, -1L, -1));
}
}