Files
Chronos/tests/Titano.Data.Tests/Jobs/JobPriorityQueueTests.cs
T
Alby96andClaude Opus 5.5 43b6871cdc 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>
2026-10-16 14:56:24 +02:00

107 lines
5.0 KiB
C#

using Titano.Core.Model;
using Titano.Data.Jobs;
using Titano.Testing;
namespace Titano.Data.Tests.Jobs;
/// <summary>The priority queue of the settings (owner review 1, R2): levels per kind of job, the claim order and what interrupts.</summary>
[Category("jobs")]
public sealed class JobPriorityQueueTests
{
private static readonly DateTimeOffset T0 = new(2026, 10, 16, 9, 0, 0, TimeSpan.Zero);
[Test]
public void RendersImportsAndDownloadsComeFirstAndTheLongResumablePassesLast()
{
var queue = new JobPriorityQueue();
Assert.Equal(JobKindPriority.High, queue.LevelOf(JobKind.Render));
Assert.Equal(JobKindPriority.High, queue.LevelOf(JobKind.Import));
Assert.Equal(JobKindPriority.High, queue.LevelOf(JobKind.Download));
Assert.Equal(JobKindPriority.Low, queue.LevelOf(JobKind.Verify));
Assert.Equal(JobKindPriority.Low, queue.LevelOf(JobKind.Proxies));
foreach (var kind in new[] { JobKind.Scan, JobKind.Thumbs, JobKind.LightAnalyse, JobKind.DeepAnalyse, JobKind.Maintenance })
Assert.Equal(JobKindPriority.Normal, queue.LevelOf(kind));
// A level the user set wins over the default.
Assert.Equal(JobKindPriority.Low, (queue with { Levels = new Dictionary<JobKind, JobKindPriority> { [JobKind.Render] = JobKindPriority.Low } }).LevelOf(JobKind.Render));
}
[Test]
public void OnlyAHigherLevelInterruptsAndOnlyWhileTheQueueIsOn()
{
var on = new JobPriorityQueue { Enabled = true };
Assert.True(on.Interrupts(JobKind.Render, JobKind.Scan));
Assert.True(on.Interrupts(JobKind.Scan, JobKind.Verify));
Assert.False(on.Interrupts(JobKind.Scan, JobKind.Render));
Assert.False(on.Interrupts(JobKind.Thumbs, JobKind.Scan));
Assert.False(on.Interrupts(JobKind.Import, JobKind.Render));
Assert.False(JobPriorityQueue.Off.Interrupts(JobKind.Render, JobKind.Verify));
}
[Test]
public void TheQueueOrdersByLevelThenPriorityThenAge()
{
var on = new JobPriorityQueue { Enabled = true };
var oldVerify = Job(JobKind.Verify, JobPriority.User, -30);
var backgroundScan = Job(JobKind.Scan, JobPriority.Background, -20);
var userThumbs = Job(JobKind.Thumbs, JobPriority.User, -10);
var render = Job(JobKind.Render, JobPriority.Normal, 0);
Job[] jobs = [oldVerify, backgroundScan, userThumbs, render];
Assert.SequenceEqual([render, userThumbs, backgroundScan, oldVerify], Sorted(jobs, on));
// Off, the strict queue of before: priority, then age.
Assert.SequenceEqual([oldVerify, userThumbs, render, backgroundScan], Sorted(jobs, JobPriorityQueue.Off));
// Two jobs alike fall back to the id.
var twin = render with { Id = Guid.CreateVersion7() };
Assert.True(on.ClaimsBefore(render, twin) != on.ClaimsBefore(twin, render));
}
[Test]
public void SettingsKeepOnlyTheLevelsThatDifferAndSkipWhatTheyCannotRead()
{
var queue = JobPriorityQueue.From(true, new Dictionary<string, string>
{
["deep-analyse"] = "high",
["Scan"] = "LOW",
["render"] = "high",
["teleport"] = "high",
["verify"] = "urgent",
["99"] = "low",
});
Assert.True(queue.Enabled);
Assert.Equal((JobKindPriority.High, JobKindPriority.Low, JobKindPriority.Low), (queue.LevelOf(JobKind.DeepAnalyse), queue.LevelOf(JobKind.Scan), queue.LevelOf(JobKind.Verify)));
var stored = JobPriorityQueue.Changed(Enum.GetValues<JobKind>().ToDictionary(k => k, queue.LevelOf));
Assert.SequenceEqual(["deep-analyse", "scan"], stored.Keys.Order(StringComparer.Ordinal));
Assert.Equal(("high", "low"), (stored["deep-analyse"], stored["scan"]));
var reread = JobPriorityQueue.From(true, stored);
foreach (var kind in Enum.GetValues<JobKind>()) Assert.Equal(queue.LevelOf(kind), reread.LevelOf(kind));
Assert.Equal(JobKindPriority.High, JobPriorityQueue.From(false, stored).LevelOf(JobKind.DeepAnalyse));
Assert.False(JobPriorityQueue.From(false, null).Enabled);
}
[Test]
public void TheClaimOrderOfTheDatabaseListsOnlyTheLevelsOtherThanNormal()
{
var on = new JobPriorityQueue { Enabled = true };
Assert.Equal(string.Empty, JobPriorityQueue.Off.OrderSql([JobKind.Render, JobKind.Verify]));
Assert.Equal(string.Empty, on.OrderSql([JobKind.Scan, JobKind.Thumbs]));
Assert.Equal("CASE WHEN kind IN (7) THEN 2 WHEN kind IN (2) THEN 0 ELSE 1 END DESC, ", on.OrderSql([JobKind.Render, JobKind.Verify, JobKind.Scan, JobKind.Render]));
}
private static Job[] Sorted(Job[] jobs, JobPriorityQueue queue)
{
var list = jobs.ToList();
list.Sort((a, b) => a.Id == b.Id ? 0 : queue.ClaimsBefore(a, b) ? -1 : 1);
return [.. list];
}
private static Job Job(JobKind kind, JobPriority priority, int minutes) => new()
{
Id = Guid.CreateVersion7(),
Kind = kind,
Priority = priority,
State = JobState.Queued,
Created = T0.AddMinutes(minutes),
};
}