Files
Chronos/src/Chronos.Data/Processing/RenderNodeClient.cs
T
Alby96andClaude Opus 5.5 45e6911082 feat(jobs): every job handler reports a structured progress detail
The job list showed a bar and a free-text line that each handler worded
its own way. Each handler now reports its step, the item it is on, photo
x of y and sequence/folder/output/step n of m through the P2.1 JobContext
detail. Clients can word and lay that out themselves, and the 2/s
throttle keeps the cost low: counts within a step go through
ReportItemAsync, which allocates nothing.

- scan: List, then Index (folder n of m, photo x of y as an adopted or
  reconciled folder is read) or Check, then FollowUps
- verify, thumbs, proxies, deep analysis: photo x of y of sequence n of m;
  light analysis: sequence n of m; a thumbnail/proxy checkpoint carries
  the frame count it saves
- convert: Verify (resume), Convert photo x of y, Replace; reorganize:
  step n of m with the title; import: the file being copied, photo x of y
- maintenance: the step key, step n of 5, then Check; duplicates, orphans
  and backup run with no count; cleanup names step n of m; empty trash:
  entry n of m
- render (local, workspace, render node): Analyse, Encode (frames of the
  last output, output n of m in a batch), Verify; sync, video copy,
  workspace copy/send/move: Upload/Download/CopyProxy/CopyOriginal/
  CopyVideo/Reorganize with their items and counts

The detail carries keys and names only, never localized text. The
existing Message strings stay for clients that still show them.

Refs #65

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:38:05 +01:00

670 lines
33 KiB
C#

