feat: jobs run one at a time, parallelism per photo and a priority queue

Owner review 1, phase 2 (docs/requests/2026-10-15-owner-review-1.md, R2).

Jobs now run one at a time on the desktop and on the server. Parallelism
(Settings > Rendering, TITANO_PARALLELISM) is how many photos the running
job processes at once: thumbnail and proxy passes draw that many photos
together and still write the pack and checkpoints in frame order, decode
tiers are probed that many at a time, the deep analysis measures that many
frames and a render decodes that many ahead. Wherever originals are
developed whole (TIFF, PNG, RAW without a usable preview), fewer run at
once within the memory budget, which the running job now has whole; the
server's frame window is sized to the host instead of 1 GiB.

A new job waits its turn and pausing the running job starts the next one.
An optional priority queue gives each job kind a level (renders, imports
and downloads high, verifies and proxies low by default): a queued job of
a higher level pauses the running one without asking, and that job goes
back to the queue with its checkpoint and resumes by itself afterwards.
Jobs of the same level, and a job's own follow-ups, never interrupt it.
The Jobs screen and the admin panel show the parallelism and the queue;
Settings > Rendering and the admin panel's Settings set them, at once.

TITANO_WORKERS and the Workers key of server.json still set the
parallelism (1 to 64, with a warning at start for the variable).
Titano.Tools gains i18n prune, which removed the renamed keys from the 36
machine-translated languages; the new strings are translated in all 38.

Found while testing: uploads and imports failed when a pass read the
sidecar while they wrote it (atomic writes now retry the rename while a
reader holds the file, and never for a read-only one), and taking a lease
just released while another process read it failed on Windows (the create
now waits for the reader). Five adversarial review rounds checked the
phase before this commit.

Also updates BRIEF (12.8, 14.6, 14.8, 14.9, 24), DECISIONS, CHANGELOG,
ARCHITECTURE, STATUS, the request file and the compose/Unraid templates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-10-16 14:56:24 +02:00
1 parent 34d03051f5
commit 43b6871cdc
228 files changed
+3535 -793

No files matched your search

