diff --git a/i18n/en/Export.resx b/i18n/en/Export.resx index 3477a2d4..34850b65 100644 --- a/i18n/en/Export.resx +++ b/i18n/en/Export.resx @@ -16,6 +16,7 @@ Downloading framesrender node progress stage Measuring framesjob progress stage {0} failed: {1}long: job log line; {0} preset name; {1} reason + No render node can encode {1}; {0} is rendered as {2} instead.long: job log line; {0} preset name; {1} codec asked for; {2} managed codec used {0} saved as {1}long: job log line; {0} preset name; {1} output file name Playback rate not understood; the export frame rate is used.long No capture times: frames are spaced evenly.long diff --git a/i18n/it/Export.it.resx b/i18n/it/Export.it.resx index 27aed9a6..da3ab965 100644 --- a/i18n/it/Export.it.resx +++ b/i18n/it/Export.it.resx @@ -16,6 +16,7 @@ Download dei fotogrammi Misura dei fotogrammi {0} non riuscito: {1} + Nessun nodo di rendering può codificare {1}; {0} viene esportato in {2}. {0} salvato come {1} Velocità di riproduzione non valida; viene usata la frequenza di esportazione. Nessun orario di scatto: fotogrammi distribuiti in modo uniforme. diff --git a/src/Titano.Data/Jobs/JobEngine.cs b/src/Titano.Data/Jobs/JobEngine.cs index d6296ebc..0b883614 100644 --- a/src/Titano.Data/Jobs/JobEngine.cs +++ b/src/Titano.Data/Jobs/JobEngine.cs @@ -242,7 +242,7 @@ public sealed class JobEngine : IAsyncDisposable Job? candidate = null; 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; break; } @@ -261,13 +261,13 @@ public sealed class JobEngine : IAsyncDisposable } /// A job its handler cannot judge is taken, so it fails here with its reason instead of blocking the queue. - private bool Accepts(Job job) + private async ValueTask AcceptsAsync(Job job, CancellationToken cancellationToken) { 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); return true; diff --git a/src/Titano.Data/Jobs/JobHandler.cs b/src/Titano.Data/Jobs/JobHandler.cs index 6a1af038..f78f9246 100644 --- a/src/Titano.Data/Jobs/JobHandler.cs +++ b/src/Titano.Data/Jobs/JobHandler.cs @@ -14,13 +14,14 @@ public interface IJobHandler } /// -/// A handler that runs only some jobs of its kind, decided from the job alone (its parameters never change). The engine -/// leaves the others queued for other claimers: the server renders what its managed encoders produce and leaves the -/// rest to render nodes (BRIEF §14.5, §14.7). +/// A handler that runs only some jobs of its kind. The engine leaves the others queued for other claimers and asks again +/// at every claim, so an answer may change while the job waits: the server renders what its managed encoders produce, +/// leaves the rest to render nodes, and takes those too in a managed format once no node can (BRIEF §14.5, §14.7). /// public interface ISelectiveJobHandler : IJobHandler { - bool Accepts(Job job); + /// Whether this handler takes now. A job it cannot read should be taken, so it fails with its reason. + ValueTask AcceptsAsync(Job job, CancellationToken cancellationToken); } /// A component that brings several handlers (the archive engine: scan, rebuild, verify, import, images, maintenance). diff --git a/src/Titano.Data/Processing/Jobs/ManagedRenderJobHandler.cs b/src/Titano.Data/Processing/Jobs/ManagedRenderJobHandler.cs index 0dfb22fa..7151b807 100644 --- a/src/Titano.Data/Processing/Jobs/ManagedRenderJobHandler.cs +++ b/src/Titano.Data/Processing/Jobs/ManagedRenderJobHandler.cs @@ -1,39 +1,87 @@ +using Titano.Core.Diagnostics; using Titano.Core.Export; +using Titano.Core.Localization; using Titano.Core.Model; using Titano.Core.Video; using Titano.Data.Jobs; +using Titano.Protocol; namespace Titano.Data.Processing.Jobs; /// -/// on the server (BRIEF §4.4, §14.5): it takes only jobs whose every output (the preset and -/// each batch preset) its managed encoders produce, and runs them like a client does. Jobs that need a platform encoder -/// (MP4) stay queued for render nodes (§14.7). Jobs whose parameters cannot be read are taken, so they fail with their -/// reason instead of waiting for a claimer that never comes. +/// on the server (BRIEF §4.4, §14.5, §14.7): it takes the jobs whose every output (the preset +/// and each batch preset) its managed encoders produce, and runs them like a client does. Jobs that need a platform +/// encoder (MP4) wait for render nodes while says one may take them; +/// 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. /// -internal sealed class ManagedRenderJobHandler(RenderJobHandler render, IReadOnlyList encoders) : ISelectiveJobHandler +internal sealed class ManagedRenderJobHandler(RenderJobHandler render, ProcessingOptions options) : ISelectiveJobHandler { - private readonly EncoderCapability[] _capabilities = [.. encoders.SelectMany(e => e.Capabilities)]; + /// The managed preset whose values stand in for an output only a render node could make. + private const string FallbackPresetId = "mjpeg-avi"; + + private readonly EncoderCapability[] _capabilities = [.. options.Encoders.SelectMany(e => e.Capabilities)]; public JobKind Kind => JobKind.Render; - public bool Accepts(Job job) + public async ValueTask AcceptsAsync(Job job, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(job); + RenderJobParameters parameters; try { - var parameters = ProcessingJobs.ReadParameters(job.ParametersJson); - // Most declined 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)) - && RenderJobHandler.Targets(ProcessingJobs.ReadExport(parameters.PresetJson), parameters.PresetName).All(t => Produces(t.Export)); + parameters = ProcessingJobs.ReadParameters(job.ParametersJson); + // Most jobs are decided by the main codec, before the export is read. + if (Produces(parameters.Codec) && 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; } + 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)); + + /// The output as MJPEG in AVI with the managed preset's quality, at the size and frame rate asked for. + 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) => _capabilities.Any(c => c.Codec == export.Codec && export.Width <= c.MaxWidth && export.Height <= c.MaxHeight); diff --git a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs index 47fb5865..359361d5 100644 --- a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs +++ b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs @@ -25,7 +25,10 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti { public JobKind Kind => JobKind.Render; - public async Task RunAsync(JobContext context) + public Task RunAsync(JobContext context) => RunAsync(context, target => target); + + /// Runs the job with each output still to render passed through (the server's managed stand-ins). + internal async Task RunAsync(JobContext context, Func adapt) { var cancellation = context.Cancellation; 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))); 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; var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, cancellation).ConfigureAwait(false); diff --git a/src/Titano.Data/Processing/ProcessingOptions.cs b/src/Titano.Data/Processing/ProcessingOptions.cs index 82add6e3..e14efb8e 100644 --- a/src/Titano.Data/Processing/ProcessingOptions.cs +++ b/src/Titano.Data/Processing/ProcessingOptions.cs @@ -1,10 +1,12 @@ using Titano.Core.Analysis.Deep; using Titano.Core.Export; using Titano.Core.Imaging; +using Titano.Core.Model; using Titano.Core.Render; using Titano.Core.Video; using Titano.Core.Video.Managed; using Titano.Data.Archiving; +using Titano.Protocol; namespace Titano.Data.Processing; @@ -40,9 +42,16 @@ public sealed record ProcessingOptions /// public Func? SequenceChanged { get; init; } - /// The server delegates platform codecs to render nodes and never runs them itself. + /// The server delegates platform codecs to render nodes (). public EncoderHost Host { get; init; } = EncoderHost.Client; + /// + /// 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. + /// + public Func>? AwaitsRenderNode { get; init; } + /// Recorded with every render this host produces (client or server name). public string ProducedBy { get; init; } = Environment.MachineName; diff --git a/src/Titano.Data/Processing/ProcessingService.cs b/src/Titano.Data/Processing/ProcessingService.cs index b5033dfd..581a87b7 100644 --- a/src/Titano.Data/Processing/ProcessingService.cs +++ b/src/Titano.Data/Processing/ProcessingService.cs @@ -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 /// renders over the archive in the index, built with the host's decoders and encoders (). /// Register it on the 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 +/// (). Create jobs with . /// 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.DeepAnalyse), - Options.Host == EncoderHost.Server ? new ManagedRenderJobHandler(render, Options.Encoders) : render, + Options.Host == EncoderHost.Server ? new ManagedRenderJobHandler(render, Options) : render, ]; } } diff --git a/src/Titano.Server/Jobs/JobEngineHost.cs b/src/Titano.Server/Jobs/JobEngineHost.cs index 2c5d5faf..e9ea3401 100644 --- a/src/Titano.Server/Jobs/JobEngineHost.cs +++ b/src/Titano.Server/Jobs/JobEngineHost.cs @@ -13,8 +13,8 @@ namespace Titano.Server.Jobs; /// Hosts the server's (BRIEF §14.6) with the s registered in DI (directly or /// through s) and /// relays its state changes and progress as WebSocket events. Render jobs run here only when the registered render -/// handler accepts them (managed formats, ); 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. +/// handler accepts them (: 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. /// public sealed class JobEngineHost : IHostedService { diff --git a/src/Titano.Server/Jobs/RenderNodeAvailability.cs b/src/Titano.Server/Jobs/RenderNodeAvailability.cs new file mode 100644 index 00000000..44d3e7b3 --- /dev/null +++ b/src/Titano.Server/Jobs/RenderNodeAvailability.cs @@ -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; + +/// +/// 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 +/// (), 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. +/// +public sealed class RenderNodeAvailability(TitanoDatabase db, ServerOptions options, TimeProvider time) +{ + private static readonly TimeSpan SnapshotAge = TimeSpan.FromSeconds(1); + + private Snapshot? _snapshot; + + public async Task 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()]); + Volatile.Write(ref _snapshot, snapshot); + } + return snapshot.Nodes.Any(node => RenderNodeService.CanEncode(node, parameters)); + } + + /// What the online nodes announced, and when it was read (a timestamp). + private sealed record Snapshot(long Read, IReadOnlyList Nodes); +} diff --git a/src/Titano.Server/Storage/ProcessingModule.cs b/src/Titano.Server/Storage/ProcessingModule.cs index 16477d56..26b47a9a 100644 --- a/src/Titano.Server/Storage/ProcessingModule.cs +++ b/src/Titano.Server/Storage/ProcessingModule.cs @@ -8,20 +8,23 @@ using Titano.Data.Processing; using Titano.Protocol; using Titano.Server.Configuration; using Titano.Server.Events; +using Titano.Server.Jobs; namespace Titano.Server.Storage; /// /// 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 -/// handler takes just the jobs whose every output a managed encoder produces (AVI MJPEG, image sequences, GIF, APNG); -/// MP4 jobs stay queued for render nodes (BRIEF §14.7). Renders recorded and Auto values changed here reach connected +/// handler takes the jobs whose every output a managed encoder produces (AVI MJPEG, image sequences, GIF, APNG); MP4 +/// jobs wait for render nodes until none can take them (), 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 sequenceChanged events (§14.3). /// public static class ProcessingModule { public static void Register(IServiceCollection services) { + services.AddSingleton(); services.AddSingleton(sp => { var db = sp.GetRequiredService(); @@ -31,6 +34,7 @@ public static class ProcessingModule Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner }, Encoders = [new ManagedEncoderProvider()], Host = EncoderHost.Server, + AwaitsRenderNode = sp.GetRequiredService().AwaitsNodeAsync, ProducedBy = sp.GetRequiredService().ServerName, SequenceChanged = async (id, change, ct) => { diff --git a/tests/Titano.Data.Tests/Jobs/JobEngineTests.cs b/tests/Titano.Data.Tests/Jobs/JobEngineTests.cs index 283648b6..d4d8d15b 100644 --- a/tests/Titano.Data.Tests/Jobs/JobEngineTests.cs +++ b/tests/Titano.Data.Tests/Jobs/JobEngineTests.cs @@ -34,7 +34,7 @@ public sealed class JobEngineTests(TestContext context) public JobKind Kind => kind; - public bool Accepts(Job job) => job.ParametersJson == Mine; + public ValueTask AcceptsAsync(Job job, CancellationToken cancellationToken) => ValueTask.FromResult(job.ParametersJson == Mine); public Task RunAsync(JobContext c) => Task.CompletedTask; } @@ -44,7 +44,8 @@ public sealed class JobEngineTests(TestContext context) { public JobKind Kind => kind; - public bool Accepts(Job job) => job.ParametersJson?.Length > 0 ? true : throw new ArgumentNullException(nameof(job)); + public ValueTask 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; diff --git a/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs b/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs index 9572451f..d5b9ad90 100644 --- a/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs +++ b/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs @@ -23,16 +23,17 @@ internal sealed class ProcessingFixture : IAsyncDisposable private JobEngine? _engine; - private ProcessingFixture(ArchiveFixture archive, GateDecoder decoder) + private ProcessingFixture(ArchiveFixture archive, GateDecoder decoder, Func? configure) { Archive = archive; 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) }, DeepAnalysis = DeepOptions, ProducedBy = "test-pc", - }); + }; + Processing = new ProcessingService(archive.Db, configure?.Invoke(options) ?? options); } 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 static async Task CreateAsync(TestContext context) => new(await ArchiveFixture.CreateAsync(context), new GateDecoder()); + /// Changes to the processing options (a server's host and render-node check). + public static async Task CreateAsync(TestContext context, Func? configure = null) => + new(await ArchiveFixture.CreateAsync(context), new GateDecoder(), configure); /// A started engine with the processing handlers, and the archive's when asked (one worker unless asked, fast checkpoints). public async Task StartEngineAsync(bool withArchive = false, int workers = 1) diff --git a/tests/Titano.Data.Tests/Processing/RenderJobTests.cs b/tests/Titano.Data.Tests/Processing/RenderJobTests.cs index 4739af73..656032ef 100644 --- a/tests/Titano.Data.Tests/Processing/RenderJobTests.cs +++ b/tests/Titano.Data.Tests/Processing/RenderJobTests.cs @@ -26,6 +26,9 @@ public sealed class RenderJobTests(TestContext context) NameTemplate = "clip-{codec}", }; + /// HEVC (a render node's codec) at the size of . + private static readonly ExportSettings Hevc = Mjpeg with { Preset = new Auto().WithManual("4k-hevc") }; + private static SyntheticSequence Scene() => SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = Frames, StartEv = 0.5, EndEv = -0.5 }); [Test] @@ -214,25 +217,62 @@ public sealed class RenderJobTests(TestContext context) public async Task AServerTakesOnlyTheRendersItsManagedEncodersProduce() { 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 sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "Synthetic" }; - bool Accepts(ExportSettings export) => - handler.Accepts(new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = ProcessingJobs.Render(sequence, export, 96, 64).ParametersJson }); + Job Job(ExportSettings export) => new() { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = ProcessingJobs.Render(sequence, export, 96, 64).ParametersJson }; + async Task 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.False(Accepts(new ExportSettings { Preset = new Auto().WithManual("4k-hevc") }), "HEVC needs a render node"); - Assert.False(Accepts(Mjpeg with { Batch = ["1080p-h264"] }), "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(handler.Accepts(new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, ParametersJson = 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}""") }), - "a null batch is no batch"); + Assert.True(await handler.AcceptsAsync(Job(Mjpeg with { Batch = ["png-sequence", "gif-preview"] }), fx.Ct), "managed outputs only"); + Assert.False(await handler.AcceptsAsync(Job(Hevc), fx.Ct), "HEVC waits for a render node"); + Assert.False(await handler.AcceptsAsync(Job(Mjpeg with { Batch = ["1080p-h264"] }), fx.Ct), "one MP4 in the batch"); + Assert.True(await handler.AcceptsAsync(Job(Hevc) with { Checkpoint = """{"done":[],"pending":0}""" }, fx.Ct), "started here: resumes here"); + Assert.True(await AcceptsAsync("{}"), "unreadable: fails with its reason"); + Assert.True(await AcceptsAsync(WithPreset("""{"preset":{},"Preset":{}}""")), "a member given twice: fails with its reason"); + Assert.True(await AcceptsAsync(WithPreset("""{"preset":{"isAuto":false,"manual":"mjpeg-avi"},"batch":null}""")), "a null batch is no batch"); 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"); } + [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 AcceptsAsync(Func>? 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] 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(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)); + + // 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)); } /// MJPEG job parameters whose preset JSON is . diff --git a/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs b/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs index aeb83eb6..6cb2d346 100644 --- a/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs +++ b/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs @@ -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); } + [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(clock); + }); + var engine = server.Services.GetRequiredService().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().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] public async Task APostedBatchBecomesOneJobPerPreset() {