using System.Diagnostics;
using System.Net;
using System.Text.Json;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Core.Video;
using Chronos.Data.Archiving;
using Chronos.Data.Jobs;
using Chronos.Protocol;
using Chronos.Protocol.Client;
namespace Chronos.Data.Processing;
/// <summary>Settings of a <see cref="RenderNodeClient"/>.</summary>
public sealed record RenderNodeOptions
{
/// <summary>Name the server lists the node under; one node per user and name.</summary>
public required string ClientName { get; init; }
/// <summary>Folder for downloaded originals and for outputs until they are uploaded (inside the client's cache).</summary>
public required string CacheDirectory { get; init; }
/// <summary>Downloaded originals beyond this size are deleted, least recently used first, after every job.</summary>
public long CacheLimitBytes { get; init; } = 20L << 30;
/// <summary>Wait between claims while the queue has nothing for this node.</summary>
public TimeSpan PollInterval { get; init; } = TimeSpan.FromSeconds(10);
/// <summary>Sign of life; the server gives the jobs of a node silent for 90 s to others.</summary>
public TimeSpan HeartbeatInterval { get; init; } = TimeSpan.FromSeconds(30);
/// <summary>Shortest interval between progress reports to the server.</summary>
public TimeSpan ProgressInterval { get; init; } = TimeSpan.FromSeconds(2);
/// <summary>Concurrent frame downloads.</summary>
public int Downloads { get; init; } = 4;
/// <summary>Retries of a frame download cut by the network or a busy server, each resuming the partial file.</summary>
public int DownloadRetries { get; init; } = 5;
/// <summary>Wait before the first retry; it doubles with each further one.</summary>
public TimeSpan RetryDelay { get; init; } = TimeSpan.FromSeconds(1);
}
/// <summary>
/// "Offer this PC as render node" (BRIEF §14.7): registers this machine's single-file encoders with the server,
/// heart-beats, claims one render job at a time that it can encode, downloads the frames it needs (originals
/// hash-verified into a content-addressed cache, or proxies when the job asks or this PC cannot decode the original; a
/// dropped download resumes; frames missing, corrupt or excluded on the server are left out), renders the job's preset
/// with the host's encoders and uploads the output, which the server files as a render of the sequence. Then it sends
/// the proxies the server asks for, made from the originals it fetched (BRIEF §2.3: the server cannot decode HEIF).
/// Progress and render remarks go to the server; a job cancelled or reassigned there stops here; a failure is reported
/// with its reason. Stopping abandons the job in flight without reporting it: the server requeues it when the node
/// registers again or its heartbeats stop. Image sequences are not offered and batches are refused: the protocol uploads
/// one file per job.
/// </summary>
public sealed class RenderNodeClient : IAsyncDisposable
{
private const string LogCategory = "render-node";
/// <summary>
/// Frames no node renders: absent on the server, not the archived file, or excluded. Undecodable is the server's
/// verdict on its own decoders, so this PC's decoders try those frames.
/// </summary>
internal const FrameFlags NotFetched = FrameFlags.Missing | FrameFlags.Corrupt | FrameFlags.Excluded;
private readonly ChronosClient _client;
private readonly ProcessingOptions _processing;
private readonly RenderNodeOptions _options;
private readonly CancellationTokenSource _stopping = new();
private Task _running = Task.CompletedTask;
private Guid? _nodeId;
public RenderNodeClient(ChronosClient client, ProcessingOptions processing, RenderNodeOptions options)
{
_client = client;
_processing = processing;
_options = options;
}
/// <summary>
/// The encoders this node offers, each with its largest output: every single-file capability of the host's
/// providers, hardware ones only when the user lets renders use them (<see cref="ProcessingOptions.AllowsHardware"/>).
/// </summary>
public IReadOnlyList<NodeEncoder> Encoders =>
[
.. _processing.Encoders.SelectMany(p => p.Capabilities)
.Where(c => VideoCodecInfo.DefaultContainer(c.Codec) != ContainerFormat.ImageSequence
&& (!c.Hardware || _processing.AllowsHardware(ExportResolver.CustomId)))
.Select(c => new NodeEncoder(ProcessingJobs.CodecName(c.Codec), c.Name, c.Hardware, c.SupportsTenBit, c.MaxWidth, c.MaxHeight)),
];
/// <summary>Completes when the node has stopped: asked to, or refused by the server (signed out, no render-node right,
/// changed certificate), which only the user can change.</summary>
public Task Completion => _running;
/// <summary>Starts registering, heart-beating and claiming in the background.</summary>
public void Start() => _running = Task.Run(() => RunAsync(_stopping.Token));
/// <summary>Stops claiming, abandons the job in flight and waits for the background work to end.</summary>
public async Task StopAsync()
{
await _stopping.CancelAsync().ConfigureAwait(false);
await _running.ConfigureAwait(false);
}
public async ValueTask DisposeAsync()
{
await StopAsync().ConfigureAwait(false);
_stopping.Dispose();
}
/// <summary>Registers this node (again) and returns its id.</summary>
public async Task<Guid> RegisterAsync(CancellationToken cancellationToken)
{
// Each encoder carries its own limit; the node-wide one (for servers that read only that) is the largest offered. The
// settings format the node reads keeps it from jobs it would render otherwise than asked (owner review 1, phase 4).
var encoders = Encoders;
var request = new RegisterNodeRequest(_options.ClientName, encoders, encoders.Select(e => e.MaxWidth).DefaultIfEmpty().Max(),
encoders.Select(e => e.MaxHeight).DefaultIfEmpty().Max(), ProjectSettings.CurrentVersion);
var node = await _client.RegisterNodeAsync(request, cancellationToken).ConfigureAwait(false);
_nodeId = node.Id;
return node.Id;
}
/// <summary>Claims one job this node can encode and runs it to the end; false when the queue had none.</summary>
public async Task<bool> RunOnceAsync(CancellationToken cancellationToken)
{
var nodeId = _nodeId ?? await RegisterAsync(cancellationToken).ConfigureAwait(false);
if (await _client.ClaimAsync(nodeId, cancellationToken).ConfigureAwait(false) is not { } claimed) return false;
var work = Path.Combine(_options.CacheDirectory, "jobs", claimed.Job.Id.ToString("N"));
using var job = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var reporter = new NodeReporter(_client, nodeId, claimed.Job.Id, _options.ProgressInterval, job);
try
{
Directory.CreateDirectory(work);
var input = await RenderAsync(claimed, work, reporter, job.Token).ConfigureAwait(false);
// The job is done on the server; the proxies it asked for follow, on the node's own time.
await SendProxiesAsync(claimed, input, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (job.IsCancellationRequested)
{
// Stopping, or the server cancelled or reassigned the job: either way it is no longer ours to report.
Log.Info(LogCategory, $"Render job {claimed.Job.Id} abandoned: {ex.Message}");
}
catch (Exception ex)
{
// Whatever went wrong belongs to this job: report it and stay available for the next one.
Log.Warning(LogCategory, $"Render job {claimed.Job.Id} failed", ex);
await ReportFailureAsync(nodeId, claimed.Job.Id, SequenceRender.Reason(ex), cancellationToken).ConfigureAwait(false);
}
finally
{
TryDelete(work);
TrimCache();
}
return true;
}
private async Task RunAsync(CancellationToken stopping)
{
using var heartbeat = CancellationTokenSource.CreateLinkedTokenSource(stopping);
Task? beating = null;
while (!stopping.IsCancellationRequested)
{
try
{
var nodeId = _nodeId ?? await RegisterAsync(stopping).ConfigureAwait(false);
beating ??= HeartbeatAsync(heartbeat.Token);
if (await RunOnceAsync(stopping).ConfigureAwait(false)) continue;
}
catch (OperationCanceledException) when (stopping.IsCancellationRequested)
{
break;
}
catch (Exception ex) when (ex is ChronosApiException { Status: HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden } or UntrustedCertificateException)
{
// Signed out, no render-node right, or a changed certificate: nothing to retry until the user acts.
Log.Warning(LogCategory, "The server refused this render node", ex);
break;
}
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.NotFound && _nodeId is not null)
{
_nodeId = null; // removed on the server: register again at once
continue;
}
catch (Exception ex)
{
// Server unreachable, busy or without the render-node routes: the node keeps trying until it is stopped.
Log.Warning(LogCategory, "Render node cycle failed; retrying", ex);
}
try
{
await Task.Delay(_options.PollInterval, stopping).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
break;
}
}
await heartbeat.CancelAsync().ConfigureAwait(false);
if (beating is not null) await beating.ConfigureAwait(false);
}
private async Task HeartbeatAsync(CancellationToken cancellationToken)
{
using var timer = new PeriodicTimer(_options.HeartbeatInterval);
try
{
while (await timer.WaitForNextTickAsync(cancellationToken).ConfigureAwait(false))
{
if (_nodeId is not { } nodeId) continue;
try
{
await _client.HeartbeatAsync(nodeId, cancellationToken).ConfigureAwait(false);
}
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
_nodeId = null;
}
catch (Exception ex) when (!cancellationToken.IsCancellationRequested)
{
// Unreachable or refusing: the claim loop decides whether to go on; beating must not stop the node.
Log.Warning(LogCategory, "Heartbeat failed", ex);
}
}
}
catch (OperationCanceledException)
{
}
}
/// <summary>Renders the job and uploads its output; returns the frames it read.</summary>
private async Task<SequenceFrames> RenderAsync(ClaimedJob claimed, string work, NodeReporter reporter, CancellationToken cancellationToken)
{
var export = ProcessingJobs.ReadExport(claimed.PresetJson);
var resolved = ExportResolver.Resolve(export);
var target = new ExportTarget(resolved, ProcessingJobs.PresetName(resolved));
if (target.Export.Container == ContainerFormat.ImageSequence) throw new InvalidOperationException(Loc.L("Errors.Render.NodeSequence"));
// One output per job: a server splits batches into one job per preset when they are posted.
if (ProcessingJobs.BatchPresets(export).Any()) throw new InvalidOperationException(Loc.L("Errors.Render.NodeBatch"));
var project = ProjectStore.Parse(claimed.SettingsJson) with { Export = export };
var sequence = ToSequence(await _client.GetSequenceAsync(claimed.SequenceId, cancellationToken).ConfigureAwait(false));
// The server's Undecodable stands, as in its own render, except for a format only its decoders lack (HEIF) that
// this PC's read: such a frame renders here from its original. One read from its proxy renders either way.
var decoder = _processing.Archive.Decoder;
var frames = (await ListFramesAsync(claimed.SequenceId, cancellationToken).ConfigureAwait(false))
.ConvertAll(f => ArchiveImages.ServerNeedsProxy(f.Format) && !ArchiveImages.NeedsProxy(decoder, f.Format)
? f with { Flags = f.Flags & ~FrameFlags.Undecodable }
: f);
var files = await DownloadAsync(claimed, frames, work, reporter, cancellationToken).ConfigureAwait(false);
var input = new SequenceFrames(sequence, frames, [.. frames.Select(f => files.GetValueOrDefault(f.Id).Path)])
{
Proxied = [.. frames.Select(f => files.GetValueOrDefault(f.Id).Proxy)],
};
IReadOnlyList<TargetOutcome> outcomes;
reporter.Step(JobStep.Encode, sequence.DisplayTitle);
await using (var render = new SequenceRender(input, project, null, _processing))
{
outcomes = await render.RenderAllAsync(
[(0, target)],
(t, setup) => ExportNaming.OutputPath(work, project.Export.NameTemplate, SequenceRender.NameContext(sequence, t, setup)),
(_, _) => ValueTask.CompletedTask,
async (_, t, result) =>
{
// Remarks first: completing the upload ends the job on the server.
foreach (var remark in SequenceRender.Remarks(result)) await reporter.LogAsync(remark).ConfigureAwait(false);
reporter.Step(JobStep.Upload, Path.GetFileName(result.OutputPath));
await reporter.ResendAsync().ConfigureAwait(false);
await UploadAsync(reporter.NodeId, claimed.Job.Id, t, result, cancellationToken).ConfigureAwait(false);
},
reporter.ReportAsync,
cancellationToken).ConfigureAwait(false);
}
if (outcomes[0].Error is { } error) throw new InvalidOperationException(error);
return input;
}
/// <summary>
/// Sends the proxies the server asks for (<see cref="ManifestFrame.WantsProxy"/>) that this PC can make from the
/// originals it fetched, as the server's proxy policy says; a failure is logged and never fails the finished job.
/// </summary>
private async Task SendProxiesAsync(ClaimedJob claimed, SequenceFrames input, CancellationToken cancellationToken)
{
if (claimed.Proxies is not { } spec) return;
var wanted = claimed.Frames.Where(f => f.WantsProxy).Select(f => f.Id).ToHashSet();
if (wanted.Count == 0) return;
var camera = new SidecarCamera { Make = input.Sequence.CameraMake, Model = input.Sequence.CameraModel };
int sent = 0, unsent = 0;
for (var i = 0; i < input.Count; i++)
{
var frame = input.Frames[i];
if (!wanted.Remove(frame.Id)) continue;
if (input.Proxied?[i] == true || input.Files[i] is not { } original)
{
unsent++;
continue;
}
try
{
var jpeg = await Task.Run(async () =>
{
await Task.Yield();
return ArchiveImages.MakeProxy(_processing.Archive.Decoder, original, frame, camera, spec.LongEdge, spec.Quality, cancellationToken);
}, cancellationToken).ConfigureAwait(false);
if (jpeg is null)
{
unsent++;
continue;
}
await _client.UploadProxyAsync(frame.Id, jpeg, cancellationToken).ConfigureAwait(false);
sent++;
}
catch (ChronosApiException ex) when (ex.Status is HttpStatusCode.Conflict or HttpStatusCode.Forbidden)
{
// Proxies turned off on the server, or this account may no longer send them: none of the rest would go either.
Log.Warning(LogCategory, $"The server refused the proxies of job {claimed.Job.Id}", ex);
return;
}
catch (Exception ex) when (!cancellationToken.IsCancellationRequested)
{
// Best effort (a timed-out upload included): the job is done, and a proxy the server still lacks is asked for
// again with the next job.
Log.Warning(LogCategory, $"The proxy of {frame.FileName} could not be sent", ex);
unsent++;
}
}
Log.Info(LogCategory, $"Proxies of job {claimed.Job.Id}: {sent} sent, {unsent + wanted.Count} not");
}
private async Task UploadAsync(Guid nodeId, Guid jobId, ExportTarget target, RenderResult result, CancellationToken cancellationToken)
{
var size = new FileInfo(result.OutputPath).Length;
var sha = await ChronosClient.HashFileAsync(result.OutputPath, cancellationToken).ConfigureAwait(false);
var request = new RenderResultRequest(Path.GetFileName(result.OutputPath), size, sha, target.PresetName, ProcessingJobs.CodecName(result.Codec),
result.Width, result.Height, result.FrameRate.Value, result.DurationSeconds);
await _client.UploadRenderResultAsync(nodeId, jobId, result.OutputPath, request, null, cancellationToken).ConfigureAwait(false);
}
private async Task<List<Frame>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
{
var frames = new List<Frame>();
string? cursor = null;
do
{
var page = await _client.ListFramesAsync(sequenceId, cursor, 1000, cancellationToken).ConfigureAwait(false);
frames.AddRange(page.Items.Select(ToFrame));
cursor = page.NextCursor;
}
while (cursor is not null);
frames.Sort((a, b) => a.Index.CompareTo(b.Index));
return frames;
}
/// <summary>
/// The files of the job's frames (none for frames missing, corrupt or excluded on the server): originals verified
/// against their SHA-256 into the content-addressed cache (reused across jobs, fetched once per content), originals the
/// server has not hashed yet into the job folder, or proxies into the job folder when the job asks for them or this PC
/// cannot decode the original, and the server has them.
/// </summary>
private async Task<Dictionary<Guid, (string Path, bool Proxy)>> DownloadAsync(ClaimedJob claimed, IReadOnlyList<Frame> rows, string work,
NodeReporter reporter, CancellationToken cancellationToken)
{
var formats = rows.ToDictionary(f => f.Id, f => f.Format);
var undecodable = rows.Where(f => (f.Flags & FrameFlags.Undecodable) != 0).Select(f => f.Id).ToHashSet();
bool Proxy(ManifestFrame f) =>
f.HasProxy && (claimed.UseProxies || (formats.TryGetValue(f.Id, out var format) && ArchiveImages.NeedsProxy(_processing.Archive.Decoder, format)));
var files = new Dictionary<Guid, (string, bool)>();
// An original that stays undecodable would only be skipped by the render.
var fetched = claimed.Frames.Where(f => ((FrameFlags)f.Flags & NotFetched) == 0 && (Proxy(f) || !undecodable.Contains(f.Id))).ToList();
var done = 0;
var total = fetched.Count;
// The log line stays for servers that show no detail.
reporter.Step(JobStep.Download, null);
await reporter.ReportAsync(0, total, Loc.L("Export.Job.Downloading")).ConfigureAwait(false);
var downloads = fetched.GroupBy(f => Proxy(f) || f.Sha256.Length == 0 ? f.Id.ToString("N") : f.Sha256.ToLowerInvariant()).ToList();
await Parallel.ForEachAsync(downloads, new ParallelOptions { MaxDegreeOfParallelism = _options.Downloads, CancellationToken = cancellationToken },
async (frames, ct) =>
{
var frame = frames.First();
var file = Proxy(frame)
? await DownloadProxyAsync(frame, work, ct).ConfigureAwait(false)
: (await DownloadOriginalAsync(frame, work, ct).ConfigureAwait(false), false);
lock (files)
{
foreach (var f in frames) files[f.Id] = file;
}
await reporter.ReportAsync(Interlocked.Add(ref done, frames.Count()), total, null).ConfigureAwait(false);
}).ConfigureAwait(false);
return files;
}
/// <summary>
/// A proxy into the job folder (regenerable, so neither cached nor hashed: the server keeps no hash of it, and a retry
/// fetches it whole, since a proxy rebuilt meanwhile must not be resumed from the old one); the original when the server
/// has none for the frame.
/// </summary>
private async Task<(string Path, bool Proxy)> DownloadProxyAsync(ManifestFrame frame, string work, CancellationToken cancellationToken)
{
var path = Path.Combine(work, "proxies", frame.Id.ToString("N") + ".jpg");
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
try
{
await FetchAsync(frame, "proxy", path, resume: false, cancellationToken).ConfigureAwait(false);
return (path, true);
}
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
File.Delete(path);
return (await DownloadOriginalAsync(frame, work, cancellationToken).ConfigureAwait(false), false);
}
}
/// <summary>
/// A cached copy, or a download (resumed from a partial one) that must hash to the manifest's SHA-256; an original the
/// server has not hashed yet goes into the job folder and must have the manifest's size.
/// </summary>
private async Task<string> DownloadOriginalAsync(ManifestFrame frame, string work, CancellationToken cancellationToken)
{
if (frame.Sha256.Length == 0) return await DownloadUnhashedAsync(frame, work, cancellationToken).ConfigureAwait(false);
var sha = frame.Sha256.ToLowerInvariant();
if (sha.Length != 64 || !sha.All(char.IsAsciiHexDigitLower)) throw new InvalidDataException(Loc.L("Errors.Render.Download", frame.FileName));
var path = Path.Combine(_options.CacheDirectory, "originals", sha[..2], sha + Path.GetExtension(frame.FileName).ToLowerInvariant());
if (File.Exists(path))
{
File.SetLastWriteTimeUtc(path, DateTime.UtcNow); // most recently used: kept by the cache trim
return path;
}
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
var part = path + ".part";
for (var attempt = 1; ; attempt++)
{
// A part as long as the file was complete when the last attempt stopped: only its hash is left to check.
if (!File.Exists(part) || new FileInfo(part).Length < frame.Size) await FetchAsync(frame, "original", part, resume: true, cancellationToken).ConfigureAwait(false);
if (await ChronosClient.HashFileAsync(part, cancellationToken).ConfigureAwait(false) == sha)
{
File.Move(part, path, overwrite: true);
return path;
}
File.Delete(part);
if (attempt == 2) throw new InvalidDataException(Loc.L("Errors.Render.Download", frame.FileName));
}
}
/// <summary>
/// An original without a SHA-256 on the server (adopted, not verified yet): nothing to check it against but its size,
/// so it is neither cached nor shared with other frames; a copy of another size is fetched again once.
/// </summary>
private async Task<string> DownloadUnhashedAsync(ManifestFrame frame, string work, CancellationToken cancellationToken)
{
var path = Path.Combine(work, "originals", frame.Id.ToString("N") + Path.GetExtension(frame.FileName).ToLowerInvariant());
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
for (var attempt = 1; ; attempt++)
{
if (!File.Exists(path) || new FileInfo(path).Length < frame.Size) await FetchAsync(frame, "original", path, resume: true, cancellationToken).ConfigureAwait(false);
if (new FileInfo(path).Length == frame.Size) return path;
File.Delete(path);
if (attempt == 2) throw new InvalidDataException(Loc.L("Errors.Render.Download", frame.FileName));
}
}
/// <summary>
/// Fetches a frame resource into <paramref name="path"/>. A dropped connection or a busy server is retried (up to
/// <see cref="RenderNodeOptions.DownloadRetries"/> times, the wait doubling from <see cref="RenderNodeOptions.RetryDelay"/>);
/// with <paramref name="resume"/> (a download its hash verifies) each attempt appends to what the last one left,
/// otherwise it starts over.
/// </summary>
private async Task FetchAsync(ManifestFrame frame, string kind, string path, bool resume, CancellationToken cancellationToken)
{
for (var attempt = 0; ; attempt++)
{
try
{
await using var file = new FileStream(path, resume ? FileMode.OpenOrCreate : FileMode.Create, FileAccess.ReadWrite, FileShare.None);
await _client.DownloadFrameAsync(frame.Id, kind, file, null, cancellationToken).ConfigureAwait(false);
return;
}
catch (Exception ex) when (attempt < _options.DownloadRetries && !cancellationToken.IsCancellationRequested && IsTransient(ex))
{
Log.Info(LogCategory, $"Download of {frame.FileName} ({kind}) interrupted, resuming: {ex.Message}");
await Task.Delay(_options.RetryDelay * (1 << attempt), cancellationToken).ConfigureAwait(false);
}
}
}
/// <summary>Failures a later attempt can get past: the network, a busy server, a timeout (not a cancellation of ours).</summary>
private static bool IsTransient(Exception ex) =>
ex is HttpRequestException or IOException or OperationCanceledException or ChronosApiException { IsTransient: true };
private async Task ReportFailureAsync(Guid nodeId, Guid jobId, string error, CancellationToken cancellationToken)
{
try
{
await _client.ReportNodeFailureAsync(nodeId, jobId, error, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is ChronosApiException or HttpRequestException or IOException or OperationCanceledException)
{
// Cancelled or reassigned meanwhile, or the server is gone: it requeues the job when the heartbeats stop.
Log.Warning(LogCategory, $"The failure of job {jobId} could not be reported", ex);
}
}
/// <summary>Deletes the least recently used originals until the cache fits its limit.</summary>
private void TrimCache()
{
try
{
var root = new DirectoryInfo(Path.Combine(_options.CacheDirectory, "originals"));
if (!root.Exists) return;
var files = root.EnumerateFiles("*", SearchOption.AllDirectories).OrderBy(f => f.LastWriteTimeUtc).ToList();
var size = files.Sum(f => f.Length);
foreach (var file in files)
{
if (size <= _options.CacheLimitBytes) break;
size -= file.Length;
file.Delete();
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning(LogCategory, "The render node cache could not be trimmed", ex);
}
}
private static void TryDelete(string folder)
{
try
{
if (Directory.Exists(folder)) Directory.Delete(folder, recursive: true);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning(LogCategory, $"Could not remove {folder}", ex);
}
}
internal static Sequence ToSequence(SequenceDto s) => new()
{
Id = s.Id,
ArchiveId = Guid.Empty,
Folder = s.Folder,
Title = s.Title,
CameraMake = s.CameraMake,
CameraModel = s.CameraModel,
Lens = s.Lens,
FirstCapture = s.FirstCapture,
LastCapture = s.LastCapture,
FrameCount = s.FrameCount,
};
internal static Frame ToFrame(FrameDto f) => new()
{
Id = f.Id,
SequenceId = f.SequenceId,
Index = f.Index,
RelativePath = f.FileName,
FileName = f.FileName,
Size = f.Size,
Sha256 = f.Sha256,
CapturedAt = f.CapturedAt,
UtcOffsetMinutes = f.UtcOffsetMinutes,
ExposureTime = f.ExposureTime,
FNumber = f.FNumber,
Iso = f.Iso,
FocalLength = f.FocalLength,
Orientation = f.Orientation,
Width = f.Width,
Height = f.Height,
Format = Enum.TryParse<ImageFileFormat>(f.Format, ignoreCase: true, out var format) ? format : default,
Flags = (FrameFlags)f.Flags,
PreviewWidth = f.PreviewWidth,
PreviewHeight = f.PreviewHeight,
};
/// <summary>
/// Progress of the job in flight to the server, at most every interval (and at the end), with what the node does now
/// (<see cref="NodeProgress.Detail"/>: frames downloaded, encoded, the output uploaded; a server that shows no detail
/// ignores it). A refusal (409) means the job was cancelled or given to another node: the job's token is cancelled.
/// </summary>
private sealed class NodeReporter(ChronosClient client, Guid nodeId, Guid jobId, TimeSpan interval, CancellationTokenSource job)
{
private JobStep _step;
private string? _item;
private bool _stepChanged;
private readonly Stopwatch _clock = Stopwatch.StartNew();
private readonly SemaphoreSlim _gate = new(1, 1);
private TimeSpan _last = TimeSpan.MinValue;
private long _lastDone;
private long _lastTotal;
private string? _lastMessage;
public Guid NodeId => nodeId;
/// <summary>What the node does from now on; its next report is sent at once.</summary>
public void Step(JobStep step, string? item)
{
_gate.Wait(CancellationToken.None);
try
{
(_step, _item, _stepChanged) = (step, item, true);
}
finally
{
_gate.Release();
}
}
/// <summary>Progress, sent at most every interval; a stage message is sent when it changes.</summary>
public ValueTask ReportAsync(long done, long total, string? message) => SendAsync(done, total, message, remark: false);
/// <summary>The progress last reported, sent again with the step set since.</summary>
public ValueTask ResendAsync() => SendAsync(-1, -1, null, remark: false);
/// <summary>A line for the job's log on the server, sent with the progress last reported.</summary>
public ValueTask LogAsync(string line) => SendAsync(0, 0, line, remark: true);
private async ValueTask SendAsync(long done, long total, string? message, bool remark)
{
await _gate.WaitAsync(CancellationToken.None).ConfigureAwait(false);
try
{
NodeProgress progress;
if (remark)
{
progress = new NodeProgress(_lastDone, _lastTotal, null, message);
}
else
{
if (done < 0) (done, total) = (_lastDone, _lastTotal);
var now = _clock.Elapsed;
var line = message is not null && message != _lastMessage ? message : null;
if (line is null && !_stepChanged && done < total && now - _last < interval) return;
double? rate = _last == TimeSpan.MinValue || done < _lastDone ? null : (done - _lastDone) / double.Max((now - _last).TotalSeconds, 1e-3);
(_last, _lastDone, _lastTotal, _stepChanged) = (now, done, total, false);
if (line is not null) _lastMessage = line;
var detail = _step == JobStep.None ? null
: new JobDetailDto(JobWire.Step(_step), _item is { Length: > JobDetail.MaxItemLength } item ? item[..JobDetail.MaxItemLength] : _item,
_step == JobStep.Upload ? 0 : done, _step == JobStep.Upload ? 0 : total);
progress = new NodeProgress(done, total, rate, line, detail);
}
await client.ReportNodeProgressAsync(nodeId, jobId, progress, job.Token).ConfigureAwait(false);
}
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.Conflict)
{
await job.CancelAsync().ConfigureAwait(false);
}
catch (Exception ex) when (ex is ChronosApiException or HttpRequestException or IOException)
{
Log.Warning(LogCategory, "Progress could not be reported", ex);
}
catch (OperationCanceledException)
{
// The job is stopping; its outcome is handled by the caller.
}
finally
{
_gate.Release();
}
}
}
}