Files
Chronos/src/Chronos.App/Jobs/JobNotices.cs
T
Alby96andClaude Opus 5.5 283d04d9cd feat: found copies foundation (contracts, migration 0012, store, wire, trash and job hooks, keys)
Owner review 6 asks Chronos to recognise the same time-lapse held under different ids on the
server and on this PC, compare the copies, and keep them apart or integrate and sync them. This
lays the shared foundation the later parcels build on, without any UI or detection yet.

- Migration 0012 adds found_copies (local-server and local-local pairs with evidence, scores,
  answer, policy and the 404 record) and copy_probes; every existing link with a local sequence
  becomes a Linked pair, so a server deletion and the per-link policy have a place.
- FoundCopyStore keeps the pairs: upserts that never touch the answer, a compare-and-set of the
  state with an Integrating backstop (a failed or cancelled job reads as the previous state),
  link promotion and demotion, the once-per-refresh 404 count, the merge's links stage
  (session keys, links reset, pairs moved, refused when both are linked on one server), the
  local overlap queries and the probes. SyncLinkStore.RekeyAsync moves pairs and probes with
  the links in the same transaction.
- Contracts for detection, review, plans, the Integrate and MergeSequences jobs (kinds 25/26,
  High level, new detail steps) and the Upload all rule that keeps suspected, kept-apart,
  deleted, busy and server-born sequences here.
- Wire: POST photos/lookup, uploads into a target (IntoTarget/Copied, trailing and optional),
  frames/delete on the client, capabilities photo-lookup and upload-into, cached per remote.
- The trash plans and deletes accept the job that holds the sequence (its own hold is not
  Busy) and a frames version checked under the lease; a Background job never interrupts a
  running one.
- Settings for linking, the photo policy and projects; the en and it keys of design section 9.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:39:04 +01:00

106 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Chronos.Core.Model;
namespace Chronos.App.Jobs;
/// <summary>What the shell says when a job of this PC ends.</summary>
public enum JobNotice
{
None,
Done,
Failed,
}
/// <summary>
/// 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.
/// </summary>
public static class JobNotices
{
/// <summary>
/// The notice for <paramref name="job"/> after a change from <paramref name="before"/> (null: not seen before).
/// A job already finished before the change (a repeated event, a page read again) says nothing.
/// </summary>
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 or JobKind.Integrate or JobKind.MergeSequences;
/// <summary>
/// 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).
/// </summary>
private static bool IsUpkeep(JobKind kind) =>
kind is JobKind.Scan or JobKind.Thumbs or JobKind.Proxies or JobKind.LightAnalyse or JobKind.Cleanup;
}
/// <summary>Endings said in one toast: one job, or several of a batch.</summary>
public sealed record JobEnding(JobNotice Notice, IReadOnlyList<Job> Jobs);
/// <summary>
/// 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.
/// </summary>
public sealed class JobEndings
{
private readonly SortedDictionary<JobKind, List<Job>> _done = [];
private readonly List<Job> _failed = [];
/// <summary>Finished jobs still waiting for the rest of their kind (tests).</summary>
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);
}
}
/// <summary>
/// The toasts due now: the failures added since the last call, then each finished kind that <paramref name="busy"/>
/// no longer reports as queued or running. What is returned is forgotten.
/// </summary>
public IReadOnlyList<JobEnding> Take(Func<JobKind, bool> busy)
{
ArgumentNullException.ThrowIfNull(busy);
var due = new List<JobEnding>();
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;
}
}