fix(archive): server scans queue one image pass per sequence

A batched Thumbs or Proxies pass stopped at the first sequence whose folder
was renamed, deleted or damaged before the pass ran, and the others kept no
images until an administrator retried it. Each indexed sequence now gets its
own background pass, labelled with its title and de-duplicated against a
queued pass of its own, as after an upload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 16:29:32 +00:00
1 parent ed5c0c71c8
commit bb7546b2ea
2 files changed
+87 -28

No files matched your search

@@ -23,6 +23,23 @@ public sealed class ArchiveJobTests(TestContext context)
private static async Task<IReadOnlyList<SequenceLocation>> LocationsAsync(ArchiveFixture fx) =>
await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id);
/// <summary>The server's archive engine (ArchiveModule), not started; the desktop's scans queue no image passes.</summary>
private static JobEngine ServerEngine(ArchiveFixture fx, params IJobHandler[] others)
{
var service = new ArchiveService(fx.Db, new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20), ImagesAfterScan = true });
return new JobEngine(fx.Db, [.. service.CreateJobHandlers(), .. others],
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
}
private static bool Built(SequenceStatus status) => status.HasFlag(SequenceStatus.ThumbsBuilt) && status.HasFlag(SequenceStatus.ProxiesBuilt);
private sealed class CallbackHandler(JobKind kind, Func<JobContext, Task> run) : IJobHandler
{
public JobKind Kind => kind;
public Task RunAsync(JobContext context) => run(context);
}
[Test]
public async Task ScanAndRebuildJobsRunAndAScanResumesAfterItsCheckpoint()
{
@@ -97,31 +114,34 @@ public sealed class ArchiveJobTests(TestContext context)
{
await using var fx = await CreateAsync(context);
Populate(fx);
// The server's archive engine (ArchiveModule); the desktop's scans queue no image passes.
var service = new ArchiveService(fx.Db, new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20), ImagesAfterScan = true });
await using var engine = new JobEngine(fx.Db, service.CreateJobHandlers(),
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
await using var engine = ServerEngine(fx);
await engine.StartAsync(fx.Ct);
var scan = await engine.EnqueueAsync(ArchiveJobs.Scan(fx.Archive), cancellationToken: fx.Ct);
await fx.WaitForAsync(scan.Id, JobState.Done);
var locations = await LocationsAsync(fx);
var first = new Dictionary<JobKind, Guid>();
var first = new HashSet<Guid>();
foreach (var kind in new[] { JobKind.Thumbs, JobKind.Proxies })
{
var pass = (await fx.Db.Jobs.ListAsync(null, kind, cancellationToken: fx.Ct)).Items.Single();
Assert.SequenceEqual(locations.Select(l => l.Id), pass.TargetIds);
Assert.Equal(JobPriority.Background, pass.Priority);
await fx.WaitForAsync(pass.Id, JobState.Done);
first[kind] = pass.Id;
// One pass per sequence, named after it, as after an upload.
var passes = (await fx.Db.Jobs.ListAsync(null, kind, cancellationToken: fx.Ct)).Items;
Assert.SequenceEqual(locations.Select(l => l.Id).Order(), passes.SelectMany(j => j.TargetIds).Order());
foreach (var pass in passes)
{
Assert.Count(1, pass.TargetIds);
var target = Assert.NotNull(await fx.Db.Sequences.GetAsync(pass.TargetIds[0]));
Assert.Equal((JobPriority.Background, target.DisplayTitle), (pass.Priority, pass.TargetLabel));
await fx.WaitForAsync(pass.Id, JobState.Done);
first.Add(pass.Id);
}
}
foreach (var location in locations) Assert.True(Built((await fx.Db.Sequences.GetAsync(location.Id))!.Status), location.Folder);
// Every folder indexed again, but none without its images: nothing is queued.
var full = await engine.EnqueueAsync(ArchiveJobs.Scan(fx.Archive, full: true), cancellationToken: fx.Ct);
await fx.WaitForAsync(full.Id, JobState.Done);
Assert.Count(1, (await fx.Db.Jobs.ListAsync(null, JobKind.Thumbs, cancellationToken: fx.Ct)).Items);
Assert.Count(1, (await fx.Db.Jobs.ListAsync(null, JobKind.Proxies, cancellationToken: fx.Ct)).Items);
Assert.Count(2, (await fx.Db.Jobs.ListAsync(null, JobKind.Thumbs, cancellationToken: fx.Ct)).Items);
Assert.Count(2, (await fx.Db.Jobs.ListAsync(null, JobKind.Proxies, cancellationToken: fx.Ct)).Items);
// A photo added to a folder: its sequence lacks the images of that frame until the passes queued for it alone ran.
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start.AddMinutes(5), 1, firstSeed: 90, firstNumber: 7);
@@ -130,7 +150,7 @@ public sealed class ArchiveJobTests(TestContext context)
var a = locations.Single(l => l.Folder == "a");
foreach (var kind in new[] { JobKind.Thumbs, JobKind.Proxies })
{
var pass = (await fx.Db.Jobs.ListAsync(null, kind, cancellationToken: fx.Ct)).Items.Single(j => j.Id != first[kind]);
var pass = (await fx.Db.Jobs.ListAsync(null, kind, cancellationToken: fx.Ct)).Items.Single(j => !first.Contains(j.Id));
Assert.SequenceEqual([a.Id], pass.TargetIds);
await fx.WaitForAsync(pass.Id, JobState.Done);
}
@@ -138,8 +158,43 @@ public sealed class ArchiveJobTests(TestContext context)
using var pack = ThumbPackReader.Open(fx.Paths.Sequence("a").ThumbsPackPath);
Assert.Count(7, pack.Entries);
Assert.Count(7, Directory.GetFiles(fx.Paths.Sequence("a").Proxies));
}
static bool Built(SequenceStatus status) => status.HasFlag(SequenceStatus.ThumbsBuilt) && status.HasFlag(SequenceStatus.ProxiesBuilt);
[Test]
public async Task AFolderGoneBeforeItsImagePassesFailsThemAloneAndTheOtherSequencesGetTheirImages()
{
await using var fx = await CreateAsync(context);
Populate(fx);
// The only worker runs the scan, then this job ahead of the background passes the scan queued: the folder goes
// meanwhile, as when someone renames or deletes it on the NAS between a poll and its passes.
var holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await using var engine = ServerEngine(fx, new CallbackHandler(JobKind.Render, async c =>
{
holding.SetResult();
await release.Task.WaitAsync(c.Cancellation);
}));
var scan = await engine.EnqueueAsync(ArchiveJobs.Scan(fx.Archive), cancellationToken: fx.Ct);
var hold = await engine.EnqueueAsync(JobKind.Render, [fx.Archive.Id], priority: JobPriority.Normal, cancellationToken: fx.Ct);
await engine.StartAsync(fx.Ct);
await holding.Task.WaitAsync(TimeSpan.FromSeconds(30), fx.Ct);
Assert.Equal(JobState.Done, (await fx.Db.Jobs.GetAsync(scan.Id, fx.Ct))!.State);
var locations = await LocationsAsync(fx);
var (a, b) = (locations.Single(l => l.Folder == "a"), locations.Single(l => l.Folder == "b"));
Directory.Delete(Path.Combine(fx.Paths.Root, "a"), recursive: true);
release.SetResult();
await fx.WaitForAsync(hold.Id, JobState.Done);
foreach (var kind in new[] { JobKind.Thumbs, JobKind.Proxies })
{
var passes = (await fx.Db.Jobs.ListAsync(null, kind, cancellationToken: fx.Ct)).Items;
await fx.WaitForAsync(passes.Single(j => j.TargetIds.SequenceEqual([a.Id])).Id, JobState.Failed);
await fx.WaitForAsync(passes.Single(j => j.TargetIds.SequenceEqual([b.Id])).Id, JobState.Done);
}
Assert.True(Built((await fx.Db.Sequences.GetAsync(b.Id))!.Status), "b built");
using var pack = ThumbPackReader.Open(fx.Paths.Sequence("b").ThumbsPackPath);
Assert.Count(6, pack.Entries);
Assert.Count(6, Directory.GetFiles(fx.Paths.Sequence("b").Proxies));
}
[Test]