refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s

The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #6.
This commit is contained in:
Alby96andClaude Opus 5.5 authored and Alby96 committed 2026-10-23 12:45:18 +02:00
1 parent c99842cc86
commit f2b58522b9
1801 files changed
+11804 -10676

No files matched your search

@@ -0,0 +1,449 @@
using Chronos.Core.Model;
using Chronos.Data.Jobs;
using Chronos.Testing;
namespace Chronos.Data.Tests.Jobs;
[Category("jobs")]
public sealed class JobEngineTests(TestContext context)
{
private sealed class CountingHandler(JobKind kind, int steps, Func<int, CancellationToken, Task>? onStep = null) : IJobHandler
{
public JobKind Kind => kind;
public List<string?> SeenCheckpoints { get; } = [];
public int Completed;
public async Task RunAsync(JobContext c)
{
lock (SeenCheckpoints) SeenCheckpoints.Add(c.Checkpoint);
var start = c.Checkpoint is { } cp ? int.Parse(cp, System.Globalization.CultureInfo.InvariantCulture) : 0;
for (var i = start; i < steps; i++)
{
c.Cancellation.ThrowIfCancellationRequested();
if (onStep is not null) await onStep(i, c.Cancellation);
await c.CheckpointAsync((i + 1).ToString(System.Globalization.CultureInfo.InvariantCulture), i + 1, steps);
}
Interlocked.Increment(ref Completed);
}
}
/// <summary>Takes only jobs whose parameters say they are its own.</summary>
private sealed class SelectiveHandler(JobKind kind) : ISelectiveJobHandler
{
public const string Mine = """{"mine":true}""";
public JobKind Kind => kind;
public ValueTask<bool> AcceptsAsync(Job job, CancellationToken cancellationToken) => ValueTask.FromResult(job.ParametersJson == Mine);
public Task RunAsync(JobContext c) => Task.CompletedTask;
}
/// <summary>Cannot judge jobs without parameters (as a render handler with a malformed preset) and fails them when run.</summary>
private sealed class FragileHandler(JobKind kind) : ISelectiveJobHandler
{
public JobKind Kind => kind;
public ValueTask<bool> AcceptsAsync(Job job, CancellationToken cancellationToken) =>
job.ParametersJson?.Length > 0 ? ValueTask.FromResult(true) : throw new ArgumentNullException(nameof(job));
public Task RunAsync(JobContext c) =>
c.Job.ParametersJson is null ? throw new InvalidDataException("no parameters") : Task.CompletedTask;
}
private sealed class CallbackHandler(JobKind kind, Func<JobContext, Task> run) : IJobHandler
{
public JobKind Kind => kind;
public Task RunAsync(JobContext context) => run(context);
}
private async Task<ChronosDatabase> OpenAsync() =>
await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory(), "jobs.db")), context.Cancellation);
private static async Task<Job> WaitForStateAsync(ChronosDatabase db, Guid id, JobState state, TimeSpan timeout)
{
var until = DateTime.UtcNow + timeout;
while (DateTime.UtcNow < until)
{
var job = await db.Jobs.GetAsync(id);
if (job?.State == state) return job;
await Task.Delay(20);
}
Assert.Fail($"Job {id} did not reach {state}");
return null!;
}
[Test]
public async Task RunsQueuedJobsToCompletion()
{
await using var db = await OpenAsync();
var handler = new CountingHandler(JobKind.Verify, 5);
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 2, WorkerId = "t", PollInterval = TimeSpan.FromMilliseconds(100) });
var progress = 0;
engine.Progress += _ => Interlocked.Increment(ref progress);
await engine.StartAsync();
var a = await engine.EnqueueAsync(JobKind.Verify, [Guid.NewGuid()], "A");
var b = await engine.EnqueueAsync(JobKind.Verify, [Guid.NewGuid()], "B");
var doneA = await WaitForStateAsync(db, a.Id, JobState.Done, TimeSpan.FromSeconds(10));
await WaitForStateAsync(db, b.Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.Equal(2, handler.Completed);
Assert.Equal(5L, doneA.ProgressDone);
Assert.Greater(progress, 0);
}
[Test]
public async Task CancelStopsARunningJob()
{
await using var db = await OpenAsync();
var started = new TaskCompletionSource();
var handler = new CountingHandler(JobKind.Scan, 1000, async (i, ct) =>
{
if (i == 0) started.TrySetResult();
await Task.Delay(10, ct);
});
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "t" });
await engine.StartAsync();
var job = await engine.EnqueueAsync(JobKind.Scan, []);
await started.Task.WaitAsync(TimeSpan.FromSeconds(10));
await engine.CancelAsync(job.Id);
await WaitForStateAsync(db, job.Id, JobState.Cancelled, TimeSpan.FromSeconds(5));
Assert.Equal(0, handler.Completed);
}
[Test]
public async Task PauseAndResumeContinueFromCheckpoint()
{
await using var db = await OpenAsync();
var gate = new SemaphoreSlim(0);
var handler = new CountingHandler(JobKind.Proxies, 20, async (i, ct) =>
{
if (i == 10) await gate.WaitAsync(ct);
});
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "t", PollInterval = TimeSpan.FromMilliseconds(100) });
await engine.StartAsync();
var job = await engine.EnqueueAsync(JobKind.Proxies, []);
var until = DateTime.UtcNow.AddSeconds(10);
while ((await db.Jobs.GetAsync(job.Id))!.Checkpoint != "10" && DateTime.UtcNow < until) await Task.Delay(20);
await engine.PauseAsync(job.Id);
var paused = await WaitForStateAsync(db, job.Id, JobState.Paused, TimeSpan.FromSeconds(5));
Assert.Equal("10", paused.Checkpoint);
gate.Release(10);
await engine.ResumeAsync(job.Id);
await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.Equal("10", handler.SeenCheckpoints[^1]);
}
[Test]
public async Task CrashedJobsResumeAfterRestart()
{
await using var db = await OpenAsync();
var job = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Thumbs, State = JobState.Queued, Created = DateTimeOffset.UtcNow };
await db.Jobs.EnqueueAsync(job);
var claimed = await db.Jobs.ClaimNextAsync("t");
Assert.NotNull(claimed);
await db.Jobs.UpdateProgressAsync(job.Id, 7, 12, null, null, "7", null);
// Simulated crash: the job stays "running" for worker t. A new engine with the same id requeues it.
var handler = new CountingHandler(JobKind.Thumbs, 12);
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "t" });
await engine.StartAsync();
await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.Equal("7", handler.SeenCheckpoints.Single());
}
[Test]
public async Task JobsOfNumberedWorkersResumeAfterRestart()
{
await using var db = await OpenAsync();
var job = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Thumbs, State = JobState.Queued, Created = DateTimeOffset.UtcNow };
await db.Jobs.EnqueueAsync(job);
Assert.NotNull(await db.Jobs.ClaimNextAsync("pc#1"));
var other = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Thumbs, State = JobState.Queued, Created = DateTimeOffset.UtcNow };
await db.Jobs.EnqueueAsync(other);
Assert.NotNull(await db.Jobs.ClaimNextAsync("pc2#0"));
// Restarting node "pc" (two workers: pc#0, pc#1) requeues pc#1's job, never another node's.
var handler = new CountingHandler(JobKind.Thumbs, 3);
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 2, WorkerId = "pc" });
await engine.StartAsync();
await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.Equal(JobState.Running, (await db.Jobs.GetAsync(other.Id))!.State);
}
[Test]
public async Task OnlyKindsWithAHandlerAreClaimed()
{
await using var db = await OpenAsync();
await using (var idle = new JobEngine(db, [], new JobEngineOptions { Workers = 1, WorkerId = "idle" }))
{
await idle.StartAsync();
var render = await idle.EnqueueAsync(JobKind.Render, []);
await Task.Delay(300);
Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(render.Id))!.State);
}
var handler = new CountingHandler(JobKind.Thumbs, 1);
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "t", Kinds = [JobKind.Thumbs, JobKind.Render] });
await engine.StartAsync();
var thumbs = await engine.EnqueueAsync(JobKind.Thumbs, []);
await WaitForStateAsync(db, thumbs.Id, JobState.Done, TimeSpan.FromSeconds(10));
var pending = (await db.Jobs.ListAsync(JobState.Queued)).Items;
Assert.Count(1, pending);
Assert.Equal(JobKind.Render, pending[0].Kind);
}
[Test]
public async Task SelectiveHandlersLeaveDeclinedJobsQueuedWithoutHoldingUpOthers()
{
await using var db = await OpenAsync();
await using var engine = new JobEngine(db, [new SelectiveHandler(JobKind.Render), new CountingHandler(JobKind.Thumbs, 1)],
new JobEngineOptions { Workers = 1, WorkerId = "t", PollInterval = TimeSpan.FromMilliseconds(50) });
var declined = await engine.EnqueueAsync(JobKind.Render, [], parametersJson: """{"mine":false}""", priority: JobPriority.User);
var thumbs = await engine.EnqueueAsync(JobKind.Thumbs, [], priority: JobPriority.Background);
var accepted = await engine.EnqueueAsync(JobKind.Render, [], parametersJson: SelectiveHandler.Mine, priority: JobPriority.User);
await engine.StartAsync();
var render = await WaitForStateAsync(db, accepted.Id, JobState.Done, TimeSpan.FromSeconds(10));
var other = await WaitForStateAsync(db, thumbs.Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.True(render.Started <= other.Started, "claim order: priority first, across selective and other kinds");
await Task.Delay(200);
var waiting = (await db.Jobs.GetAsync(declined.Id))!;
Assert.Equal((JobState.Queued, (string?)null), (waiting.State, waiting.NodeId));
}
[Test]
public async Task ASelectiveHandlerThatThrowsDoesNotStopTheWorkers()
{
await using var db = await OpenAsync();
await using var engine = new JobEngine(db, [new FragileHandler(JobKind.Render), new CountingHandler(JobKind.Thumbs, 1)],
new JobEngineOptions { Workers = 2, WorkerId = "t", PollInterval = TimeSpan.FromMilliseconds(50) });
var broken = await engine.EnqueueAsync(JobKind.Render, [], priority: JobPriority.User);
await engine.StartAsync();
var failed = await WaitForStateAsync(db, broken.Id, JobState.Failed, TimeSpan.FromSeconds(10));
Assert.Equal("no parameters", failed.Error);
// The workers are still there for every kind.
var thumbs = await engine.EnqueueAsync(JobKind.Thumbs, []);
var render = await engine.EnqueueAsync(JobKind.Render, [], parametersJson: "{}");
await WaitForStateAsync(db, thumbs.Id, JobState.Done, TimeSpan.FromSeconds(10));
await WaitForStateAsync(db, render.Id, JobState.Done, TimeSpan.FromSeconds(10));
}
[Test]
public async Task FollowUpsAreQueuedOnlyForKindsTheEngineRuns()
{
await using var db = await OpenAsync();
var target = Guid.CreateVersion7();
Job? thumbs = null, light = null;
var scan = new CallbackHandler(JobKind.Scan, async c =>
{
thumbs = await c.EnqueueFollowUpAsync(JobKind.Thumbs, [target], "Roof", null, JobPriority.Background);
light = await c.EnqueueFollowUpAsync(JobKind.LightAnalyse, [target], "Roof", null, JobPriority.Background);
});
await using var engine = new JobEngine(db, [scan, new CountingHandler(JobKind.Thumbs, 1)],
new JobEngineOptions { Workers = 1, WorkerId = "t", PollInterval = TimeSpan.FromMilliseconds(50) });
await engine.StartAsync();
var user = Guid.CreateVersion7();
var job = await engine.EnqueueAsync(JobKind.Scan, [], createdBy: user);
await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.Null(light);
var followUp = await WaitForStateAsync(db, Assert.NotNull(thumbs).Id, JobState.Done, TimeSpan.FromSeconds(10));
Assert.Equal((user, JobPriority.Background, "Roof"), (followUp.CreatedBy!.Value, followUp.Priority, followUp.TargetLabel!));
Assert.SequenceEqual([target], followUp.TargetIds);
Assert.Empty((await db.Jobs.ListAsync(null, JobKind.LightAnalyse)).Items);
}
/// <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)
{
var until = DateTime.UtcNow.AddSeconds(10);
while (!condition())
{
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);
}
}
}
[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, 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);
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(Chronos.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(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);
}
[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));
// 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]
public async Task HandlerFailureMarksJobFailedWithMessage()
{
await using var db = await OpenAsync();
var handler = new CountingHandler(JobKind.Render, 3, (i, _) => i == 1 ? throw new InvalidOperationException("encoder lost") : Task.CompletedTask);
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "t" });
await engine.StartAsync();
var job = await engine.EnqueueAsync(JobKind.Render, []);
var failed = await WaitForStateAsync(db, job.Id, JobState.Failed, TimeSpan.FromSeconds(10));
Assert.Equal("encoder lost", failed.Error);
Assert.Contains("encoder lost", failed.LogTail);
}
}
@@ -0,0 +1,106 @@
using Chronos.Core.Model;
using Chronos.Data.Jobs;
using Chronos.Testing;
namespace Chronos.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),
};
}
@@ -0,0 +1,147 @@
using Chronos.Core.Model;
using Chronos.Data.Jobs;
using Chronos.Protocol;
using Chronos.Testing;
namespace Chronos.Data.Tests.Jobs;
/// <summary>A clock the test moves by hand (BCL <see cref="TimeProvider"/>, no package).</summary>
internal sealed class ManualTime : TimeProvider
{
private long _ticks;
public override long TimestampFrequency => TimeSpan.TicksPerSecond;
public override long GetTimestamp() => Interlocked.Read(ref _ticks);
public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks);
}
[Category("jobs")]
public sealed class JobThroughputTests(TestContext context)
{
[Test]
public void TheFirstWindowWithProgressSeedsTheRate()
{
var throughput = new JobThroughput(0);
Assert.Null(throughput.Sample(2, 0, 100).Rate);
var (rate, eta) = throughput.Sample(4, 10, 100);
Assert.Equal(5.0, rate);
Assert.Equal(TimeSpan.FromSeconds(18), eta);
}
[Test]
public void AStalledJobLosesItsRateAndTimeLeftWithoutOverflowing()
{
// The owner's log: one sequence of 17 done, then a long one checkpointed every 10 s without moving the count.
var throughput = new JobThroughput(0);
throughput.Sample(600, 1, 17);
(double? Rate, TimeSpan? Eta) last = default;
for (var i = 1; i <= 2000; i++) last = throughput.Sample(600 + i * 10, 1, 17);
Assert.Null(last.Rate);
Assert.Null(last.Eta);
}
[Test]
public void ProgressThatGoesBackStartsANewWindow()
{
var throughput = new JobThroughput(0);
throughput.Sample(2, 20, 100);
throughput.Sample(3, 5, 100);
var (rate, _) = throughput.Sample(5, 15, 100);
Assert.Approximately(0.7 * 10 + 0.3 * 5, rate!.Value, 1e-9);
}
[Theory]
[Case(double.NaN)]
[Case(double.PositiveInfinity)]
[Case(-1.0)]
[Case(1e300)]
[Case(4e7)]
public void UnusableSecondsGiveNoTimeLeft(double seconds) => Assert.Null(JobEta.FromSeconds(seconds));
[Test]
public void UsableSecondsAndRatesGiveTheTimeLeft()
{
Assert.Equal(TimeSpan.FromSeconds(90), JobEta.FromSeconds(90));
Assert.Equal(TimeSpan.FromSeconds(4), JobEta.Of(8, 2));
Assert.Null(JobEta.Of(0, 2));
Assert.Null(JobEta.Of(8, null));
Assert.Null(JobEta.Of(8, 1e-300));
}
[Test]
public void ServerTimeLeftBeyondTheMaximumReadsUnknown()
{
var dto = new JobDto(Guid.NewGuid(), "render", [], null, "running", "normal", 1, 17, 1e-11, 1e300, null, null, 0, null,
DateTimeOffset.UtcNow, null, null);
Assert.Null(JobWire.FromDto(dto)!.Eta);
}
[Test]
public async Task AJobWhoseCountStallsForHoursStillEndsDone()
{
var time = new ManualTime();
var reported = new List<JobProgressInfo>();
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory(), "jobs.db")), context.Cancellation);
var handler = new CallbackHandler(JobKind.Proxies, async c =>
{
time.Advance(TimeSpan.FromSeconds(600));
await c.CheckpointAsync("1:0", 1, 17);
for (var frame = 1; frame <= 400; frame++)
{
time.Advance(TimeSpan.FromSeconds(11));
await c.CheckpointAsync($"1:{frame}", 1, 17);
}
});
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "t", Time = time });
engine.Progress += p =>
{
lock (reported) reported.Add(p);
};
await engine.StartAsync();
var job = await engine.EnqueueAsync(JobKind.Proxies, [Guid.NewGuid()]);
var done = await WaitForAsync(db, job.Id, JobState.Done);
Assert.Null(done.Eta);
Assert.Null(done.ItemsPerSecond);
lock (reported) Assert.Null(reported[^1].Eta);
}
[Test]
public async Task AProgressSubscriberThatThrowsNeverFailsTheJob()
{
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory(), "jobs.db")), context.Cancellation);
var handler = new CallbackHandler(JobKind.Verify, async c =>
{
for (var i = 1; i <= 5; i++) await c.CheckpointAsync(i.ToString(System.Globalization.CultureInfo.InvariantCulture), i, 5);
});
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "t" });
engine.Progress += _ => throw new InvalidOperationException("broken subscriber");
await engine.StartAsync();
var first = await engine.EnqueueAsync(JobKind.Verify, [Guid.NewGuid()]);
var second = await engine.EnqueueAsync(JobKind.Verify, [Guid.NewGuid()]);
Assert.Equal(5L, (await WaitForAsync(db, first.Id, JobState.Done)).ProgressDone);
await WaitForAsync(db, second.Id, JobState.Done);
}
private sealed class CallbackHandler(JobKind kind, Func<JobContext, Task> run) : IJobHandler
{
public JobKind Kind => kind;
public Task RunAsync(JobContext context) => run(context);
}
private static async Task<Job> WaitForAsync(ChronosDatabase db, Guid id, JobState state)
{
var until = DateTime.UtcNow.AddSeconds(15);
Job? job = null;
while (DateTime.UtcNow < until)
{
job = await db.Jobs.GetAsync(id);
if (job?.State == state) return job;
await Task.Delay(20);
}
Assert.Fail($"Job {id} is {job?.State}, not {state}");
return null!;
}
}