Files
Chronos/tests/Titano.Server.Tests/Jobs/JobApiTests.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

140 lines
7.4 KiB
C#

using System.Net;
using System.Net.Http.Json;
using Microsoft.Extensions.DependencyInjection;
using Titano.Core.Model;
using Titano.Data.Jobs;
using Titano.Protocol;
using Titano.Testing;
namespace Titano.Server.Tests.Jobs;
[Category("server")]
public sealed class JobApiTests(TestContext context)
{
[Test]
public async Task JobsMoveThroughPauseResumeAndDone()
{
var handler = new BlockingJobHandler(JobKind.DeepAnalyse);
await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton<IJobHandler>(handler));
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var sequence = await CreateSequenceAsync(editor);
var job = await CreateAsync(editor, "deep-analyse", sequence);
Assert.Equal(("queued", "user", "Harbour sunset"), (job.State, job.Priority, job.TargetLabel));
await handler.Started.Task.WaitAsync(TimeSpan.FromSeconds(10));
await WaitForAsync(editor, job.Id, "running");
Assert.Equal("paused", (await ActAsync(editor, job.Id, "pause", HttpStatusCode.OK)).State);
await WaitForAsync(editor, job.Id, "paused");
await ActAsync(editor, job.Id, "pause", HttpStatusCode.Conflict);
await ActAsync(editor, job.Id, "retry", HttpStatusCode.Conflict);
await ActAsync(editor, job.Id, "resume", HttpStatusCode.OK);
await WaitForAsync(editor, job.Id, "running");
handler.Release();
var done = await WaitForAsync(editor, job.Id, "done");
Assert.Equal((4L, 4L), (done.ProgressDone, done.ProgressTotal));
await ActAsync(editor, job.Id, "cancel", HttpStatusCode.Conflict);
using var list = await editor.GetAsync("/api/v1/jobs?state=done&kind=deep-analyse");
Assert.Equal(job.Id, (await list.ReadAsync(ProtocolJson.Default.PageJobDto)).Items.Single().Id);
}
[Test]
public async Task FailedJobsRetryAndRunningJobsCancel()
{
var handler = new BlockingJobHandler(JobKind.Proxies) { FailWith = "decoder exploded" };
await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton<IJobHandler>(handler));
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var sequence = await CreateSequenceAsync(editor);
var job = await CreateAsync(editor, "proxies", sequence);
var failed = await WaitForAsync(editor, job.Id, "failed");
Assert.Equal("decoder exploded", failed.Error);
Assert.Contains("decoder exploded", failed.LogTail);
await ActAsync(editor, job.Id, "retry", HttpStatusCode.OK);
await WaitForAsync(editor, job.Id, "running");
Assert.Equal("cancelled", (await ActAsync(editor, job.Id, "cancel", HttpStatusCode.OK)).State);
await Task.Delay(200);
Assert.Equal("cancelled", (await GetAsync(editor, job.Id)).State);
}
[Test]
public async Task WaitingJobsChangePriorityAndRightsAreChecked()
{
var handler = new BlockingJobHandler(JobKind.Thumbs);
await using var server = await TestServer.StartAsync(context, o => o.Parallelism = 1, s => s.AddSingleton<IJobHandler>(handler));
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
var sequence = await CreateSequenceAsync(editor);
var first = await CreateAsync(editor, "thumbs", sequence);
await WaitForAsync(editor, first.Id, "running");
var second = await CreateAsync(editor, "thumbs", sequence);
using (var lower = await editor.PostAsJsonAsync($"/api/v1/jobs/{second.Id}/priority", new JobPriorityRequest("background"), ProtocolJson.Default.JobPriorityRequest))
Assert.Equal("background", (await lower.ReadAsync(ProtocolJson.Default.JobDto)).Priority);
using (var bad = await editor.PostAsJsonAsync($"/api/v1/jobs/{second.Id}/priority", new JobPriorityRequest("urgent"), ProtocolJson.Default.JobPriorityRequest))
await bad.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_priority");
using (var foreign = await viewer.PostAsync($"/api/v1/jobs/{second.Id}/cancel", null)) await foreign.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
using (var create = await viewer.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("thumbs", [sequence], null), ProtocolJson.Default.CreateJobRequest))
await create.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
using (var unknown = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("teleport", [sequence], null), ProtocolJson.Default.CreateJobRequest))
await unknown.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_job_kind");
using (var unavailable = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("deep-analyse", [sequence], null), ProtocolJson.Default.CreateJobRequest))
await unavailable.ExpectErrorAsync(HttpStatusCode.Conflict, "job_unavailable");
using (var render = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("render", [sequence], "{}"), ProtocolJson.Default.CreateJobRequest))
await render.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_parameters");
var admin = await server.AdminAsync();
using (var removed = await admin.PostAsync($"/api/v1/jobs/{second.Id}/cancel", null)) await removed.ExpectAsync(HttpStatusCode.OK);
handler.Release();
await WaitForAsync(editor, first.Id, "done");
}
public static async Task<Guid> CreateSequenceAsync(HttpClient client, string title = "Harbour sunset")
{
using var created = await client.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest(title, null), ProtocolJson.Default.CreateSequenceRequest);
await created.ExpectAsync(HttpStatusCode.Created);
return (await created.ReadAsync(ProtocolJson.Default.SequenceDto)).Id;
}
public static async Task<JobDto> CreateAsync(HttpClient client, string kind, Guid target, string? parameters = null)
{
using var response = await client.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest(kind, [target], parameters), ProtocolJson.Default.CreateJobRequest);
await response.ExpectAsync(HttpStatusCode.Accepted);
return await response.ReadAsync(ProtocolJson.Default.JobDto);
}
public static async Task<JobDto> GetAsync(HttpClient client, Guid id)
{
using var response = await client.GetAsync($"/api/v1/jobs/{id}");
return await response.ReadAsync(ProtocolJson.Default.JobDto);
}
public static async Task<JobDto> WaitForAsync(HttpClient client, Guid id, string state)
{
var deadline = DateTime.UtcNow.AddSeconds(15);
while (true)
{
var job = await GetAsync(client, id);
if (job.State == state) return job;
if (DateTime.UtcNow > deadline) Assert.Fail($"Job {id} is {job.State}, expected {state}");
await Task.Delay(50);
}
}
private static async Task<JobDto> ActAsync(HttpClient client, Guid id, string action, HttpStatusCode expected)
{
using var response = await client.PostAsync($"/api/v1/jobs/{id}/{action}", null);
if (expected != HttpStatusCode.OK)
{
await response.ExpectErrorAsync(expected, "invalid_state");
return await GetAsync(client, id);
}
await response.ExpectAsync(expected);
return await response.ReadAsync(ProtocolJson.Default.JobDto);
}
}