feat(data): scheduler engine, schedules table and housekeeping jobs
Owner review 2, R10: every timer that queues work becomes a visible schedule, so the server and the desktop need one shared engine to run them. - Migration 0007: schedules table (system rows keyed by system_key) and jobs.schedule_id; Job.ScheduleId, JobDto.ScheduleId, JobRepository HasPendingAsync / ListPendingTargetsAsync / ListByScheduleAsync. - ScheduleRepository: seeding that keeps user edits, reset, compare-and-set claim of a run (one winner across processes), outcomes. - Scheduling: in-house recurrence (every/daily/weekly/monthly, DST gap moves forward, ambiguous time runs once), ScheduleActions resolving scan, verify, thumbnails, proxies, analysis, maintenance, backup, cleanup, duplicates and orphans into archive jobs, IScheduleHost, and the Scheduler loop (catch-up, clock going back, run now, CRUD as API v1 DTOs). - New job kinds Backup (16) and Cleanup (17) with handlers, shared clean-up steps (job history, audit log, trash report that never deletes) and host steps through ArchiveServiceOptions; read-only Duplicates and Orphans report handlers, so the server can run those kinds too. - JobEngine no longer purges history at start: the housekeeping schedule's job-history step does; JobEngine.CanRun and an EnqueueAsync with scheduleId. - Protocol DTOs for schedules; job kind names in en and it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit eb0edd2483a493db7c6945d5b494461cabb2a4ad)
This commit is contained in:
1 parent
0e84392f4f
commit
e5508e7e69
51 files changed
+3219
-30
No files matched your search
@@ -40,6 +40,8 @@ public sealed class ArchiveService : IJobHandlerProvider
|
||||
Images = new ArchiveImages(db, _writer, Indexer);
|
||||
Imports = new ImportService(db, Ingest);
|
||||
Trash = new SequenceTrash(db, _writer, Indexer);
|
||||
var shared = Housekeeping.CleanupSteps.Shared(Trash);
|
||||
CleanupSteps = [.. shared, .. _writer.Options.CleanupSteps.Where(s => !shared.Any(o => o.Name == s.Name))];
|
||||
}
|
||||
|
||||
public ArchiveIndexer Indexer { get; }
|
||||
@@ -56,7 +58,16 @@ public sealed class ArchiveService : IJobHandlerProvider
|
||||
/// <summary>Deletes sequences into the archive's trash and restores them.</summary>
|
||||
public SequenceTrash Trash { get; }
|
||||
|
||||
/// <summary>Handlers for scan, rebuild, verify, import, thumbs, proxies and maintenance jobs; register them on the <see cref="JobEngine"/>.</summary>
|
||||
/// <summary>
|
||||
/// The steps this host's <see cref="JobKind.Cleanup"/> jobs can run: the shared ones (<see cref="Housekeeping.CleanupSteps.Shared"/>),
|
||||
/// then the host's (<see cref="ArchiveServiceOptions.CleanupSteps"/>). A schedule host lists their names.
|
||||
/// </summary>
|
||||
public IReadOnlyList<Housekeeping.ICleanupStep> CleanupSteps { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Handlers for scan, rebuild, verify, import, thumbs, proxies and maintenance jobs, the read-only duplicates and orphans
|
||||
/// reports, index backups and clean-ups (owner review 2, R10); register them on the <see cref="JobEngine"/>.
|
||||
/// </summary>
|
||||
public IReadOnlyList<IJobHandler> CreateJobHandlers() =>
|
||||
[
|
||||
new ScanJobHandler(_db, Indexer, JobKind.Scan, _writer.Options.ImagesAfterScan),
|
||||
@@ -66,6 +77,10 @@ public sealed class ArchiveService : IJobHandlerProvider
|
||||
new ImageJobHandler(_db, Images, JobKind.Thumbs),
|
||||
new ImageJobHandler(_db, Images, JobKind.Proxies),
|
||||
new MaintenanceJobHandler(_db, Maintenance),
|
||||
new DuplicatesJobHandler(_db, Maintenance),
|
||||
new OrphansJobHandler(_db, Maintenance),
|
||||
new Housekeeping.BackupJobHandler(_db, _writer.Options.BackupDirectory),
|
||||
new Housekeeping.CleanupJobHandler(_db, CleanupSteps, TimeProvider.System),
|
||||
];
|
||||
|
||||
/// <summary>Makes <paramref name="root"/> a new, empty archive and indexes it.</summary>
|
||||
@@ -482,6 +497,24 @@ public static class ArchiveJobs
|
||||
new(JobKind.Maintenance, [archive.Id], archive.Name, Json(new MaintenanceParameters(dryRun), ArchiveJobJson.Default.MaintenanceParameters),
|
||||
JobPriority.Normal);
|
||||
|
||||
/// <summary><see cref="JobKind.Duplicates"/>: a read-only report of the archive's duplicate photos into the job log.</summary>
|
||||
public static ArchiveJobRequest Duplicates(Archive archive) => new(JobKind.Duplicates, [archive.Id], archive.Name, null, JobPriority.Normal);
|
||||
|
||||
/// <summary><see cref="JobKind.Orphans"/>: a read-only report of where the index and the disk disagree into the job log.</summary>
|
||||
public static ArchiveJobRequest Orphans(Archive archive) => new(JobKind.Orphans, [archive.Id], archive.Name, null, JobPriority.Normal);
|
||||
|
||||
/// <summary><see cref="JobKind.Backup"/>: a copy of the SQLite index into the backups folder, keeping the newest <paramref name="keep"/>.</summary>
|
||||
public static ArchiveJobRequest Backup(int keep = DatabaseBackups.Keep) =>
|
||||
new(JobKind.Backup, [], null, Json(new BackupParameters(keep), ArchiveJobJson.Default.BackupParameters), JobPriority.Normal);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="JobKind.Cleanup"/>: the clean-up <paramref name="steps"/> (by name, in order) with their age limit; the
|
||||
/// archive, when given, is what archive steps (the trash report) look at.
|
||||
/// </summary>
|
||||
public static ArchiveJobRequest Cleanup(Archive? archive, IReadOnlyList<string> steps, int olderThanDays) =>
|
||||
new(JobKind.Cleanup, archive is null ? [] : [archive.Id], archive?.Name,
|
||||
Json(new CleanupParameters(steps, olderThanDays), ArchiveJobJson.Default.CleanupParameters), JobPriority.Normal);
|
||||
|
||||
public static Task<Job> EnqueueAsync(this JobEngine engine, ArchiveJobRequest request, Guid? createdBy = null, CancellationToken cancellationToken = default) =>
|
||||
engine.EnqueueAsync(request.Kind, request.Targets, request.Label, request.ParametersJson, request.Priority, createdBy, cancellationToken);
|
||||
|
||||
|
||||
@@ -22,6 +22,15 @@ public sealed record MaintenanceParameters(bool DryRun);
|
||||
/// <summary><see cref="JobKind.Import"/>: the exact plan the user confirmed.</summary>
|
||||
public sealed record ImportJobParameters(Guid ArchiveId, ImportPlan Plan, Guid? CardId, bool Verify, Guid? UserId);
|
||||
|
||||
/// <summary><see cref="JobKind.Backup"/>: no target; the newest <paramref name="Keep"/> copies are kept.</summary>
|
||||
public sealed record BackupParameters(int Keep);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="JobKind.Cleanup"/>: target = the archive (or none); the clean-up steps to run, by name and in order, and
|
||||
/// the age in days from which what they clean goes (0 keeps everything).
|
||||
/// </summary>
|
||||
public sealed record CleanupParameters(IReadOnlyList<string> Steps, int OlderThanDays);
|
||||
|
||||
/// <summary>A job ready to enqueue.</summary>
|
||||
public sealed record ArchiveJobRequest(JobKind Kind, IReadOnlyList<Guid> Targets, string? Label, string? ParametersJson, JobPriority Priority);
|
||||
|
||||
@@ -31,6 +40,8 @@ public sealed record ArchiveJobRequest(JobKind Kind, IReadOnlyList<Guid> Targets
|
||||
[JsonSerializable(typeof(ImageJobParameters))]
|
||||
[JsonSerializable(typeof(MaintenanceParameters))]
|
||||
[JsonSerializable(typeof(ImportJobParameters))]
|
||||
[JsonSerializable(typeof(BackupParameters))]
|
||||
[JsonSerializable(typeof(CleanupParameters))]
|
||||
[JsonSerializable(typeof(CardProfile))]
|
||||
internal sealed partial class ArchiveJobJson : JsonSerializerContext
|
||||
{
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Jobs;
|
||||
|
||||
namespace Chronos.Data.Archiving.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="JobKind.Duplicates"/> as a job (owner review 2, R10): the archive's groups of photos sharing a hash, counted and
|
||||
/// the largest listed in the job log. Read-only: the Archive screen's Duplicates tab and the maintenance job's duplicate flags
|
||||
/// stay what acts on them. This makes the kind runnable on the server too (scheduled reports).
|
||||
/// </summary>
|
||||
internal sealed class DuplicatesJobHandler(ChronosDatabase db, ArchiveMaintenance maintenance) : IJobHandler
|
||||
{
|
||||
/// <summary>Groups listed one per log line, the largest first; the summary counts them all.</summary>
|
||||
private const int ListedGroups = 20;
|
||||
|
||||
public JobKind Kind => JobKind.Duplicates;
|
||||
|
||||
public async Task RunAsync(JobContext context)
|
||||
{
|
||||
var archive = await JobArchive.LoadAsync(db, context.Job, context.Cancellation).ConfigureAwait(false);
|
||||
await context.ReportAsync(0, 1).ConfigureAwait(false);
|
||||
var groups = await maintenance.FindDuplicatesAsync(archive.Id, context.Cancellation).ConfigureAwait(false);
|
||||
foreach (var group in groups.OrderByDescending(g => g.Frames.Count).ThenBy(g => g.Frames[0].RelativePath, StringComparer.Ordinal).Take(ListedGroups))
|
||||
context.Log(Loc.L("Archive.Log.DuplicateGroup", group.Frames[0].RelativePath, group.Frames.Count - 1));
|
||||
context.Log(Loc.L("Archive.Log.DuplicatesDone", groups.Count, groups.Sum(g => g.Frames.Count - 1)));
|
||||
context.Log(Loc.L("Archive.Log.ReportOnly"));
|
||||
await context.ReportAsync(1, 1).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="JobKind.Orphans"/> as a job (owner review 2, R10): where the index and the disk disagree — photos the index
|
||||
/// does not list, frames whose file is gone, sequences without their folder, folders not indexed yet — counted and listed in
|
||||
/// the job log. Read-only: the Archive screen's fixes and a scan stay what acts on them. The archive must be reachable.
|
||||
/// </summary>
|
||||
internal sealed class OrphansJobHandler(ChronosDatabase db, ArchiveMaintenance maintenance) : IJobHandler
|
||||
{
|
||||
/// <summary>Folders listed one per log line; the summary counts them all.</summary>
|
||||
private const int ListedFolders = 20;
|
||||
|
||||
public JobKind Kind => JobKind.Orphans;
|
||||
|
||||
public async Task RunAsync(JobContext context)
|
||||
{
|
||||
var archive = await JobArchive.LoadAsync(db, context.Job, context.Cancellation).ConfigureAwait(false);
|
||||
await context.ReportAsync(0, 1).ConfigureAwait(false);
|
||||
var report = await maintenance.FindOrphansAsync(archive, context.Cancellation).ConfigureAwait(false);
|
||||
foreach (var missing in report.MissingSequences.Take(ListedFolders))
|
||||
context.Log(Loc.L("Archive.Log.FolderMissing", missing.Folder));
|
||||
foreach (var folder in report.UnindexedFolders.Take(ListedFolders))
|
||||
context.Log(Loc.L("Archive.Log.FolderUnindexed", folder));
|
||||
context.Log(Loc.L("Archive.Log.Orphans", report.UnindexedFiles, report.MissingFiles));
|
||||
context.Log(Loc.L("Archive.Log.OrphanFolders", report.MissingSequences.Count, report.UnindexedFolders.Count));
|
||||
context.Log(Loc.L("Archive.Log.ReportOnly"));
|
||||
await context.ReportAsync(1, 1).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,19 @@ public sealed record ArchiveServiceOptions
|
||||
/// </summary>
|
||||
public Func<long> MemoryBudget { get; init; } = () => DefaultMemoryBudget;
|
||||
|
||||
/// <summary>
|
||||
/// The host's own clean-up steps (the server's uploads sweep, the desktop's cache trim), run by
|
||||
/// <see cref="Core.Model.JobKind.Cleanup"/> jobs after the shared ones of <see cref="Housekeeping.CleanupSteps"/> when a
|
||||
/// schedule names them (owner review 2, R10). A name the shared steps use already is ignored.
|
||||
/// </summary>
|
||||
public IReadOnlyList<Housekeeping.ICleanupStep> CleanupSteps { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Folder of the copies <see cref="Core.Model.JobKind.Backup"/> jobs make; null: the <c>backups</c> folder next to the
|
||||
/// SQLite index (<see cref="DatabaseBackups.DefaultDirectory"/>), where the desktop and the server keep them.
|
||||
/// </summary>
|
||||
public string? BackupDirectory { get; init; }
|
||||
|
||||
/// <summary>Half the logical cores, at least one: the parallelism when the host sets none.</summary>
|
||||
public static int DefaultParallelism => Math.Max(1, Environment.ProcessorCount / 2);
|
||||
|
||||
|
||||
Reference in new issue
Block a user