feat(server): scheduled jobs on the server and a Scheduled tab in the admin panel

Owner review 2, R10: nothing may run out of sight, so every timer the server
ran becomes a system schedule the admin panel shows and manages, and
administrators create their own.

- SchedulerHost runs the shared Scheduler on the job engine (started after
  JobEngineHost, stopped before it); ServerScheduleHost gives it the
  container's time zone (UTC with a warning when TZ is unknown), the archive
  refreshed from archive.json at every run (what ArchivePolling did), the
  clean-up steps and the system schedules: archive-scan every 10 minutes
  (no catch-up, also run once at start like the polling), upload-cleanup
  every 30 minutes, index-backup at 03:00 (SQLite only), housekeeping (job
  history) at 04:00 and audit-cleanup at 04:30.
- ArchivePolling and DatabaseBackupService are gone; UploadMaintenance only
  recovers commits at start, its sweep is the "uploads" clean-up step.
- /api/v1/schedules (ManageArchive, audited): list with actions and time
  zone, create, PUT/PATCH, delete (409 schedule_system), run now (409
  schedule_pending), reset (409 schedule_no_defaults), runs paged by cursor;
  invalid fields are 400 invalid_schedule naming the field.
- CHRONOS_AUDIT_RETENTION_DAYS (AuditRetentionDays, default 365) seeds the
  audit clean-up; set by the environment it is applied at every start and
  locked. Job history leaves Settings: it is the housekeeping schedule's age
  limit, and the setting is what a reset restores.
- Admin panel: Jobs has Queue and Scheduled tabs; schedules with when (in
  the panel's language and the server's zone), next run, last result, a
  switch, Run now, Edit, Delete or Reset, their runs linking to the jobs, and
  a side panel for the action, recurrence and parameters; job rows a
  schedule queued carry a Scheduled pill. Strings in en and it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 808ce94ae29e3bf4522cb4d40dfd76d4f8529c64)
This commit is contained in:
Alby96 committed 2026-10-27 02:25:01 +01:00
1 parent e5508e7e69
commit 75daab7275
66 files changed
+1985 -290

No files matched your search