+164 -41
View File
@@ -252,63 +252,186 @@ public sealed class JobEngineTests(TestContext context)
Assert.Empty((await db.Jobs.ListAsync(null, JobKind.LightAnalyse)).Items);
}
[Test]
public async Task TheNumberOfWorkersChangesWhileTheEngineRuns()
/// <summary>Waits until <paramref name="condition"/> holds, failing with <paramref name="what"/> after ten seconds.</summary>
private static async Task UntilAsync(Func<bool> condition, string what)
{
await using var db = await OpenAsync();
var running = 0;
var started = 0;
var release = new SemaphoreSlim(0);
var handler = new CallbackHandler(JobKind.Thumbs, async c =>
var until = DateTime.UtcNow.AddSeconds(10);
while (!condition())
{
Interlocked.Increment(ref running);
Interlocked.Increment(ref started);
Assert.True(DateTime.UtcNow < until, what);
await Task.Delay(20);
}
}
/// <summary>A handler whose jobs run until released, counting those running and started, by kind.</summary>
private sealed class GatedHandler(JobKind kind, SemaphoreSlim release) : IJobHandler
{
public int Running;
public int Started;
public JobKind Kind => kind;
public async Task RunAsync(JobContext c)
{
Interlocked.Increment(ref Running);
Interlocked.Increment(ref Started);
try
{
await release.WaitAsync(c.Cancellation);
}
finally
{
Interlocked.Decrement(ref running);
}
});
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50) });
await engine.StartAsync();
var jobs = new List<Job>();
for (var i = 0; i < 6; i++) jobs.Add(await engine.EnqueueAsync(JobKind.Thumbs, []));
async Task UntilAsync(Func<bool> condition, string what)
{
var until = DateTime.UtcNow.AddSeconds(10);
while (!condition())
{
Assert.True(DateTime.UtcNow < until, what);
await Task.Delay(20);
Interlocked.Decrement(ref Running);
}
}
}
// One worker: one job at a time.
await UntilAsync(() => Volatile.Read(ref running) == 1, "the first job runs");
[Test]
public async Task JobsRunOneAtATimeAndPausingStartsTheNext()
{
// Owner review 1 (R2): one job at a time by default; pausing the running job lets the next one start.
await using var db = await OpenAsync();
var release = new SemaphoreSlim(0);
var handler = new GatedHandler(JobKind.Thumbs, release);
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50) });
Assert.Equal(1, engine.Options.Workers);
await engine.StartAsync();
var jobs = new List<Job>();
for (var i = 0; i < 3; i++) jobs.Add(await engine.EnqueueAsync(JobKind.Thumbs, []));
await UntilAsync(() => Volatile.Read(ref handler.Running) == 1, "the first job runs");
await Task.Delay(300);
Assert.Equal(1, Volatile.Read(ref running));
Assert.Equal((1, 1), (Volatile.Read(ref handler.Running), Volatile.Read(ref handler.Started)));
Assert.Count(1, engine.RunningJobIds);
var first = engine.RunningJobIds.First();
Assert.Equal("pc", (await db.Jobs.GetAsync(first))!.NodeId);
// Three workers: two more jobs start at once, and no fourth.
engine.SetWorkers(3);
Assert.Equal(3, engine.Workers);
await UntilAsync(() => Volatile.Read(ref running) == 3, "three jobs run together");
await engine.PauseAsync(first);
await UntilAsync(() => Volatile.Read(ref handler.Started) == 2 && Volatile.Read(ref handler.Running) == 1, "the next job starts");
Assert.Equal(JobState.Paused, (await db.Jobs.GetAsync(first))!.State);
release.Release(10);
foreach (var job in jobs.Where(j => j.Id != first)) await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.Equal(JobState.Paused, (await db.Jobs.GetAsync(first))!.State);
}
[Test]
public async Task WithoutThePriorityQueueARunningJobIsNeverInterrupted()
{
await using var db = await OpenAsync();
var release = new SemaphoreSlim(0);
var scan = new GatedHandler(JobKind.Scan, release);
var render = new GatedHandler(JobKind.Render, release);
await using var engine = new JobEngine(db, [scan, render], new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50) });
await engine.StartAsync();
var scanning = await engine.EnqueueAsync(JobKind.Scan, []);
await UntilAsync(() => Volatile.Read(ref scan.Running) == 1, "the scan runs");
var rendering = await engine.EnqueueAsync(JobKind.Render, [], priority: JobPriority.User);
await Task.Delay(400);
Assert.Equal((1, 0), (Volatile.Read(ref scan.Running), Volatile.Read(ref render.Started)));
Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(rendering.Id))!.State);
release.Release(10);
await WaitForStateAsync(db, scanning.Id, JobState.Done, TimeSpan.FromSeconds(10));
await WaitForStateAsync(db, rendering.Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.Equal(1, Volatile.Read(ref scan.Started));
}
[Test]
public async Task AHigherJobPausesTheRunningOneWhichResumesFromItsCheckpoint()
{
// Owner review 1 (R2): with render at the highest level, starting a render while a scan runs pauses the scan
// without asking, renders, and resumes the scan by itself where it was.
await using var db = await OpenAsync();
var checkpointed = new TaskCompletionSource();
var go = new SemaphoreSlim(0);
var scan = new CountingHandler(JobKind.Scan, 4, async (i, ct) =>
{
if (i < 2) return;
checkpointed.TrySetResult();
await go.WaitAsync(ct);
});
var renderRelease = new SemaphoreSlim(0);
var render = new GatedHandler(JobKind.Render, renderRelease);
var queue = new JobPriorityQueue { Enabled = true };
Assert.Equal((JobKindPriority.High, JobKindPriority.Normal), (queue.LevelOf(JobKind.Render), queue.LevelOf(JobKind.Scan)));
await using var engine = new JobEngine(db, [scan, render],
new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50), PriorityQueue = () => queue });
await engine.StartAsync();
var scanning = await engine.EnqueueAsync(JobKind.Scan, []);
await checkpointed.Task.WaitAsync(TimeSpan.FromSeconds(10));
var rendering = await engine.EnqueueAsync(JobKind.Render, [], priority: JobPriority.User);
await UntilAsync(() => Volatile.Read(ref render.Running) == 1, "the render starts at once");
var paused = await db.Jobs.GetAsync(scanning.Id);
Assert.Equal((JobState.Queued, "2", (string?)null), (paused!.State, paused.Checkpoint, paused.NodeId));
Assert.Contains(Titano.Core.Localization.Loc.L("Jobs.Kind.Render"), paused.LogTail);
// The scan waits while the render runs, then resumes from its checkpoint without being asked.
go.Release(10);
await Task.Delay(300);
Assert.Equal(3, Volatile.Read(ref running));
Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(scanning.Id))!.State);
renderRelease.Release();
await WaitForStateAsync(db, rendering.Id, JobState.Done, TimeSpan.FromSeconds(10));
await WaitForStateAsync(db, scanning.Id, JobState.Done, TimeSpan.FromSeconds(10));
lock (scan.SeenCheckpoints) Assert.SequenceEqual([null, "2"], scan.SeenCheckpoints);
Assert.Equal(1, scan.Completed);
}
// Back to one: the running jobs finish undisturbed, then a single worker carries on.
engine.SetWorkers(1);
release.Release(3);
await UntilAsync(() => Volatile.Read(ref started) == 4 && Volatile.Read(ref running) == 1, "one job after the three");
await Task.Delay(300);
Assert.Equal((4, 1), (Volatile.Read(ref started), Volatile.Read(ref running)));
[Test]
public async Task OnlyAHigherLevelInterruptsAndNeverTheJobsOwnFollowUps()
{
await using var db = await OpenAsync();
var release = new SemaphoreSlim(0);
var queued = new TaskCompletionSource<Job?>();
// The light analysis queues a deep analysis of the same sequence, which the user put higher.
var light = new CallbackHandler(JobKind.LightAnalyse, async c =>
{
queued.TrySetResult(await c.EnqueueFollowUpAsync(JobKind.DeepAnalyse, [Guid.NewGuid()], "Roof", null, JobPriority.Background));
await release.WaitAsync(c.Cancellation);
});
var deep = new GatedHandler(JobKind.DeepAnalyse, release);
var thumbs = new GatedHandler(JobKind.Thumbs, release);
var queue = new JobPriorityQueue
{
Enabled = true,
Levels = new Dictionary<JobKind, JobKindPriority> { [JobKind.DeepAnalyse] = JobKindPriority.High, [JobKind.Thumbs] = JobKindPriority.Normal },
};
await using var engine = new JobEngine(db, [light, deep, thumbs],
new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50), PriorityQueue = () => queue });
await engine.StartAsync();
var analysing = await engine.EnqueueAsync(JobKind.LightAnalyse, []);
var followUp = Assert.NotNull(await queued.Task.WaitAsync(TimeSpan.FromSeconds(10)));
// A job of the same level waits too.
var drawing = await engine.EnqueueAsync(JobKind.Thumbs, [], priority: JobPriority.User);
await Task.Delay(400);
Assert.Equal(JobState.Running, (await db.Jobs.GetAsync(analysing.Id))!.State);
Assert.Equal((0, 0), (Volatile.Read(ref deep.Started), Volatile.Read(ref thumbs.Started)));
Assert.False(await engine.ReviewAsync(context.Cancellation));
release.Release(3);
foreach (var job in jobs) await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.Throws<ArgumentOutOfRangeException>(() => engine.SetWorkers(0));
// Another deep analysis, not its own, interrupts it.
var other = await engine.EnqueueAsync(JobKind.DeepAnalyse, [Guid.NewGuid()], priority: JobPriority.User);
await UntilAsync(() => Volatile.Read(ref deep.Running) == 1, "a higher job runs");
Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(analysing.Id))!.State);
release.Release(20);
foreach (var id in new[] { analysing.Id, followUp.Id, drawing.Id, other.Id }) await WaitForStateAsync(db, id, JobState.Done, TimeSpan.FromSeconds(10));
}
[Test]
public async Task RequeueingAYieldedJobLeavesPausesAndOtherNodesAlone()
{
await using var db = await OpenAsync();
var job = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Verify, State = JobState.Queued, Created = DateTimeOffset.UtcNow };
await db.Jobs.EnqueueAsync(job);
Assert.NotNull(await db.Jobs.ClaimNextAsync("pc"));
Assert.False(await db.Jobs.RequeueAsync(job.Id, "other"));
await db.Jobs.SetStateAsync(job.Id, JobState.Paused);
Assert.False(await db.Jobs.RequeueAsync(job.Id, "pc"));
Assert.Equal(JobState.Paused, (await db.Jobs.GetAsync(job.Id))!.State);
await db.Jobs.SetStateAsync(job.Id, JobState.Queued);
Assert.NotNull(await db.Jobs.ClaimNextAsync("pc"));
Assert.True(await db.Jobs.RequeueAsync(job.Id, "pc"));
Assert.Equal((JobState.Queued, (string?)null), ((await db.Jobs.GetAsync(job.Id))!.State, (await db.Jobs.GetAsync(job.Id))!.NodeId));
}
[Test]