feat(jobs): progress detail contract and a 2/s publish throttle
Mass passes need to say what they do now (photo x of y, sequence n of m) without slowing down. This adds the contract and the engine side; handlers and displays follow in their own parcels. - Core: JobDetail (step, item, item x of y, part n of m with unit), JobStep and JobPartUnit appended-only and stored by kebab name; Job.Detail. - Protocol: JobDetailDto, appended as JobDto.Detail, JobProgressEvent.Detail and NodeProgress.Detail, so older peers ignore it and newer ones fall back. - Data: JobContext keeps a detail across reports (ReportAsync with a detail, ReportItemAsync without allocating, CheckpointAsync with a detail) and publishes at most once per ProgressInterval (500 ms on the engine's TimeProvider), at once for the first report, a new step or a completed count; a dropped report costs no allocation or lock, and the persist that falls due publishes what it saves. Persist stays at most every 5 s. JobWire maps steps, units and details; an unknown name reads as none. - Migration 0011 adds jobs.progress_detail (nullable JSON text); an unreadable cell reads as no detail. Lists return it. - Server: ProgressGate (500 ms per job, step-change and completion bypass) shared by the engine host and render nodes; node details are stored and broadcast, an older node without one still works. Refs #65 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
58ed737310
commit
ee9505df22
20 files changed
+968
-63
No files matched your search
@@ -1,4 +1,3 @@
|
||||
using System.Diagnostics;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Jobs;
|
||||
@@ -16,22 +15,20 @@ namespace Chronos.Server.Jobs;
|
||||
/// running one, in the order of the priority queue of the settings (owner review 1). Render jobs run here only when the registered render
|
||||
/// handler accepts them (<see cref="ISelectiveJobHandler"/>: managed formats, and MP4 jobs no render node can take);
|
||||
/// render nodes claim the others (BRIEF §14.5, §14.7). On shutdown running jobs stay marked running and are requeued from their checkpoints at the next start.
|
||||
/// Progress goes out through the <see cref="Gate"/> render nodes share, with what each job does now (owner review 5, #65).
|
||||
/// </summary>
|
||||
public sealed class JobEngineHost : IHostedService
|
||||
{
|
||||
private static readonly long ProgressInterval = Stopwatch.Frequency / 4;
|
||||
|
||||
private readonly ChronosDatabase _db;
|
||||
private readonly EventHub _events;
|
||||
private readonly HashSet<JobKind> _kinds;
|
||||
private readonly Dictionary<Guid, long> _lastProgress = [];
|
||||
private readonly Lock _gate = new();
|
||||
|
||||
public JobEngineHost(ChronosDatabase db, IEnumerable<IJobHandler> handlers, IEnumerable<IJobHandlerProvider> providers, ServerOptions options,
|
||||
EventHub events)
|
||||
EventHub events, TimeProvider time)
|
||||
{
|
||||
_db = db;
|
||||
_events = events;
|
||||
Gate = new ProgressGate(time);
|
||||
// A render handler must be selective: a plain one would take the jobs that need a render node's encoder.
|
||||
var local = handlers.Concat(providers.SelectMany(p => p.CreateJobHandlers())).Where(h => h.Kind != JobKind.Render || h is ISelectiveJobHandler).ToList();
|
||||
_kinds = [.. local.Select(h => h.Kind)];
|
||||
@@ -51,6 +48,9 @@ public sealed class JobEngineHost : IHostedService
|
||||
|
||||
public JobEngine Engine { get; }
|
||||
|
||||
/// <summary>The pace of every job's progress events, the server's own jobs and render nodes' alike.</summary>
|
||||
public ProgressGate Gate { get; }
|
||||
|
||||
/// <summary>True when a job of <paramref name="kind"/> has somewhere to run: a local handler, or render nodes for renders.</summary>
|
||||
public bool CanRun(JobKind kind) => kind == JobKind.Render || _kinds.Contains(kind);
|
||||
|
||||
@@ -63,21 +63,14 @@ public sealed class JobEngineHost : IHostedService
|
||||
|
||||
private void OnJobChanged(Job job)
|
||||
{
|
||||
if (job.State != JobState.Running)
|
||||
{
|
||||
lock (_gate) _lastProgress.Remove(job.Id);
|
||||
}
|
||||
if (job.State != JobState.Running) Gate.Forget(job.Id);
|
||||
_events.Publish(new JobStateEvent(job.Id, JobWire.State(job.State), job.Error));
|
||||
}
|
||||
|
||||
private void OnProgress(JobProgressInfo progress)
|
||||
{
|
||||
var now = Stopwatch.GetTimestamp();
|
||||
lock (_gate)
|
||||
{
|
||||
if (_lastProgress.TryGetValue(progress.JobId, out var last) && now - last < ProgressInterval && progress.Done < progress.Total) return;
|
||||
_lastProgress[progress.JobId] = now;
|
||||
}
|
||||
_events.Publish(new JobProgressEvent(progress.JobId, progress.Done, progress.Total, progress.ItemsPerSecond, progress.Eta?.TotalSeconds));
|
||||
if (!Gate.Passes(progress.JobId, progress.Done, progress.Total, progress.Detail?.Step ?? JobStep.None)) return;
|
||||
_events.Publish(new JobProgressEvent(progress.JobId, progress.Done, progress.Total, progress.ItemsPerSecond, progress.Eta?.TotalSeconds,
|
||||
JobWire.Detail(progress.Detail)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Chronos.Core.Model;
|
||||
|
||||
namespace Chronos.Server.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// How often a job's progress goes out on the event socket (owner review 5, #65): at most once per <see cref="Interval"/>
|
||||
/// per job, for the server's own jobs (<see cref="JobEngineHost"/>) and those render nodes run (<see cref="RenderNodeService"/>)
|
||||
/// alike. A job's first report, a new step and a count that completes always pass. A job that stops running is forgotten
|
||||
/// (<see cref="Forget"/>), so its next run starts afresh.
|
||||
/// </summary>
|
||||
public sealed class ProgressGate(TimeProvider time)
|
||||
{
|
||||
/// <summary>The shortest time between two progress events of one job: twice a second, as the job engine publishes.</summary>
|
||||
public static readonly TimeSpan Interval = TimeSpan.FromMilliseconds(500);
|
||||
|
||||
/// <summary>
|
||||
/// What a report may come early by and still pass: the engine spaces its reports by the same interval, measured a moment
|
||||
/// before this gate reads its own clock, and a report it sent on time must not wait for the next one.
|
||||
/// </summary>
|
||||
private static readonly TimeSpan Slack = TimeSpan.FromMilliseconds(50);
|
||||
|
||||
private readonly Dictionary<Guid, (long At, JobStep Step)> _last = [];
|
||||
private readonly Lock _gate = new();
|
||||
|
||||
/// <summary>True when the report (<paramref name="done"/> of <paramref name="total"/>, at <paramref name="step"/>) goes out now; it is then the job's last.</summary>
|
||||
public bool Passes(Guid jobId, long done, long total, JobStep step)
|
||||
{
|
||||
var now = time.GetTimestamp();
|
||||
lock (_gate)
|
||||
{
|
||||
if (_last.TryGetValue(jobId, out var last) && last.Step == step && !(total > 0 && done >= total)
|
||||
&& time.GetElapsedTime(last.At, now) < Interval - Slack)
|
||||
return false;
|
||||
_last[jobId] = (now, step);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Forgets a job that no longer runs (done, failed, paused, requeued).</summary>
|
||||
public void Forget(Guid jobId)
|
||||
{
|
||||
lock (_gate) _last.Remove(jobId);
|
||||
}
|
||||
|
||||
/// <summary>The jobs the gate remembers (tests).</summary>
|
||||
internal int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate) return _last.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -185,10 +185,14 @@ public sealed class RenderNodeService(
|
||||
var rate = progress.ItemsPerSecond is { } r && double.IsFinite(r) && r > 0 ? r : (double?)null;
|
||||
var eta = JobEta.Of(progress.Total - progress.Done, rate);
|
||||
var tail = AppendLog(job.LogTail, progress.LogLine);
|
||||
if (!await db.Jobs.UpdateNodeProgressAsync(jobId, NodeKey(node), progress.Done, progress.Total, rate, eta, tail, cancellationToken))
|
||||
// What the node does now (owner review 5, #65), read as this build knows it; an older node sends none, which clears it.
|
||||
var detail = JobWire.FromDto(progress.Detail);
|
||||
if (!await db.Jobs.UpdateNodeProgressAsync(jobId, NodeKey(node), progress.Done, progress.Total, rate, eta, tail, detail, cancellationToken))
|
||||
return Outcome<bool>.Fail(409, "job_not_assigned");
|
||||
await TouchAsync(node, cancellationToken);
|
||||
events.Publish(new JobProgressEvent(jobId, progress.Done, progress.Total, rate, eta?.TotalSeconds));
|
||||
// Every report is stored; the event socket gets them at the pace of the server's own jobs.
|
||||
if (jobs.Gate.Passes(jobId, progress.Done, progress.Total, detail?.Step ?? JobStep.None))
|
||||
events.Publish(new JobProgressEvent(jobId, progress.Done, progress.Total, rate, eta?.TotalSeconds, JobWire.Detail(detail)));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user