fix(jobs): claim only handled kinds and requeue numbered workers after a crash

An engine with no handlers (or a filter it could not serve) claimed every
job, so a host without a render handler would fail render-node work.
With several workers, jobs carry node ids such as pc#1, but crash
recovery only matched the bare node id and left them running forever.

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-02 12:49:15 +00:00
1 parent b4f61e29b9
commit 6ac27413a7
3 files changed
+52 -5

No files matched your search

+9 -2
View File
@@ -34,6 +34,7 @@ public sealed class JobEngine : IAsyncDisposable
{
private readonly TitanoDatabase _db;
private readonly Dictionary<JobKind, IJobHandler> _handlers;
private IReadOnlyCollection<JobKind> _kinds = [];
private readonly ConcurrentDictionary<Guid, CancellationTokenSource> _running = new();
private readonly Channel<bool> _wake = Channel.CreateBounded<bool>(new BoundedChannelOptions(1) { FullMode = BoundedChannelFullMode.DropWrite });
private readonly CancellationTokenSource _shutdown = new();
@@ -62,6 +63,13 @@ public sealed class JobEngine : IAsyncDisposable
var requeued = await _db.Jobs.RequeueRunningAsync(Options.WorkerId, cancellationToken).ConfigureAwait(false);
if (requeued > 0) Log.Info("jobs", $"Requeued {requeued} interrupted job(s)");
await _db.Jobs.PurgeHistoryAsync(DateTimeOffset.UtcNow - Options.HistoryRetention, cancellationToken).ConfigureAwait(false);
// Only kinds this engine can run: an empty filter would claim (and fail) every job, render-node work included.
_kinds = [.. (Options.Kinds ?? _handlers.Keys).Where(_handlers.ContainsKey).Distinct()];
if (_kinds.Count == 0)
{
Log.Info("jobs", "No job handlers; this engine claims nothing");
return;
}
for (var i = 0; i < Options.Workers; i++)
{
var id = Options.Workers == 1 ? Options.WorkerId : $"{Options.WorkerId}#{i}";
@@ -161,13 +169,12 @@ public sealed class JobEngine : IAsyncDisposable
private async Task WorkerLoopAsync(string workerId, CancellationToken shutdown)
{
var kinds = Options.Kinds ?? [.. _handlers.Keys];
while (!shutdown.IsCancellationRequested)
{
Job? job;
try
{
job = await _db.Jobs.ClaimNextAsync(workerId, kinds, shutdown).ConfigureAwait(false);
job = await _db.Jobs.ClaimNextAsync(workerId, _kinds, shutdown).ConfigureAwait(false);
}
catch (OperationCanceledException)
{