feat(server): render MP4 jobs no render node can take as MJPEG, and say so

BRIEF §14.7: the server falls back to managed formats when no node is
available and says so in the job log. An MP4 job waits for render nodes at
least the node timeout, then as long as an online node can encode its codec
at its size; after that the server's engine takes it and writes each output
it cannot encode as MJPEG in AVI at the same size and frame rate, with a job
log line. A job the server started resumes there. Selective acceptance is
asynchronous for that; when node availability cannot be read the job keeps
waiting.

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-03 09:39:17 +00:00
1 parent 1e255fb075
commit 4007293833
15 files changed
+247 -48

No files matched your search

+1
View File
@@ -16,6 +16,7 @@
<data name="Export.Job.Downloading" xml:space="preserve"><value>Downloading frames</value><comment>render node progress stage</comment></data> <data name="Export.Job.Downloading" xml:space="preserve"><value>Downloading frames</value><comment>render node progress stage</comment></data>
<data name="Export.Job.Preparing" xml:space="preserve"><value>Measuring frames</value><comment>job progress stage</comment></data> <data name="Export.Job.Preparing" xml:space="preserve"><value>Measuring frames</value><comment>job progress stage</comment></data>
<data name="Export.Log.Failed" xml:space="preserve"><value>{0} failed: {1}</value><comment>long: job log line; {0} preset name; {1} reason</comment></data> <data name="Export.Log.Failed" xml:space="preserve"><value>{0} failed: {1}</value><comment>long: job log line; {0} preset name; {1} reason</comment></data>
<data name="Export.Log.NoRenderNode" xml:space="preserve"><value>No render node can encode {1}; {0} is rendered as {2} instead.</value><comment>long: job log line; {0} preset name; {1} codec asked for; {2} managed codec used</comment></data>
<data name="Export.Log.Rendered" xml:space="preserve"><value>{0} saved as {1}</value><comment>long: job log line; {0} preset name; {1} output file name</comment></data> <data name="Export.Log.Rendered" xml:space="preserve"><value>{0} saved as {1}</value><comment>long: job log line; {0} preset name; {1} output file name</comment></data>
<data name="Export.Plan.FrameRateIgnored" xml:space="preserve"><value>Playback rate not understood; the export frame rate is used.</value><comment>long</comment></data> <data name="Export.Plan.FrameRateIgnored" xml:space="preserve"><value>Playback rate not understood; the export frame rate is used.</value><comment>long</comment></data>
<data name="Export.Plan.NoCaptureTimes" xml:space="preserve"><value>No capture times: frames are spaced evenly.</value><comment>long</comment></data> <data name="Export.Plan.NoCaptureTimes" xml:space="preserve"><value>No capture times: frames are spaced evenly.</value><comment>long</comment></data>
+1
View File
@@ -16,6 +16,7 @@
<data name="Export.Job.Downloading" xml:space="preserve"><value>Download dei fotogrammi</value></data> <data name="Export.Job.Downloading" xml:space="preserve"><value>Download dei fotogrammi</value></data>
<data name="Export.Job.Preparing" xml:space="preserve"><value>Misura dei fotogrammi</value></data> <data name="Export.Job.Preparing" xml:space="preserve"><value>Misura dei fotogrammi</value></data>
<data name="Export.Log.Failed" xml:space="preserve"><value>{0} non riuscito: {1}</value></data> <data name="Export.Log.Failed" xml:space="preserve"><value>{0} non riuscito: {1}</value></data>
<data name="Export.Log.NoRenderNode" xml:space="preserve"><value>Nessun nodo di rendering può codificare {1}; {0} viene esportato in {2}.</value></data>
<data name="Export.Log.Rendered" xml:space="preserve"><value>{0} salvato come {1}</value></data> <data name="Export.Log.Rendered" xml:space="preserve"><value>{0} salvato come {1}</value></data>
<data name="Export.Plan.FrameRateIgnored" xml:space="preserve"><value>Velocità di riproduzione non valida; viene usata la frequenza di esportazione.</value></data> <data name="Export.Plan.FrameRateIgnored" xml:space="preserve"><value>Velocità di riproduzione non valida; viene usata la frequenza di esportazione.</value></data>
<data name="Export.Plan.NoCaptureTimes" xml:space="preserve"><value>Nessun orario di scatto: fotogrammi distribuiti in modo uniforme.</value></data> <data name="Export.Plan.NoCaptureTimes" xml:space="preserve"><value>Nessun orario di scatto: fotogrammi distribuiti in modo uniforme.</value></data>
+4 -4
View File
@@ -242,7 +242,7 @@ public sealed class JobEngine : IAsyncDisposable
Job? candidate = null; Job? candidate = null;
foreach (var queued in await _db.Jobs.ListQueuedAsync(_selective.Keys, SelectiveWindow, cancellationToken).ConfigureAwait(false)) foreach (var queued in await _db.Jobs.ListQueuedAsync(_selective.Keys, SelectiveWindow, cancellationToken).ConfigureAwait(false))
{ {
if (!Accepts(queued)) continue; if (!await AcceptsAsync(queued, cancellationToken).ConfigureAwait(false)) continue;
candidate = queued; candidate = queued;
break; break;
} }
@@ -261,13 +261,13 @@ public sealed class JobEngine : IAsyncDisposable
} }
/// <summary>A job its handler cannot judge is taken, so it fails here with its reason instead of blocking the queue.</summary> /// <summary>A job its handler cannot judge is taken, so it fails here with its reason instead of blocking the queue.</summary>
private bool Accepts(Job job) private async ValueTask<bool> AcceptsAsync(Job job, CancellationToken cancellationToken)
{ {
try try
{ {
return _selective[job.Kind].Accepts(job); return await _selective[job.Kind].AcceptsAsync(job, cancellationToken).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex) when (!(ex is OperationCanceledException && cancellationToken.IsCancellationRequested))
{ {
Log.Warning("jobs", $"{job.Kind} job {job.Id} could not be examined; it is claimed so that it fails with its reason", ex); Log.Warning("jobs", $"{job.Kind} job {job.Id} could not be examined; it is claimed so that it fails with its reason", ex);
return true; return true;
+5 -4
View File
@@ -14,13 +14,14 @@ public interface IJobHandler
} }
/// <summary> /// <summary>
/// A handler that runs only some jobs of its kind, decided from the job alone (its parameters never change). The engine /// A handler that runs only some jobs of its kind. The engine leaves the others queued for other claimers and asks again
/// leaves the others queued for other claimers: the server renders what its managed encoders produce and leaves the /// at every claim, so an answer may change while the job waits: the server renders what its managed encoders produce,
/// rest to render nodes (BRIEF §14.5, §14.7). /// leaves the rest to render nodes, and takes those too in a managed format once no node can (BRIEF §14.5, §14.7).
/// </summary> /// </summary>
public interface ISelectiveJobHandler : IJobHandler public interface ISelectiveJobHandler : IJobHandler
{ {
bool Accepts(Job job); /// <summary>Whether this handler takes <paramref name="job"/> now. A job it cannot read should be taken, so it fails with its reason.</summary>
ValueTask<bool> AcceptsAsync(Job job, CancellationToken cancellationToken);
} }
/// <summary>A component that brings several handlers (the archive engine: scan, rebuild, verify, import, images, maintenance).</summary> /// <summary>A component that brings several handlers (the archive engine: scan, rebuild, verify, import, images, maintenance).</summary>
@@ -1,39 +1,87 @@
using Titano.Core.Diagnostics;
using Titano.Core.Export; using Titano.Core.Export;
using Titano.Core.Localization;
using Titano.Core.Model; using Titano.Core.Model;
using Titano.Core.Video; using Titano.Core.Video;
using Titano.Data.Jobs; using Titano.Data.Jobs;
using Titano.Protocol;
namespace Titano.Data.Processing.Jobs; namespace Titano.Data.Processing.Jobs;
/// <summary> /// <summary>
/// <see cref="JobKind.Render"/> on the server (BRIEF §4.4, §14.5): it takes only jobs whose every output (the preset and /// <see cref="JobKind.Render"/> on the server (BRIEF §4.4, §14.5, §14.7): it takes the jobs whose every output (the preset
/// each batch preset) its managed encoders produce, and runs them like a client does. Jobs that need a platform encoder /// and each batch preset) its managed encoders produce, and runs them like a client does. Jobs that need a platform
/// (MP4) stay queued for render nodes (§14.7). Jobs whose parameters cannot be read are taken, so they fail with their /// encoder (MP4) wait for render nodes while <see cref="ProcessingOptions.AwaitsRenderNode"/> says one may take them;
/// reason instead of waiting for a claimer that never comes. /// after that the server takes them too, writes each output it cannot encode as MJPEG in AVI at the same size and frame
/// rate, and says so in the job log. A job it started (it left a checkpoint) resumes here, and jobs whose parameters
/// cannot be read are taken, so they fail with their reason instead of waiting for a claimer that never comes.
/// </summary> /// </summary>
internal sealed class ManagedRenderJobHandler(RenderJobHandler render, IReadOnlyList<IVideoEncoderProvider> encoders) : ISelectiveJobHandler internal sealed class ManagedRenderJobHandler(RenderJobHandler render, ProcessingOptions options) : ISelectiveJobHandler
{ {
private readonly EncoderCapability[] _capabilities = [.. encoders.SelectMany(e => e.Capabilities)]; /// <summary>The managed preset whose values stand in for an output only a render node could make.</summary>
private const string FallbackPresetId = "mjpeg-avi";
private readonly EncoderCapability[] _capabilities = [.. options.Encoders.SelectMany(e => e.Capabilities)];
public JobKind Kind => JobKind.Render; public JobKind Kind => JobKind.Render;
public bool Accepts(Job job) public async ValueTask<bool> AcceptsAsync(Job job, CancellationToken cancellationToken)
{ {
ArgumentNullException.ThrowIfNull(job); ArgumentNullException.ThrowIfNull(job);
RenderJobParameters parameters;
try try
{ {
var parameters = ProcessingJobs.ReadParameters(job.ParametersJson); parameters = ProcessingJobs.ReadParameters(job.ParametersJson);
// Most declined jobs are decided by the main codec, before the export is read. // Most jobs are decided by the main codec, before the export is read.
return _capabilities.Any(c => string.Equals(ProcessingJobs.CodecName(c.Codec), parameters.Codec, StringComparison.OrdinalIgnoreCase)) if (Produces(parameters.Codec) && RenderJobHandler.Targets(ProcessingJobs.ReadExport(parameters.PresetJson), parameters.PresetName).All(t => Produces(t.Export)))
&& RenderJobHandler.Targets(ProcessingJobs.ReadExport(parameters.PresetJson), parameters.PresetName).All(t => Produces(t.Export)); return true;
} }
catch (InvalidDataException) catch (Exception ex) when (ex is not OperationCanceledException)
{ {
return true; return true;
} }
if (job.Checkpoint is not null) return true;
if (options.AwaitsRenderNode is not { } awaits) return false;
try
{
return !await awaits(job, parameters, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Unknown whether a node could take it: the job keeps waiting rather than losing its format.
Log.Warning("processing", $"Render job {job.Id}: render node availability unknown", ex);
return false;
}
} }
public Task RunAsync(JobContext context) => render.RunAsync(context); public Task RunAsync(JobContext context) =>
render.RunAsync(context, target => Produces(target.Export) ? target : Fallback(context, target));
/// <summary>The output as MJPEG in AVI with the managed preset's quality, at the size and frame rate asked for.</summary>
private static ExportTarget Fallback(JobContext context, ExportTarget target)
{
var mjpeg = ExportResolver.FromPreset(ExportPresets.Find(FallbackPresetId)!);
context.Log(Loc.L("Export.Log.NoRenderNode", target.PresetName, ExportNaming.CodecLabel(target.Export.Codec), ExportNaming.CodecLabel(mjpeg.Codec)));
return target with
{
Export = target.Export with
{
PresetId = ExportResolver.CustomId,
NameKey = null,
Codec = mjpeg.Codec,
Profile = mjpeg.Profile,
BitDepth = mjpeg.BitDepth,
BitRate = 0,
BitsPerPixel = 0,
Quality = mjpeg.Quality,
RateControl = mjpeg.RateControl,
Encoder = null,
},
};
}
private bool Produces(string codec) =>
_capabilities.Any(c => string.Equals(ProcessingJobs.CodecName(c.Codec), codec, StringComparison.OrdinalIgnoreCase));
private bool Produces(ResolvedExport export) => private bool Produces(ResolvedExport export) =>
_capabilities.Any(c => c.Codec == export.Codec && export.Width <= c.MaxWidth && export.Height <= c.MaxHeight); _capabilities.Any(c => c.Codec == export.Codec && export.Width <= c.MaxWidth && export.Height <= c.MaxHeight);
@@ -25,7 +25,10 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti
{ {
public JobKind Kind => JobKind.Render; public JobKind Kind => JobKind.Render;
public async Task RunAsync(JobContext context) public Task RunAsync(JobContext context) => RunAsync(context, target => target);
/// <summary>Runs the job with each output still to render passed through <paramref name="adapt"/> (the server's managed stand-ins).</summary>
internal async Task RunAsync(JobContext context, Func<ExportTarget, ExportTarget> adapt)
{ {
var cancellation = context.Cancellation; var cancellation = context.Cancellation;
var (parameters, export) = ProcessingJobs.ReadRender(context.Job.ParametersJson); var (parameters, export) = ProcessingJobs.ReadRender(context.Job.ParametersJson);
@@ -56,7 +59,7 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti
Discard(output, keepOutput: recorded.Any(r => r.OutputPath == Stored(paths, output))); Discard(output, keepOutput: recorded.Any(r => r.OutputPath == Stored(paths, output)));
names.Release(output); // free again for the entry's new attempt names.Release(output); // free again for the entry's new attempt
} }
var targets = all.Select((t, i) => (i, t)).Where(t => !done.Contains(t.i)).ToList(); var targets = all.Select((t, i) => (i, t)).Where(t => !done.Contains(t.i)).Select(t => (t.i, adapt(t.t))).ToList();
if (targets.Count == 0) return; if (targets.Count == 0) return;
var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, cancellation).ConfigureAwait(false); var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, cancellation).ConfigureAwait(false);
@@ -1,10 +1,12 @@
using Titano.Core.Analysis.Deep; using Titano.Core.Analysis.Deep;
using Titano.Core.Export; using Titano.Core.Export;
using Titano.Core.Imaging; using Titano.Core.Imaging;
using Titano.Core.Model;
using Titano.Core.Render; using Titano.Core.Render;
using Titano.Core.Video; using Titano.Core.Video;
using Titano.Core.Video.Managed; using Titano.Core.Video.Managed;
using Titano.Data.Archiving; using Titano.Data.Archiving;
using Titano.Protocol;
namespace Titano.Data.Processing; namespace Titano.Data.Processing;
@@ -40,9 +42,16 @@ public sealed record ProcessingOptions
/// </summary> /// </summary>
public Func<Guid, string, CancellationToken, Task>? SequenceChanged { get; init; } public Func<Guid, string, CancellationToken, Task>? SequenceChanged { get; init; }
/// <summary>The server delegates platform codecs to render nodes and never runs them itself.</summary> /// <summary>The server delegates platform codecs to render nodes (<see cref="AwaitsRenderNode"/>).</summary>
public EncoderHost Host { get; init; } = EncoderHost.Client; public EncoderHost Host { get; init; } = EncoderHost.Client;
/// <summary>
/// Server: whether a queued render job that needs a platform encoder still waits for a render node (BRIEF §14.7). Once
/// it does not, the server renders it itself, every output only a node could make as MJPEG in AVI at the same size and
/// frame rate, and says so in the job log. Null: such jobs always wait for a node.
/// </summary>
public Func<Job, RenderJobParameters, CancellationToken, Task<bool>>? AwaitsRenderNode { get; init; }
/// <summary>Recorded with every render this host produces (client or server name).</summary> /// <summary>Recorded with every render this host produces (client or server name).</summary>
public string ProducedBy { get; init; } = Environment.MachineName; public string ProducedBy { get; init; } = Environment.MachineName;
@@ -10,8 +10,8 @@ namespace Titano.Data.Processing;
/// The analysis and render engine of a host (BRIEF §4.3, §10, §11): job handlers for light analysis, deep analysis and /// The analysis and render engine of a host (BRIEF §4.3, §10, §11): job handlers for light analysis, deep analysis and
/// renders over the archive in the index, built with the host's decoders and encoders (<see cref="ProcessingOptions"/>). /// renders over the archive in the index, built with the host's decoders and encoders (<see cref="ProcessingOptions"/>).
/// Register it on the <see cref="JobEngine"/> next to the archive engine. A client renders every job it is given; a /// Register it on the <see cref="JobEngine"/> next to the archive engine. A client renders every job it is given; a
/// server takes only the renders its managed encoders produce and leaves the others to render nodes. Create jobs with /// server takes the renders its managed encoders produce and leaves the others to render nodes until none can take them
/// <see cref="ProcessingJobs"/>. /// (<see cref="ProcessingOptions.AwaitsRenderNode"/>). Create jobs with <see cref="ProcessingJobs"/>.
/// </summary> /// </summary>
public sealed class ProcessingService : IJobHandlerProvider public sealed class ProcessingService : IJobHandlerProvider
{ {
@@ -37,7 +37,7 @@ public sealed class ProcessingService : IJobHandlerProvider
[ [
new AnalyseJobHandler(_db, _analyser, JobKind.LightAnalyse), new AnalyseJobHandler(_db, _analyser, JobKind.LightAnalyse),
new AnalyseJobHandler(_db, _analyser, JobKind.DeepAnalyse), new AnalyseJobHandler(_db, _analyser, JobKind.DeepAnalyse),
Options.Host == EncoderHost.Server ? new ManagedRenderJobHandler(render, Options.Encoders) : render, Options.Host == EncoderHost.Server ? new ManagedRenderJobHandler(render, Options) : render,
]; ];
} }
} }
+2 -2
View File
@@ -13,8 +13,8 @@ namespace Titano.Server.Jobs;
/// Hosts the server's <see cref="JobEngine"/> (BRIEF §14.6) with the <see cref="IJobHandler"/>s registered in DI (directly or /// Hosts the server's <see cref="JobEngine"/> (BRIEF §14.6) with the <see cref="IJobHandler"/>s registered in DI (directly or
/// through <see cref="IJobHandlerProvider"/>s) and /// through <see cref="IJobHandlerProvider"/>s) and
/// relays its state changes and progress as WebSocket events. Render jobs run here only when the registered render /// relays its state changes and progress as WebSocket events. Render jobs run here only when the registered render
/// handler accepts them (managed formats, <see cref="ISelectiveJobHandler"/>); render nodes claim the others (BRIEF /// handler accepts them (<see cref="ISelectiveJobHandler"/>: managed formats, and MP4 jobs no render node can take);
/// §14.5, §14.7). On shutdown running jobs stay marked running and are requeued from their checkpoints at the next start. /// 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.
/// </summary> /// </summary>
public sealed class JobEngineHost : IHostedService public sealed class JobEngineHost : IHostedService
{ {
@@ -0,0 +1,42 @@
using System.Diagnostics;
using Titano.Core.Model;
using Titano.Data;
using Titano.Protocol;
using Titano.Server.Api;
using Titano.Server.Configuration;
namespace Titano.Server.Jobs;
/// <summary>
/// Whether a queued render job that needs a platform encoder still waits for a render node (BRIEF §14.7); once it does
/// not, the server's own engine renders it in a managed format. A job waits at least as long as a node may stay silent
/// (<see cref="ServerOptions.NodeTimeoutSeconds"/>), so a node that is restarting or reconnecting takes it back, and
/// then as long as an online node can encode its codec at its size. The server's render handler asks for every queued
/// job it examines, so the online nodes are read at most once a second.
/// </summary>
public sealed class RenderNodeAvailability(TitanoDatabase db, ServerOptions options, TimeProvider time)
{
private static readonly TimeSpan SnapshotAge = TimeSpan.FromSeconds(1);
private Snapshot? _snapshot;
public async Task<bool> AwaitsNodeAsync(Job job, RenderJobParameters parameters, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(job);
ArgumentNullException.ThrowIfNull(parameters);
var now = time.GetUtcNow();
var timeout = TimeSpan.FromSeconds(options.NodeTimeoutSeconds);
if (now - job.Created < timeout) return true;
var snapshot = Volatile.Read(ref _snapshot);
if (snapshot is null || Stopwatch.GetElapsedTime(snapshot.Read) >= SnapshotAge)
{
var online = await db.RenderNodes.ListOnlineAsync(now - timeout, cancellationToken);
snapshot = new Snapshot(Stopwatch.GetTimestamp(), [.. online.Select(Dto.Capabilities).OfType<RegisterNodeRequest>()]);
Volatile.Write(ref _snapshot, snapshot);
}
return snapshot.Nodes.Any(node => RenderNodeService.CanEncode(node, parameters));
}
/// <summary>What the online nodes announced, and when it was read (a <see cref="Stopwatch"/> timestamp).</summary>
private sealed record Snapshot(long Read, IReadOnlyList<RegisterNodeRequest> Nodes);
}
@@ -8,20 +8,23 @@ using Titano.Data.Processing;
using Titano.Protocol; using Titano.Protocol;
using Titano.Server.Configuration; using Titano.Server.Configuration;
using Titano.Server.Events; using Titano.Server.Events;
using Titano.Server.Jobs;
namespace Titano.Server.Storage; namespace Titano.Server.Storage;
/// <summary> /// <summary>
/// Analysis and render engine of the server (BRIEF §4.2, §10, §14.5, §14.6): light and deep analysis with managed /// Analysis and render engine of the server (BRIEF §4.2, §10, §14.5, §14.6): light and deep analysis with managed
/// decoders (the light analysis queued after uploads runs here) and renders with managed encoders only. The render /// decoders (the light analysis queued after uploads runs here) and renders with managed encoders only. The render
/// handler takes just the jobs whose every output a managed encoder produces (AVI MJPEG, image sequences, GIF, APNG); /// handler takes the jobs whose every output a managed encoder produces (AVI MJPEG, image sequences, GIF, APNG); MP4
/// MP4 jobs stay queued for render nodes (BRIEF §14.7). Renders recorded and Auto values changed here reach connected /// jobs wait for render nodes until none can take them (<see cref="RenderNodeAvailability"/>), and then render here as
/// MJPEG in AVI with a line in the job log (BRIEF §14.7). Renders recorded and Auto values changed here reach connected
/// clients as <c>sequenceChanged</c> events (§14.3). /// clients as <c>sequenceChanged</c> events (§14.3).
/// </summary> /// </summary>
public static class ProcessingModule public static class ProcessingModule
{ {
public static void Register(IServiceCollection services) public static void Register(IServiceCollection services)
{ {
services.AddSingleton<RenderNodeAvailability>();
services.AddSingleton(sp => services.AddSingleton(sp =>
{ {
var db = sp.GetRequiredService<TitanoDatabase>(); var db = sp.GetRequiredService<TitanoDatabase>();
@@ -31,6 +34,7 @@ public static class ProcessingModule
Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner }, Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner },
Encoders = [new ManagedEncoderProvider()], Encoders = [new ManagedEncoderProvider()],
Host = EncoderHost.Server, Host = EncoderHost.Server,
AwaitsRenderNode = sp.GetRequiredService<RenderNodeAvailability>().AwaitsNodeAsync,
ProducedBy = sp.GetRequiredService<ServerOptions>().ServerName, ProducedBy = sp.GetRequiredService<ServerOptions>().ServerName,
SequenceChanged = async (id, change, ct) => SequenceChanged = async (id, change, ct) =>
{ {
@@ -34,7 +34,7 @@ public sealed class JobEngineTests(TestContext context)
public JobKind Kind => kind; public JobKind Kind => kind;
public bool Accepts(Job job) => job.ParametersJson == Mine; public ValueTask<bool> AcceptsAsync(Job job, CancellationToken cancellationToken) => ValueTask.FromResult(job.ParametersJson == Mine);
public Task RunAsync(JobContext c) => Task.CompletedTask; public Task RunAsync(JobContext c) => Task.CompletedTask;
} }
@@ -44,7 +44,8 @@ public sealed class JobEngineTests(TestContext context)
{ {
public JobKind Kind => kind; public JobKind Kind => kind;
public bool Accepts(Job job) => job.ParametersJson?.Length > 0 ? true : throw new ArgumentNullException(nameof(job)); 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) => public Task RunAsync(JobContext c) =>
c.Job.ParametersJson is null ? throw new InvalidDataException("no parameters") : Task.CompletedTask; c.Job.ParametersJson is null ? throw new InvalidDataException("no parameters") : Task.CompletedTask;
@@ -23,16 +23,17 @@ internal sealed class ProcessingFixture : IAsyncDisposable
private JobEngine? _engine; private JobEngine? _engine;
private ProcessingFixture(ArchiveFixture archive, GateDecoder decoder) private ProcessingFixture(ArchiveFixture archive, GateDecoder decoder, Func<ProcessingOptions, ProcessingOptions>? configure)
{ {
Archive = archive; Archive = archive;
Decoder = decoder; Decoder = decoder;
Processing = new ProcessingService(archive.Db, new ProcessingOptions var options = new ProcessingOptions
{ {
Archive = new ArchiveServiceOptions { Decoder = new ImageDecoder([decoder]), Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }, Archive = new ArchiveServiceOptions { Decoder = new ImageDecoder([decoder]), Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) },
DeepAnalysis = DeepOptions, DeepAnalysis = DeepOptions,
ProducedBy = "test-pc", ProducedBy = "test-pc",
}); };
Processing = new ProcessingService(archive.Db, configure?.Invoke(options) ?? options);
} }
public ArchiveFixture Archive { get; } public ArchiveFixture Archive { get; }
@@ -43,7 +44,9 @@ internal sealed class ProcessingFixture : IAsyncDisposable
public JobEngine Engine => _engine ?? throw new InvalidOperationException("Start the engine first."); public JobEngine Engine => _engine ?? throw new InvalidOperationException("Start the engine first.");
public static async Task<ProcessingFixture> CreateAsync(TestContext context) => new(await ArchiveFixture.CreateAsync(context), new GateDecoder()); /// <param name="configure">Changes to the processing options (a server's host and render-node check).</param>
public static async Task<ProcessingFixture> CreateAsync(TestContext context, Func<ProcessingOptions, ProcessingOptions>? configure = null) =>
new(await ArchiveFixture.CreateAsync(context), new GateDecoder(), configure);
/// <summary>A started engine with the processing handlers, and the archive's when asked (one worker unless asked, fast checkpoints).</summary> /// <summary>A started engine with the processing handlers, and the archive's when asked (one worker unless asked, fast checkpoints).</summary>
public async Task<JobEngine> StartEngineAsync(bool withArchive = false, int workers = 1) public async Task<JobEngine> StartEngineAsync(bool withArchive = false, int workers = 1)
@@ -26,6 +26,9 @@ public sealed class RenderJobTests(TestContext context)
NameTemplate = "clip-{codec}", NameTemplate = "clip-{codec}",
}; };
/// <summary>HEVC (a render node's codec) at the size of <see cref="Mjpeg"/>.</summary>
private static readonly ExportSettings Hevc = Mjpeg with { Preset = new Auto<string>().WithManual("4k-hevc") };
private static SyntheticSequence Scene() => SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = Frames, StartEv = 0.5, EndEv = -0.5 }); private static SyntheticSequence Scene() => SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = Frames, StartEv = 0.5, EndEv = -0.5 });
[Test] [Test]
@@ -214,25 +217,62 @@ public sealed class RenderJobTests(TestContext context)
public async Task AServerTakesOnlyTheRendersItsManagedEncodersProduce() public async Task AServerTakesOnlyTheRendersItsManagedEncodersProduce()
{ {
await using var fx = await ProcessingFixture.CreateAsync(context); await using var fx = await ProcessingFixture.CreateAsync(context);
var server = new ProcessingService(fx.Db, new ProcessingOptions { Host = EncoderHost.Server }); var server = new ProcessingService(fx.Db, new ProcessingOptions { Host = EncoderHost.Server, AwaitsRenderNode = (_, _, _) => Task.FromResult(true) });
var handler = Assert.NotNull(server.CreateJobHandlers().Single(h => h.Kind == JobKind.Render) as ISelectiveJobHandler); var handler = Assert.NotNull(server.CreateJobHandlers().Single(h => h.Kind == JobKind.Render) as ISelectiveJobHandler);
var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "Synthetic" }; var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "Synthetic" };
bool Accepts(ExportSettings export) => Job Job(ExportSettings export) => new() { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = ProcessingJobs.Render(sequence, export, 96, 64).ParametersJson };
handler.Accepts(new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = ProcessingJobs.Render(sequence, export, 96, 64).ParametersJson }); async Task<bool> AcceptsAsync(string? parametersJson) =>
await handler.AcceptsAsync(new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = parametersJson }, fx.Ct);
Assert.True(Accepts(Mjpeg with { Batch = ["png-sequence", "gif-preview"] }), "managed outputs only"); Assert.True(await handler.AcceptsAsync(Job(Mjpeg with { Batch = ["png-sequence", "gif-preview"] }), fx.Ct), "managed outputs only");
Assert.False(Accepts(new ExportSettings { Preset = new Auto<string>().WithManual("4k-hevc") }), "HEVC needs a render node"); Assert.False(await handler.AcceptsAsync(Job(Hevc), fx.Ct), "HEVC waits for a render node");
Assert.False(Accepts(Mjpeg with { Batch = ["1080p-h264"] }), "one MP4 in the batch"); Assert.False(await handler.AcceptsAsync(Job(Mjpeg with { Batch = ["1080p-h264"] }), fx.Ct), "one MP4 in the batch");
Assert.True(handler.Accepts(new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = "{}" }), "unreadable: fails with its reason"); Assert.True(await handler.AcceptsAsync(Job(Hevc) with { Checkpoint = """{"done":[],"pending":0}""" }, fx.Ct), "started here: resumes here");
Assert.True(handler.Accepts(new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = WithPreset("""{"preset":{},"Preset":{}}""") }), Assert.True(await AcceptsAsync("{}"), "unreadable: fails with its reason");
"a member given twice: fails with its reason"); Assert.True(await AcceptsAsync(WithPreset("""{"preset":{},"Preset":{}}""")), "a member given twice: fails with its reason");
Assert.True(handler.Accepts(new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = WithPreset("""{"preset":{"isAuto":false,"manual":"mjpeg-avi"},"batch":null}""") }), Assert.True(await AcceptsAsync(WithPreset("""{"preset":{"isAuto":false,"manual":"mjpeg-avi"},"batch":null}""")), "a null batch is no batch");
"a null batch is no batch");
var client = new ProcessingService(fx.Db).CreateJobHandlers().Single(h => h.Kind == JobKind.Render); var client = new ProcessingService(fx.Db).CreateJobHandlers().Single(h => h.Kind == JobKind.Render);
Assert.False(client is ISelectiveJobHandler, "a client renders whatever it is given"); Assert.False(client is ISelectiveJobHandler, "a client renders whatever it is given");
} }
[Test]
public async Task AServerTakesMp4JobsOnceNoRenderNodeCanAndOnlyThen()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "Synthetic" };
var job = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = ProcessingJobs.Render(sequence, Hevc, 96, 64).ParametersJson };
async Task<bool> AcceptsAsync(Func<Job, Titano.Protocol.RenderJobParameters, CancellationToken, Task<bool>>? awaits)
{
var server = new ProcessingService(fx.Db, new ProcessingOptions { Host = EncoderHost.Server, AwaitsRenderNode = awaits });
return await ((ISelectiveJobHandler)server.CreateJobHandlers().Single(h => h.Kind == JobKind.Render)).AcceptsAsync(job, fx.Ct);
}
Titano.Protocol.RenderJobParameters? asked = null;
Assert.True(await AcceptsAsync((_, p, _) => { asked = p; return Task.FromResult(false); }), "no node can take it");
Assert.Equal(("hevc", 96, 64), (asked!.Codec, asked.Width, asked.Height));
Assert.False(await AcceptsAsync(null), "without a node check, MP4 jobs always wait");
Assert.False(await AcceptsAsync((_, _, _) => throw new InvalidOperationException("database unreachable")), "unknown: keeps waiting");
}
[Test]
public async Task AServerRendersAsMjpegWhatNoRenderNodeTookAndSaysSo()
{
await using var fx = await ProcessingFixture.CreateAsync(context, o => o with { Host = EncoderHost.Server, AwaitsRenderNode = (_, _, _) => Task.FromResult(false) });
var sequence = await fx.AddAsync(Scene());
await fx.StartEngineAsync();
var request = ProcessingJobs.Render(sequence, Hevc, 96, 64);
var job = await fx.WaitForAsync((await fx.EnqueueAsync(request)).Id, JobState.Done);
var avi = await File.ReadAllBytesAsync(Path.Combine(fx.Folder(sequence).Exports, "clip-MJPEG.avi"), fx.Ct);
Assert.Equal(("RIFF", "AVI "), (Encoding.ASCII.GetString(avi, 0, 4), Encoding.ASCII.GetString(avi, 8, 4)));
var render = (await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct)).Single();
var preset = Loc.L("Export.Preset.Custom");
Assert.Equal((preset, "Mjpeg", 96, 64), (render.PresetName, render.Codec, render.Width, render.Height));
Assert.Contains(Loc.L("Export.Log.NoRenderNode", preset, "HEVC", "MJPEG"), job.LogTail);
}
[Test] [Test]
public void ABatchForAServerBecomesOneJobPerPreset() public void ABatchForAServerBecomesOneJobPerPreset()
{ {
@@ -243,6 +283,11 @@ public sealed class RenderJobTests(TestContext context)
Assert.True(jobs.All(j => j.Kind == JobKind.Render && j.Targets.SequenceEqual([sequence.Id])), "render jobs of the sequence"); Assert.True(jobs.All(j => j.Kind == JobKind.Render && j.Targets.SequenceEqual([sequence.Id])), "render jobs of the sequence");
Assert.True(parameters.All(p => ProcessingJobs.ReadExport(p.PresetJson).Batch.Count == 0), "no job carries a batch"); Assert.True(parameters.All(p => ProcessingJobs.ReadExport(p.PresetJson).Batch.Count == 0), "no job carries a batch");
Assert.Equal((3840, 2160), (parameters[2].Width, parameters[2].Height)); Assert.Equal((3840, 2160), (parameters[2].Width, parameters[2].Height));
// A posted batch written by hand may hold blank entries: they name no preset.
var posted = System.Text.Json.JsonSerializer.Deserialize(WithPreset("""{"preset":{"isAuto":false,"manual":"mjpeg-avi"},"batch":[null," ","gif-preview"]}"""),
Titano.Protocol.ProtocolJson.Default.RenderJobParameters)!;
Assert.SequenceEqual(["mjpeg", "gif"], ProcessingJobs.Split(posted, 96, 64).Select(p => p.Codec));
} }
/// <summary>MJPEG job parameters whose preset JSON is <paramref name="presetJson"/>.</summary> /// <summary>MJPEG job parameters whose preset JSON is <paramref name="presetJson"/>.</summary>
@@ -94,6 +94,47 @@ public sealed class ProcessingModuleTests(TestContext context)
Assert.Contains(Drain(events), e => e is SequenceChangedEvent { Change: "renders" } changed && changed.SequenceId == sequence.Id); Assert.Contains(Drain(events), e => e is SequenceChangedEvent { Change: "renders" } changed && changed.SequenceId == sequence.Id);
} }
[Test]
public async Task Mp4JobsNoRenderNodeCanTakeAreRenderedAsMjpegAndTheLogSaysSo()
{
var clock = new ManualTimeProvider();
await using var server = await TestServer.StartAsync(context, o => o.NodeTimeoutSeconds = 60, s =>
{
Production(s);
s.AddSingleton<TimeProvider>(clock);
});
var engine = server.Services.GetRequiredService<JobEngineHost>().Engine;
var (editor, _, _) = await server.UserAsync("alberto", "editor", renderNode: true);
var sequence = (await server.Database.Sequences.GetAsync(await UploadSceneAsync(editor)))!;
var hevc = new ExportSettings
{
Preset = new Auto<string>().WithManual("4k-hevc"),
Overrides = new ExportOverrides { Width = 96, Height = 64 },
NameTemplate = "clip-{codec}",
};
var job = await JobApiTests.CreateAsync(editor, "render", sequence.Id, ProcessingJobs.Render(sequence, hevc, 96, 64).ParametersJson);
// A node that encodes HEVC is online: the job waits for it, however long it has been queued.
using (var registered = await editor.PostAsJsonAsync("/api/v1/nodes/register",
new RegisterNodeRequest("studio-pc", [new NodeEncoder("hevc", "NVENC HEVC", true, true, 8192, 8192)], 8192, 8192), ProtocolJson.Default.RegisterNodeRequest))
await registered.ExpectAsync(HttpStatusCode.OK);
clock.Advance(TimeSpan.FromSeconds(61));
var node = (await server.Database.RenderNodes.ListAsync()).Single();
Assert.True(await server.Database.RenderNodes.HeartbeatAsync(node.Id, "idle", clock.GetUtcNow()));
engine.Wake();
await Task.Delay(1500);
Assert.Equal("queued", (await JobApiTests.GetAsync(editor, job.Id)).State);
// The node goes silent: the server renders the job itself in a managed format.
clock.Advance(TimeSpan.FromSeconds(61));
engine.Wake();
var done = await JobApiTests.WaitForAsync(editor, job.Id, "done");
Assert.Equal(JobEngineHost.WorkerId, done.NodeId?.Split('#')[0]);
var render = (await server.Database.Renders.ListBySequenceAsync(sequence.Id)).Single();
Assert.Equal(($"{sequence.Folder}/exports/clip-MJPEG.avi", "Mjpeg", 96, 64), (render.OutputPath, render.Codec, render.Width, render.Height));
Assert.Contains(Loc.L("Export.Log.NoRenderNode", Loc.L("Export.Preset.Custom"), "HEVC", "MJPEG"), done.LogTail);
}
[Test] [Test]
public async Task APostedBatchBecomesOneJobPerPreset() public async Task APostedBatchBecomesOneJobPerPreset()
{ {