fix(processing): concurrent renders keep their own names; analysis status under the lock; server announces changes

A render reserves the output name it picks, so the same preset queued twice
no longer overwrites one output with the other; render rows take ids from
their job and entry, so a resumed job recognises only its own outputs and
never deletes another render's file. A light pass records its status under
the sequence lock and cannot clear a deep pass's. Renders and Auto value
changes made by the server engine publish sequenceChanged events.

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 23:16:23 +00:00
1 parent 23c40ece6f
commit c7ca1ef463
9 files changed
+316 -56

No files matched your search

+9 -8
View File
@@ -105,33 +105,34 @@ public static class ExportNaming
/// <summary>
/// Full output path inside <paramref name="folder"/>: the expanded name plus the codec's extension, or the bare name
/// for image sequences (a folder), made unique against existing files and folders.
/// for image sequences (a folder), made unique against existing files and folders and the paths <paramref name="taken"/>
/// says are being written.
/// </summary>
public static string OutputPath(string folder, string template, ExportNameContext context)
public static string OutputPath(string folder, string template, ExportNameContext context, Func<string, bool>? taken = null)
{
ArgumentException.ThrowIfNullOrWhiteSpace(folder);
var name = Expand(template, context).Name;
var extension = VideoCodecInfo.DefaultContainer(context.Codec) == ContainerFormat.ImageSequence ? string.Empty : VideoCodecInfo.FileExtension(context.Codec);
return Unique(folder, name, extension);
return Unique(folder, name, extension, taken);
}
/// <summary>
/// Output path of a project's export: <see cref="ExportSettings.Folder"/> or, when unset, the sequence's exports folder,
/// named by <see cref="ExportSettings.NameTemplate"/>.
/// </summary>
public static string OutputPath(ExportSettings settings, string sequenceExportsFolder, ExportNameContext context)
public static string OutputPath(ExportSettings settings, string sequenceExportsFolder, ExportNameContext context, Func<string, bool>? taken = null)
{
ArgumentNullException.ThrowIfNull(settings);
return OutputPath(string.IsNullOrWhiteSpace(settings.Folder) ? sequenceExportsFolder : settings.Folder, settings.NameTemplate, context);
return OutputPath(string.IsNullOrWhiteSpace(settings.Folder) ? sequenceExportsFolder : settings.Folder, settings.NameTemplate, context, taken);
}
/// <summary>"name.ext", or "name (2).ext", "name (3).ext"… when a file or folder of that name exists.</summary>
public static string Unique(string folder, string name, string extension)
/// <summary>"name.ext", or "name (2).ext", "name (3).ext"… when a file or folder of that name exists or is <paramref name="taken"/>.</summary>
public static string Unique(string folder, string name, string extension, Func<string, bool>? taken = null)
{
for (var n = 1; ; n++)
{
var candidate = Path.Combine(folder, (n == 1 ? name : string.Create(CultureInfo.InvariantCulture, $"{name} ({n})")) + extension);
if (!File.Exists(candidate) && !Directory.Exists(candidate)) return candidate;
if (!File.Exists(candidate) && !Directory.Exists(candidate) && taken?.Invoke(candidate) != true) return candidate;
}
}
@@ -1,3 +1,5 @@
using System.Buffers.Binary;
using System.Security.Cryptography;
using Titano.Core.Archiving;
using Titano.Core.Export;
using Titano.Core.Localization;
@@ -13,9 +15,11 @@ namespace Titano.Data.Processing.Jobs;
/// <c>RenderJobParameters</c> whose preset JSON is the export section of a project (preset, overrides, name template,
/// folder, batch). The project is the embedded one or the sequence's own; frames flagged missing, corrupt, excluded or
/// undecodable are left out. The preset and every batch preset render after one preparation pass into the sequence's
/// <c>exports/</c> (or the export folder, never on a server), named by the template, and each finished output is recorded at once.
/// Checkpoint: the batch entries recorded and the output being written, so a crashed attempt's leftovers are removed and
/// only unfinished entries render again. A failed entry fails the job after the others ran; retrying renders it alone.
/// <c>exports/</c> (or the export folder, never on a server), named by the template without taking a name another render
/// of this process is writing, and each finished output is recorded at once (its row id derives from the job and the
/// entry, so every attempt knows what is recorded). Checkpoint: the entries recorded and the output being written, so a
/// crashed attempt's leftovers are removed and only unfinished entries render again. A failed entry fails the job after
/// the others ran; retrying renders it alone.
/// </summary>
internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions options) : IJobHandler
{
@@ -36,16 +40,23 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti
var project = (parameters.SettingsJson is { } embedded ? ProjectStore.Parse(embedded) : ProjectStore.Read(sequence, folder)) with { Export = export };
var settingsJson = project.ToJson();
using var names = new OutputNames();
var all = Targets(export, parameters.PresetName);
var resume = RenderCheckpoint.Parse(context.Checkpoint);
var done = resume.Done.ToList();
if (resume is { Pending: { } pending, PendingOutput: { } output })
// A crash after a render row was written but before the checkpoint: that entry is finished.
for (var i = 0; i < all.Count; i++)
{
// A crash after the render row was written but before the checkpoint: that entry is finished.
var recorded = await db.Renders.ListBySequenceAsync(sequence.Id, cancellation).ConfigureAwait(false);
if (recorded.Any(r => r.OutputPath == Stored(paths, output))) done.Add(pending);
else Discard(output);
if (!done.Contains(i) && await db.Renders.GetAsync(RenderId(context.Job.Id, i), cancellation).ConfigureAwait(false) is not null) done.Add(i);
}
var targets = Targets(export, parameters.PresetName).Select((t, i) => (i, t)).Where(t => !done.Contains(t.i)).ToList();
if (resume is { Pending: { } pending, PendingOutput: { } output } && !done.Contains(pending) && names.TryReserve(output))
{
// Unless another render recorded that name since (the output did not exist yet when it chose it).
var recorded = await db.Renders.ListBySequenceAsync(sequence.Id, cancellation).ConfigureAwait(false);
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();
if (targets.Count == 0) return;
var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, cancellation).ConfigureAwait(false);
@@ -54,17 +65,22 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti
{
outcomes = await render.RenderAllAsync(
targets,
(target, setup) => ExportNaming.OutputPath(project.Export, folder.Exports, SequenceRender.NameContext(sequence, target, setup)),
(target, setup) => names.Reserve(taken => ExportNaming.OutputPath(project.Export, folder.Exports, SequenceRender.NameContext(sequence, target, setup), taken)),
(index, path) => context.CheckpointAsync(new RenderCheckpoint(done, index, path).ToJson(), context.Done, context.Total),
async (index, target, result) =>
{
// The output is complete: record it even if the job is being cancelled, so it is never orphaned.
var entity = result.ToEntity(sequence.Id, target.PresetName, settingsJson, options.ProducedBy) with { OutputPath = Stored(paths, result.OutputPath) };
var entity = result.ToEntity(sequence.Id, target.PresetName, settingsJson, options.ProducedBy) with
{
Id = RenderId(context.Job.Id, index),
OutputPath = Stored(paths, result.OutputPath),
};
await db.Renders.InsertAsync(entity, CancellationToken.None).ConfigureAwait(false);
done.Add(index);
await context.CheckpointAsync(new RenderCheckpoint(done, null, null).ToJson(), context.Done, context.Total).ConfigureAwait(false);
context.Log(Loc.L("Export.Log.Rendered", target.PresetName, Path.GetFileName(result.OutputPath)));
foreach (var remark in SequenceRender.Remarks(result)) context.Log(remark);
await options.NotifyAsync(sequence.Id, ProcessingOptions.RenderedChange, CancellationToken.None).ConfigureAwait(false);
},
context.ReportAsync,
cancellation).ConfigureAwait(false);
@@ -90,15 +106,32 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti
return relative.StartsWith("..", StringComparison.Ordinal) || Path.IsPathRooted(relative) ? output : relative;
}
/// <summary>
/// The id of the render row of entry <paramref name="index"/> of a job: the same in every attempt, so a resumed job
/// finds what it recorded (a name-based UUID, version 8).
/// </summary>
internal static Guid RenderId(Guid jobId, int index)
{
Span<byte> name = stackalloc byte[20];
jobId.TryWriteBytes(name, bigEndian: true, out _);
BinaryPrimitives.WriteInt32BigEndian(name[16..], index);
Span<byte> hash = stackalloc byte[SHA256.HashSizeInBytes];
SHA256.HashData(name, hash);
hash[6] = (byte)((hash[6] & 0x0F) | 0x80);
hash[8] = (byte)((hash[8] & 0x3F) | 0x80);
return new Guid(hash[..16], bigEndian: true);
}
/// <summary>
/// Removes what an interrupted attempt left of <paramref name="output"/>: the encoder's hidden temporary file or
/// folder, and an output that was completed but never recorded (it renders again under the same name).
/// </summary>
private static void Discard(string output)
private static void Discard(string output, bool keepOutput)
{
var parent = Path.GetDirectoryName(output);
if (parent is null || !Directory.Exists(parent)) return;
foreach (var leftover in Directory.EnumerateFileSystemEntries(parent, "." + Path.GetFileName(output) + ".*.tmp").Append(output))
var leftovers = Directory.EnumerateFileSystemEntries(parent, "." + Path.GetFileName(output) + ".*.tmp");
foreach (var leftover in keepOutput ? leftovers : leftovers.Append(output))
{
if (Directory.Exists(leftover)) Directory.Delete(leftover, recursive: true);
else if (File.Exists(leftover)) File.Delete(leftover);
+55
View File
@@ -0,0 +1,55 @@
using System.Collections.Concurrent;
namespace Titano.Data.Processing;
/// <summary>
/// The output paths the renders of this process are writing. Encoders write to a hidden temporary file and rename it over
/// the output when they finish, so two renders that picked the same free name (the same preset queued twice) would
/// overwrite each other: a render reserves each name it picks until it ends.
/// </summary>
internal sealed class OutputNames : IDisposable
{
private static readonly ConcurrentDictionary<string, byte> Writing = new(StringComparer.OrdinalIgnoreCase);
private readonly List<string> _mine = [];
/// <summary>A path from <paramref name="pick"/>, given the test for paths other renders are writing, reserved for this render.</summary>
public string Reserve(Func<Func<string, bool>, string> pick)
{
while (true)
{
var path = pick(p => Writing.ContainsKey(Key(p)));
// Another render may have taken the same name since: pick again.
if (TryReserve(path)) return path;
}
}
/// <summary>Reserves <paramref name="path"/> for this render; false when another render is writing it.</summary>
public bool TryReserve(string path)
{
var key = Key(path);
if (!Writing.TryAdd(key, 0)) return false;
lock (_mine) _mine.Add(key);
return true;
}
/// <summary>Gives back a path this render reserved, so it can be picked again.</summary>
public void Release(string path)
{
var key = Key(path);
lock (_mine)
{
if (_mine.Remove(key)) Writing.TryRemove(key, out _);
}
}
public void Dispose()
{
lock (_mine)
{
foreach (var key in _mine) Writing.TryRemove(key, out _);
_mine.Clear();
}
}
private static string Key(string path) => Path.GetFullPath(path);
}
+19 -14
View File
@@ -59,21 +59,26 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti
(changed, unreadable) = (false, true);
}
// A deep analysis of the same sequence may have finished meanwhile: count its findings and keep its status.
report ??= ReadReport(folder, frames.Count);
var warnings = Advisor.Recommend(light, report, context).Findings.Count(f => f.Severity >= FindingSeverity.Warning);
var set = SequenceStatus.AnalyzedLight | (report is null ? SequenceStatus.None : SequenceStatus.AnalyzedDeep)
| (warnings > 0 ? SequenceStatus.HasWarnings : SequenceStatus.None);
// Without a current report (none, or made before frames were added) the deep status no longer holds.
var clear = (report is null ? SequenceStatus.AnalyzedDeep : SequenceStatus.None) | (warnings > 0 ? SequenceStatus.None : SequenceStatus.HasWarnings);
var summary = deep && report is not null ? AnalysisSummary.From(report).ToJson() : null;
await writer.UpdateAsync(paths, sequence.Id, sequence.Folder, m =>
if (changed) await options.NotifyAsync(sequence.Id, ProcessingOptions.SettingsChange, cancellationToken).ConfigureAwait(false);
int warnings;
// Under the sequence lock, which a deep pass of the same sequence also takes to record its report: a light pass
// then either sees that report or runs before it is recorded, and never clears what it recorded.
await using (await writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false))
{
var status = (m.Status | set) & ~clear;
return status == m.Status ? null : m with { Status = status, UpdatedAt = DateTimeOffset.UtcNow };
}, cancellationToken).ConfigureAwait(false);
await db.Sequences.RecordAnalysisAsync(sequence.Id, set, clear, warnings, summary, cancellationToken).ConfigureAwait(false);
await indexer.RecordStampsAsync(archive, folder, sequence.Id, cancellationToken).ConfigureAwait(false);
report ??= ReadReport(folder, frames.Count);
warnings = Advisor.Recommend(light, report, context).Findings.Count(f => f.Severity >= FindingSeverity.Warning);
var set = SequenceStatus.AnalyzedLight | (report is null ? SequenceStatus.None : SequenceStatus.AnalyzedDeep)
| (warnings > 0 ? SequenceStatus.HasWarnings : SequenceStatus.None);
// Without a current report (none, or made before frames were added) the deep status no longer holds.
var clear = (report is null ? SequenceStatus.AnalyzedDeep : SequenceStatus.None) | (warnings > 0 ? SequenceStatus.None : SequenceStatus.HasWarnings);
var manifest = SequenceWriter.ReadLocked(folder, sequence.Id);
var status = (manifest.Status | set) & ~clear;
if (status != manifest.Status) ManifestStore.WriteSequence(folder.ManifestPath, manifest with { Status = status, UpdatedAt = DateTimeOffset.UtcNow });
var summary = deep && report is not null ? AnalysisSummary.From(report).ToJson() : null;
await db.Sequences.RecordAnalysisAsync(sequence.Id, set, clear, warnings, summary, cancellationToken).ConfigureAwait(false);
await indexer.RecordStampsAsync(archive, folder, sequence.Id, cancellationToken).ConfigureAwait(false);
}
var presets = deep ? SequencePresets.Generate(light, report, context) : [];
return new AnalysisOutcome(sequence, frames.Count, warnings, changed, unreadable, presets);
+20 -7
View File
@@ -5,7 +5,9 @@ using Titano.Data;
using Titano.Data.Archiving;
using Titano.Data.Jobs;
using Titano.Data.Processing;
using Titano.Protocol;
using Titano.Server.Configuration;
using Titano.Server.Events;
namespace Titano.Server.Storage;
@@ -13,19 +15,30 @@ 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).
/// MP4 jobs stay queued for render nodes (BRIEF §14.7). Renders recorded and Auto values changed here reach connected
/// clients as <c>sequenceChanged</c> events (§14.3).
/// </summary>
public static class ProcessingModule
{
public static void Register(IServiceCollection services)
{
services.AddSingleton(sp => new ProcessingService(sp.GetRequiredService<TitanoDatabase>(), new ProcessingOptions
services.AddSingleton(sp =>
{
Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner },
Encoders = [new ManagedEncoderProvider()],
Host = EncoderHost.Server,
ProducedBy = sp.GetRequiredService<ServerOptions>().ServerName,
}));
var db = sp.GetRequiredService<TitanoDatabase>();
var events = sp.GetRequiredService<EventHub>();
return new ProcessingService(db, new ProcessingOptions
{
Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner },
Encoders = [new ManagedEncoderProvider()],
Host = EncoderHost.Server,
ProducedBy = sp.GetRequiredService<ServerOptions>().ServerName,
SequenceChanged = async (id, change, ct) =>
{
if (await db.Sequences.GetAsync(id, ct) is { } sequence)
events.Publish(new SequenceChangedEvent(id, change, sequence.FramesVersion, sequence.SettingsVersion));
},
});
});
services.AddSingleton<IJobHandlerProvider>(sp => sp.GetRequiredService<ProcessingService>());
}
}
@@ -80,11 +80,11 @@ internal sealed class ArchiveFixture : IAsyncDisposable
}
/// <summary>Queues a job directly with a checkpoint, as a crashed earlier attempt would have left it.</summary>
public async Task<Job> EnqueueWithCheckpointAsync(JobEngine engine, Titano.Data.Archiving.Jobs.ArchiveJobRequest request, string checkpoint)
public async Task<Job> EnqueueWithCheckpointAsync(JobEngine engine, Titano.Data.Archiving.Jobs.ArchiveJobRequest request, string checkpoint, Guid? id = null)
{
var job = new Job
{
Id = Guid.CreateVersion7(),
Id = id ?? Guid.CreateVersion7(),
Kind = request.Kind,
TargetIds = request.Targets,
ParametersJson = request.ParametersJson,
@@ -45,11 +45,11 @@ internal sealed class ProcessingFixture : IAsyncDisposable
public static async Task<ProcessingFixture> CreateAsync(TestContext context) => new(await ArchiveFixture.CreateAsync(context), new GateDecoder());
/// <summary>A started engine with the processing handlers, and the archive's when asked (one worker, fast checkpoints).</summary>
public async Task<JobEngine> StartEngineAsync(bool withArchive = false)
/// <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)
{
_engine = new JobEngine(Db, [.. withArchive ? Archive.Service.CreateJobHandlers() : [], .. Processing.CreateJobHandlers()],
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
new JobEngineOptions { Workers = workers, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
await _engine.StartAsync(Ct);
return _engine;
}
@@ -6,6 +6,7 @@ using Titano.Core.Imaging.Decoders;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Core.Video;
using Titano.Data.Jobs;
using Titano.Data.Processing;
using Titano.Testing;
@@ -126,18 +127,13 @@ public sealed class RenderJobTests(TestContext context)
Directory.CreateDirectory(exports);
var recorded = Encoding.ASCII.GetBytes("RIFF recorded before the crash");
await File.WriteAllBytesAsync(output, recorded, fx.Ct);
await fx.Db.Renders.InsertAsync(new Render
{
Id = Guid.CreateVersion7(),
SequenceId = sequence.Id,
PresetName = "MJPEG",
OutputPath = "Synthetic/exports/clip-MJPEG.avi",
Created = DateTimeOffset.UtcNow,
}, fx.Ct);
// The crash came after the job recorded its first entry and before its checkpoint said so.
var jobId = Guid.CreateVersion7();
await fx.Db.Renders.InsertAsync(RecordedRender(sequence, Titano.Data.Processing.Jobs.RenderJobHandler.RenderId(jobId, 0)), fx.Ct);
await fx.StartEngineAsync();
var job = await fx.Archive.EnqueueWithCheckpointAsync(fx.Engine, ProcessingJobs.Render(sequence, Mjpeg with { Batch = ["gif-preview"] }, 96, 64),
new RenderCheckpoint([], 0, output).ToJson());
new RenderCheckpoint([], 0, output).ToJson(), jobId);
await fx.WaitForAsync(job.Id, JobState.Done);
var kept = await File.ReadAllBytesAsync(output, fx.Ct);
@@ -146,6 +142,74 @@ public sealed class RenderJobTests(TestContext context)
Assert.Count(2, await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task ARenderResumedAfterAnotherRecordedItsNameKeepsThatOutput()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
var exports = fx.Folder(sequence).Exports;
var output = Path.Combine(exports, "clip-MJPEG.avi");
Directory.CreateDirectory(exports);
// While this job waited to resume, another render of the same preset finished under the name it had chosen.
var other = Encoding.ASCII.GetBytes("RIFF another render's output");
await File.WriteAllBytesAsync(output, other, fx.Ct);
await fx.Db.Renders.InsertAsync(RecordedRender(sequence, Guid.CreateVersion7()), fx.Ct);
await fx.StartEngineAsync();
var job = await fx.Archive.EnqueueWithCheckpointAsync(fx.Engine, ProcessingJobs.Render(sequence, Mjpeg, 96, 64), new RenderCheckpoint([], 0, output).ToJson());
await fx.WaitForAsync(job.Id, JobState.Done);
var kept = await File.ReadAllBytesAsync(output, fx.Ct);
Assert.BytesEqual(other, kept);
Assert.SequenceEqual(["clip-MJPEG (2).avi", "clip-MJPEG.avi"], Entries(exports));
Assert.Count(2, await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task ConcurrentRendersOfOnePresetKeepBothOutputs()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
await fx.StartEngineAsync(workers: 2);
var first = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
var second = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
await fx.WaitForAsync(first.Id, JobState.Done);
await fx.WaitForAsync(second.Id, JobState.Done);
Assert.SequenceEqual(["clip-MJPEG (2).avi", "clip-MJPEG.avi"], Entries(fx.Folder(sequence).Exports));
var renders = await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct);
Assert.Equal(2, renders.Select(r => r.OutputPath).Distinct().Count());
}
[Test]
public void ANameBeingWrittenIsNotPickedAgain()
{
var folder = context.CreateTempDirectory("exports");
var name = new ExportNameContext { Date = new DateTime(2026, 6, 1), Title = "Roof", PresetName = "MJPEG", Codec = VideoCodec.Mjpeg };
using var a = new OutputNames();
using var b = new OutputNames();
var first = a.Reserve(taken => ExportNaming.OutputPath(folder, "{title}", name, taken));
var second = b.Reserve(taken => ExportNaming.OutputPath(folder, "{title}", name, taken));
Assert.Equal((Path.Combine(folder, "Roof.avi"), Path.Combine(folder, "Roof (2).avi")), (first, second));
Assert.False(b.TryReserve(first), "held by the first render");
a.Dispose();
using var c = new OutputNames();
Assert.Equal(first, c.Reserve(taken => ExportNaming.OutputPath(folder, "{title}", name, taken)));
}
[Test]
public void RenderRowIdsAreStablePerJobAndEntry()
{
var job = Guid.CreateVersion7();
var id = Titano.Data.Processing.Jobs.RenderJobHandler.RenderId(job, 0);
Assert.Equal(id, Titano.Data.Processing.Jobs.RenderJobHandler.RenderId(job, 0));
Assert.NotEqual(id, Titano.Data.Processing.Jobs.RenderJobHandler.RenderId(job, 1));
Assert.NotEqual(id, Titano.Data.Processing.Jobs.RenderJobHandler.RenderId(Guid.CreateVersion7(), 0));
Assert.Equal(8, id.Version);
}
[Test]
public async Task AServerTakesOnlyTheRendersItsManagedEncodersProduce()
{
@@ -160,6 +224,10 @@ public sealed class RenderJobTests(TestContext context)
Assert.False(Accepts(new ExportSettings { Preset = new Auto<string>().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");
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");
@@ -177,6 +245,19 @@ public sealed class RenderJobTests(TestContext context)
Assert.Equal((3840, 2160), (parameters[2].Width, parameters[2].Height));
}
/// <summary>MJPEG job parameters whose preset JSON is <paramref name="presetJson"/>.</summary>
private static string WithPreset(string presetJson) => System.Text.Json.JsonSerializer.Serialize(
new Titano.Protocol.RenderJobParameters("MJPEG", presetJson, "mjpeg", 96, 64, false, null), Titano.Protocol.ProtocolJson.Default.RenderJobParameters);
private static Render RecordedRender(Sequence sequence, Guid id) => new()
{
Id = id,
SequenceId = sequence.Id,
PresetName = "MJPEG",
OutputPath = "Synthetic/exports/clip-MJPEG.avi",
Created = DateTimeOffset.UtcNow,
};
private static List<string> Entries(string folder) =>
Directory.Exists(folder) ? [.. Directory.EnumerateFileSystemEntries(folder).Select(e => Path.GetFileName(e)).Order(StringComparer.Ordinal)] : [];
@@ -1,11 +1,17 @@
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Titano.Core.Archiving;
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Diagnostics.Synthetic;
using Titano.Core.Metadata;
using Titano.Core.Model;
using Titano.Core.Localization;
using Titano.Core.Project;
using Titano.Data.Processing;
using Titano.Protocol;
using Titano.Server.Events;
using Titano.Server.Jobs;
using Titano.Server.Storage;
using Titano.Server.Tests.Jobs;
@@ -33,6 +39,7 @@ public sealed class ProcessingModuleTests(TestContext context)
public async Task UploadedFramesAreLightAnalysedAndAdvised()
{
await using var server = await TestServer.StartAsync(context, services: Production);
using var events = server.Services.GetRequiredService<EventHub>().Subscribe();
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var sequenceId = await UploadSceneAsync(editor);
@@ -52,12 +59,15 @@ public sealed class ProcessingModuleTests(TestContext context)
var folder = new ArchivePaths(server.Options.ArchivePath).Sequence(sequence.Folder);
Assert.FileExists(folder.ProjectPath);
Assert.True(ManifestStore.ReadSequence(folder.ManifestPath).Status.HasFlag(SequenceStatus.AnalyzedLight), "sidecar status");
// Connected clients learn that the Auto values changed (BRIEF §14.3).
Assert.Contains(Drain(events), e => e is SequenceChangedEvent { Change: "settings" } changed && changed.SequenceId == sequenceId);
}
[Test]
public async Task TheServerRendersManagedFormatsAndLeavesMp4ToRenderNodes()
{
await using var server = await TestServer.StartAsync(context, services: Production);
using var events = server.Services.GetRequiredService<EventHub>().Subscribe();
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var sequence = (await server.Database.Sequences.GetAsync(await UploadSceneAsync(editor)))!;
// A client's export folder means nothing on the server: the output still goes to the sequence's exports.
@@ -81,6 +91,68 @@ public sealed class ProcessingModuleTests(TestContext context)
Assert.Empty(Directory.EnumerateFileSystemEntries(avi.Folder));
var waiting = await JobApiTests.GetAsync(editor, mp4.Id);
Assert.Equal(("queued", (string?)null), (waiting.State, waiting.NodeId));
Assert.Contains(Drain(events), e => e is SequenceChangedEvent { Change: "renders" } changed && changed.SequenceId == sequence.Id);
}
[Test]
public async Task APostedBatchBecomesOneJobPerPreset()
{
await using var server = await TestServer.StartAsync(context, services: Production);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var sequence = (await server.Database.Sequences.GetAsync(await UploadSceneAsync(editor)))!;
var batch = new ExportSettings
{
Preset = new Auto<string>().WithManual("mjpeg-avi"),
Overrides = new ExportOverrides { Width = 96, Height = 64 },
NameTemplate = "{title} - {codec}",
Batch = ["gif-preview", "1080p-h264"],
};
var main = await JobApiTests.CreateAsync(editor, "render", sequence.Id, ProcessingJobs.Render(sequence, batch, 96, 64).ParametersJson);
var jobs = (await server.Database.Jobs.ListAsync(null, JobKind.Render)).Items;
Assert.Count(3, jobs);
var mp4 = jobs.Single(j => j.TargetLabel!.EndsWith(Loc.L("Export.Preset.1080pShare"), StringComparison.Ordinal));
var gif = jobs.Single(j => j.TargetLabel!.EndsWith(Loc.L("Export.Preset.Gif"), StringComparison.Ordinal));
await JobApiTests.WaitForAsync(editor, main.Id, "done");
await JobApiTests.WaitForAsync(editor, gif.Id, "done");
// The server renders the managed presets; the MP4 one waits for a render node, at the preset's size.
Assert.SequenceEqual(["Gif", "Mjpeg"], (await server.Database.Renders.ListBySequenceAsync(sequence.Id)).Select(r => r.Codec!).Order(StringComparer.Ordinal));
Assert.Equal("queued", (await JobApiTests.GetAsync(editor, mp4.Id)).State);
var parameters = JsonSerializer.Deserialize(mp4.ParametersJson!, ProtocolJson.Default.RenderJobParameters)!;
Assert.Equal(("h264", 1920, 1080), (parameters.Codec, parameters.Width, parameters.Height));
}
[Test]
public async Task RenderJobsThatCannotBeReadNeverStopTheEngine()
{
await using var server = await TestServer.StartAsync(context, services: Production);
var (editor, user, _) = await server.UserAsync("alberto", "editor");
var sequence = (await server.Database.Sequences.GetAsync(await UploadSceneAsync(editor)))!;
const string mjpeg = """{"preset":{"isAuto":false,"manual":"mjpeg-avi"},"overrides":{"width":96,"height":64}""";
// The API refuses a preset that gives a member twice; one already queued fails with its reason.
using (var refused = await editor.PostAsJsonAsync("/api/v1/jobs",
new CreateJobRequest("render", [sequence.Id], Parameters(mjpeg + ""","Preset":{}}""")), ProtocolJson.Default.CreateJobRequest))
await refused.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_parameters");
var engine = server.Services.GetRequiredService<JobEngineHost>().Engine;
var broken = await engine.EnqueueAsync(JobKind.Render, [sequence.Id], "broken", Parameters(mjpeg + ""","Preset":{}}"""), JobPriority.User, user.Id);
var failed = await JobApiTests.WaitForAsync(editor, broken.Id, "failed");
Assert.Equal(Loc.L("Errors.Render.Parameters"), failed.Error);
// The workers carry on: a null batch is no batch.
var fine = await JobApiTests.CreateAsync(editor, "render", sequence.Id, Parameters(mjpeg + ""","batch":null}"""));
await JobApiTests.WaitForAsync(editor, fine.Id, "done");
}
private static string Parameters(string presetJson) =>
JsonSerializer.Serialize(new RenderJobParameters("MJPEG", presetJson, "mjpeg", 96, 64, false, null), ProtocolJson.Default.RenderJobParameters);
private static List<ServerEvent> Drain(EventHub.Subscription events)
{
var drained = new List<ServerEvent>();
while (events.Reader.TryRead(out var e)) drained.Add(e);
return drained;
}
/// <summary>Uploads a synthetic scene with Exif into one new sequence; returns its id.</summary>