using Chronos.Core.Model; namespace Chronos.App.Jobs; /// What the shell says when a job of this PC ends. public enum JobNotice { None, Done, Failed, } /// /// Which job endings raise a toast (BRIEF §12.1 "toasts for job completion"): the long operations the user asked for /// end with one toast, whichever screen started them. Background work (thumbnails, proxies, light analysis, the /// analysis queued after an import) and scans (the archive scan schedule runs every 10 minutes) end silently; an import /// has its own toast when it ends well, and a download shows on its Library card and Server row. Every other failure /// is said. A schedule's background run (owner review 2, R10: the index backup, a weekly verify) is said only when it /// fails, unless it is upkeep the next run repeats. Cancelled jobs were stopped on purpose. /// public static class JobNotices { /// /// The notice for after a change from (null: not seen before). /// A job already finished before the change (a repeated event, a page read again) says nothing. /// public static JobNotice For(JobState? before, Job job) { ArgumentNullException.ThrowIfNull(job); if (before is JobState.Done or JobState.Failed or JobState.Cancelled) return JobNotice.None; if (job.Priority == JobPriority.Background) return job.ScheduleId is not null && job.State == JobState.Failed && !IsUpkeep(job.Kind) ? JobNotice.Failed : JobNotice.None; return job.State switch { JobState.Done when Announces(job.Kind) => JobNotice.Done, JobState.Failed when !IsUpkeep(job.Kind) => JobNotice.Failed, _ => JobNotice.None, }; } private static bool Announces(JobKind kind) => kind is JobKind.Render or JobKind.DeepAnalyse or JobKind.Verify or JobKind.Rebuild or JobKind.Maintenance or JobKind.UploadCommit or JobKind.Move or JobKind.Adopt or JobKind.EmptyTrash or JobKind.Convert or JobKind.WorkspaceRender or JobKind.WorkspaceSend or JobKind.WorkspaceMove or JobKind.VideoCopy; /// /// Work that keeps the index and its previews current: the next run repeats it, the Archive screen shows what is missing. /// Clean-ups too: the housekeeping schedule runs them again (a failed index backup is said). /// private static bool IsUpkeep(JobKind kind) => kind is JobKind.Scan or JobKind.Thumbs or JobKind.Proxies or JobKind.LightAnalyse or JobKind.Cleanup; } /// Endings said in one toast: one job, or several of a batch. public sealed record JobEnding(JobNotice Notice, IReadOnlyList Jobs); /// /// Job endings waiting for their toast, so that a batch speaks once (Server › Upload all queues one upload per /// sequence): finished jobs of one kind wait until no job of that kind the user waits for is queued or running, then /// one toast says how many finished; failures are said at the end of the update that brought them, together when /// several failed at once. WPF-free; feed it on the UI thread. /// public sealed class JobEndings { private readonly SortedDictionary> _done = []; private readonly List _failed = []; /// Finished jobs still waiting for the rest of their kind (tests). public int Waiting => _done.Values.Sum(l => l.Count); public void Add(JobNotice notice, Job job) { ArgumentNullException.ThrowIfNull(job); if (notice == JobNotice.Failed) { _failed.Add(job); } else if (notice == JobNotice.Done) { if (!_done.TryGetValue(job.Kind, out var jobs)) _done[job.Kind] = jobs = []; jobs.Add(job); } } /// /// The toasts due now: the failures added since the last call, then each finished kind that /// no longer reports as queued or running. What is returned is forgotten. /// public IReadOnlyList Take(Func busy) { ArgumentNullException.ThrowIfNull(busy); var due = new List(); if (_failed.Count > 0) { due.Add(new JobEnding(JobNotice.Failed, [.. _failed])); _failed.Clear(); } foreach (var kind in _done.Keys.Where(k => !busy(k)).ToList()) { due.Add(new JobEnding(JobNotice.Done, _done[kind])); _done.Remove(kind); } return due; } }