@@ -52,12 +52,16 @@ public sealed class ArchiveModuleTests(TestContext context)
}
[Test]
public async Task AServerOnSqliteBacksUpItsIndexOnStart()
public async Task TheIndexBackupScheduleCopiesAnSqliteIndexIntoTheDataFolder()
{
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var backups = Path.Combine(server.Options.DataPath, "backups");
// No copy yet, so the first one is made at once (BRIEF §6.2), named after the index it copies.
// The nightly copy (BRIEF §6.2) is the index-backup system schedule's (owner review 2, R10), here run now.
var schedule = Assert.NotNull(await server.Database.Schedules.GetBySystemKeyAsync(ServerScheduleHost.IndexBackup, context.Cancellation));
Assert.Equal("daily:03:00", schedule.Recurrence);
var run = Assert.NotNull(await server.Services.GetRequiredService<SchedulerHost>().Scheduler.RunNowAsync(schedule.Id, null, context.Cancellation));
Assert.Equal("queued", run.Outcome);
var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);
while (!Directory.Exists(backups) || Directory.GetFiles(backups, "*.db").Length == 0)
{
@@ -71,7 +75,7 @@ public sealed class ArchiveModuleTests(TestContext context)
}
[Test]
public async Task PhotosAlreadyInTheArchiveAndAddedLaterAreIndexedByPollingWithThumbnailsAndProxies()
public async Task PhotosAlreadyInTheArchiveAndAddedLaterAreIndexedByTheScanScheduleWithThumbnailsAndProxies()
{
var photo = Photo();
// An adopted folder that already holds photos.
@@ -87,10 +91,9 @@ public sealed class ArchiveModuleTests(TestContext context)
// The scan queues the thumbnails and proxies clients load from the server, as an upload does.
await UntilImagesAsync(server, editor, archive.Id, "roof");
// Another writer adds a folder: the next poll queues a scan, but not while one is pending.
// Another writer adds a folder: the next run of the scan schedule queues a scan, but not while one is pending.
Directory.CreateDirectory(Path.Combine(server.Options.ArchivePath, "garden"));
File.WriteAllBytes(Path.Combine(server.Options.ArchivePath, "garden", "IMG_0002.JPG"), Photo(seed: 3));
var polling = server.Services.GetRequiredService<ArchivePolling>();
var paused = new Job
{
Id = Guid.CreateVersion7(),
@@ -100,11 +103,13 @@ public sealed class ArchiveModuleTests(TestContext context)
Created = DateTimeOffset.UtcNow,
};
await server.Database.Jobs.EnqueueAsync(paused, context.Cancellation);
Assert.Null(await polling.PollAsync(context.Cancellation));
Assert.Equal("skipped-pending", (await RunScanAsync(server)).Outcome);
await server.Services.GetRequiredService<JobEngineHost>().Engine.CancelAsync(paused.Id, context.Cancellation);
var scan = Assert.NotNull(await polling.PollAsync(context.Cancellation));
Assert.Equal(JobPriority.Background, scan.Priority);
var run = await RunScanAsync(server);
Assert.Equal("queued", run.Outcome);
var scan = Assert.NotNull(await server.Database.Jobs.GetAsync(run.JobId!.Value, context.Cancellation));
Assert.Equal((JobKind.Scan, JobPriority.Background, (Guid?)run.Schedule.Id), (scan.Kind, scan.Priority, scan.ScheduleId));
await UntilIndexedAsync(server, archive.Id, "garden");
await UntilImagesAsync(server, editor, archive.Id, "garden");
}
@@ -141,8 +146,8 @@ public sealed class ArchiveModuleTests(TestContext context)
}
Assert.Equal(1, Assert.NotNull(await server.Database.Sequences.GetAsync(roof.Id)).FrameCount);
// The next poll indexes it and queues the images: the new frame and the one it moved get theirs.
Assert.NotNull(await server.Services.GetRequiredService<ArchivePolling>().PollAsync(context.Cancellation));
// The scan schedule's next run indexes it and queues the images: the new frame and the one it moved get theirs.
Assert.Equal("queued", (await RunScanAsync(server)).Outcome);
await UntilScannedAsync(server);
Assert.Equal(2, Assert.NotNull(await server.Database.Sequences.GetAsync(roof.Id)).FrameCount);
await UntilImagesAsync(server, editor, archive.Id, "roof");
@@ -182,11 +187,12 @@ public sealed class ArchiveModuleTests(TestContext context)
{
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
var archive = server.Services.GetRequiredService<ServerArchive>();
var polling = server.Services.GetRequiredService<ArchivePolling>();
// Every run of a schedule reads the archive through its host, which takes archive.json's settings first.
var schedules = server.Services.GetRequiredService<ServerScheduleHost>();
// Nothing changed: the served archive (the one an administrator's edit put there) is not replaced by a row read again.
var served = archive.Current;
await polling.PollAsync(context.Cancellation);
Assert.True(ReferenceEquals(served, archive.Current), "a poll that registered nothing keeps the served archive");
await schedules.GetArchiveAsync(context.Cancellation);
Assert.True(ReferenceEquals(served, archive.Current), "a refresh that registered nothing keeps the served archive");
// A desktop sharing the archive changed its folder template and proxy size in archive.json.
var path = new ArchivePaths(archive.Root).ManifestPath;
@@ -197,14 +203,14 @@ public sealed class ArchiveModuleTests(TestContext context)
ProxyPolicy = manifest.ProxyPolicy with { LongEdge = 2560 },
});
await polling.PollAsync(context.Cancellation);
Assert.Equal("{camera}/{date}", Assert.NotNull(await schedules.GetArchiveAsync(context.Cancellation)).FolderTemplate);
// Uploads (the index row) and the admin panel (the served archive) follow it.
Assert.Equal(("{camera}/{date}", 2560), (archive.Current.FolderTemplate, archive.Current.ProxyLongEdge));
var row = Assert.NotNull(await server.Database.Archives.GetAsync(archive.Id, context.Cancellation));
Assert.Equal(("{camera}/{date}", 2560), (row.FolderTemplate, row.ProxyLongEdge));
// The admin panel opens before the next poll: its form, which saves every field, starts from the desktop's change.
// The admin panel opens before the next run: its form, which saves every field, starts from the desktop's change.
ManifestStore.WriteArchive(path, ManifestStore.ReadArchive(path) with { LayoutTemplates = manifest.LayoutTemplates with { Folder = "{year}/{camera}" } });
var admin = await server.AdminAsync();
using (var read = await admin.GetAsync("/api/v1/archive/settings"))
@@ -275,6 +281,13 @@ public sealed class ArchiveModuleTests(TestContext context)
}
}
/// <summary>Runs the archive-scan system schedule now, as its next run would.</summary>
private static async Task<ScheduleRunDto> RunScanAsync(TestServer server)
{
var schedule = Assert.NotNull(await server.Database.Schedules.GetBySystemKeyAsync(ServerScheduleHost.ArchiveScan));
return Assert.NotNull(await server.Services.GetRequiredService<SchedulerHost>().Scheduler.RunNowAsync(schedule.Id, null));
}
/// <summary>Waits for the scan queued at start to finish: its sequences are indexed before it ends.</summary>
private static async Task UntilScannedAsync(TestServer server)
{