Renders now record their SHA-256 and kind, so copies, deletes and restores can be verified. The new videos library lists every render with paging and filters, deletes into the archive trash after a dry run whose fingerprint must still match (journalled, so a crash mid-move is settled on open), restores by id refusing a tampered file, and a VideoCopy job copies renders hash-verified into a videos folder outside the archive, never overwriting. A still job saves one frame as an image through an interim Data-side path until P1's Core still API lands. Batch exports keep the playback and routing uses the job's own export, so a playback job needs settings format 3 and an older node never claims it. Refs #21 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
132 lines
6.9 KiB
C#
132 lines
6.9 KiB
C#
using System.Globalization;
|
|
using System.Text;
|
|
using Chronos.Core.Model;
|
|
|
|
namespace Chronos.Data.Jobs;
|
|
|
|
/// <summary>How much a kind of job counts in the priority queue (<see cref="JobPriorityQueue"/>).</summary>
|
|
public enum JobKindPriority
|
|
{
|
|
Low,
|
|
Normal,
|
|
High,
|
|
}
|
|
|
|
/// <summary>
|
|
/// The priority queue of Settings (owner review 1, R2). Jobs always run one at a time. Off, they run in claim order
|
|
/// (priority, then age) and a running job is never interrupted: the user pauses it to let the next one start. On, a kind
|
|
/// of a higher <see cref="JobKindPriority"/> runs first, and a queued job of a higher level that the engine can run makes
|
|
/// the running job of a lower level yield without asking: it goes back to the queue with its checkpoint and resumes by
|
|
/// itself once nothing above it waits. Kinds of the same level never interrupt each other.
|
|
/// </summary>
|
|
public sealed record JobPriorityQueue
|
|
{
|
|
/// <summary>
|
|
/// The levels a kind has unless the user changed it: renders, imports and downloads first, and the copy of a project's
|
|
/// media into the workspace, which Edit waits for (owner review 2, R8), a workspace's local render and the move of the
|
|
/// workspace folder, which Edit is held for (phase 5c), and the copy of videos into the videos folder, a transfer the user
|
|
/// waits for like a download (owner review 1, R9); a local render's sending home at the normal level; the long
|
|
/// passes that resume anywhere, and the housekeeping of the schedules (backups, clean-ups), last.
|
|
/// </summary>
|
|
public static IReadOnlyDictionary<JobKind, JobKindPriority> DefaultLevels { get; } = new Dictionary<JobKind, JobKindPriority>
|
|
{
|
|
[JobKind.Render] = JobKindPriority.High,
|
|
[JobKind.Import] = JobKindPriority.High,
|
|
[JobKind.Download] = JobKindPriority.High,
|
|
[JobKind.WorkspaceCopy] = JobKindPriority.High,
|
|
[JobKind.WorkspaceRender] = JobKindPriority.High,
|
|
[JobKind.WorkspaceSend] = JobKindPriority.Normal,
|
|
[JobKind.WorkspaceMove] = JobKindPriority.High,
|
|
[JobKind.VideoCopy] = JobKindPriority.High,
|
|
[JobKind.Verify] = JobKindPriority.Low,
|
|
[JobKind.Proxies] = JobKindPriority.Low,
|
|
[JobKind.Backup] = JobKindPriority.Low,
|
|
[JobKind.Cleanup] = JobKindPriority.Low,
|
|
};
|
|
|
|
/// <summary>The strict queue: no levels, no interruptions.</summary>
|
|
public static JobPriorityQueue Off { get; } = new();
|
|
|
|
/// <summary>Whether levels order the queue and interrupt lower jobs.</summary>
|
|
public bool Enabled { get; init; }
|
|
|
|
/// <summary>Levels the user set; a kind not listed has its <see cref="DefaultLevels"/> level, else normal.</summary>
|
|
public IReadOnlyDictionary<JobKind, JobKindPriority> Levels { get; init; } = new Dictionary<JobKind, JobKindPriority>();
|
|
|
|
/// <summary>The level of <paramref name="kind"/>.</summary>
|
|
public JobKindPriority LevelOf(JobKind kind) =>
|
|
Levels.TryGetValue(kind, out var level) || DefaultLevels.TryGetValue(kind, out level) ? level : JobKindPriority.Normal;
|
|
|
|
/// <summary>Whether a queued job of <paramref name="challenger"/> interrupts a running job of <paramref name="running"/>.</summary>
|
|
public bool Interrupts(JobKind challenger, JobKind running) => Enabled && LevelOf(challenger) > LevelOf(running);
|
|
|
|
/// <summary>
|
|
/// Whether <paramref name="a"/> is claimed before <paramref name="b"/>: the higher level first while the queue is on,
|
|
/// then the higher priority, the older job and the lower id (<see cref="Repositories.JobRepository.ClaimNextAsync"/>).
|
|
/// </summary>
|
|
public bool ClaimsBefore(Job a, Job b)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(a);
|
|
ArgumentNullException.ThrowIfNull(b);
|
|
if (Enabled && LevelOf(a.Kind) != LevelOf(b.Kind)) return LevelOf(a.Kind) > LevelOf(b.Kind);
|
|
return a.Priority != b.Priority ? a.Priority > b.Priority : a.Created != b.Created ? a.Created < b.Created : a.Id.CompareTo(b.Id) < 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The SQL ordering of the queue (before priority, age and id): the levels of <paramref name="kinds"/>, highest first, as
|
|
/// a <c>CASE</c> over the kind column; empty while the queue is off or every kind has the same level.
|
|
/// </summary>
|
|
internal string OrderSql(IEnumerable<JobKind> kinds)
|
|
{
|
|
if (!Enabled) return string.Empty;
|
|
var levels = kinds.Distinct().GroupBy(LevelOf).Where(g => g.Key != JobKindPriority.Normal).ToList();
|
|
if (levels.Count == 0) return string.Empty;
|
|
var sql = new StringBuilder("CASE");
|
|
foreach (var level in levels)
|
|
{
|
|
sql.Append(" WHEN kind IN (").AppendJoin(", ", level.Select(k => ((int)k).ToString(CultureInfo.InvariantCulture))).Append(") THEN ")
|
|
.Append(((int)level.Key).ToString(CultureInfo.InvariantCulture));
|
|
}
|
|
return sql.Append(" ELSE ").Append(((int)JobKindPriority.Normal).ToString(CultureInfo.InvariantCulture)).Append(" END DESC, ").ToString();
|
|
}
|
|
|
|
/// <summary>Wire name of a level in API v1 and settings files: <c>high</c>, <c>normal</c>, <c>low</c>.</summary>
|
|
public static string Wire(JobKindPriority level) => level.ToString().ToLowerInvariant();
|
|
|
|
/// <summary>The level named <paramref name="text"/> (<see cref="Wire"/>, any case); false for anything else.</summary>
|
|
public static bool TryParse(string? text, out JobKindPriority level)
|
|
{
|
|
foreach (var candidate in Enum.GetValues<JobKindPriority>())
|
|
{
|
|
if (!string.Equals(text, Wire(candidate), StringComparison.OrdinalIgnoreCase)) continue;
|
|
level = candidate;
|
|
return true;
|
|
}
|
|
level = JobKindPriority.Normal;
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The queue from settings: <paramref name="levels"/> maps kind names (<see cref="JobWire.Kind"/> or the enum name) to
|
|
/// level names; unknown kinds and levels are skipped, so a settings file written by a newer build still loads.
|
|
/// </summary>
|
|
public static JobPriorityQueue From(bool enabled, IReadOnlyDictionary<string, string>? levels)
|
|
{
|
|
var parsed = new Dictionary<JobKind, JobKindPriority>();
|
|
foreach (var (name, value) in levels ?? new Dictionary<string, string>())
|
|
{
|
|
if ((JobWire.TryParseKind(name, out var kind) || Enum.TryParse(name, ignoreCase: true, out kind)) && Enum.IsDefined(kind) && TryParse(value, out var level))
|
|
parsed[kind] = level;
|
|
}
|
|
return new JobPriorityQueue { Enabled = enabled, Levels = parsed };
|
|
}
|
|
|
|
/// <summary>The levels that differ from <see cref="DefaultLevels"/>, by wire kind name: what settings store.</summary>
|
|
public static Dictionary<string, string> Changed(IReadOnlyDictionary<JobKind, JobKindPriority> levels)
|
|
{
|
|
var defaults = new JobPriorityQueue();
|
|
return levels.Where(l => defaults.LevelOf(l.Key) != l.Value).OrderBy(l => l.Key)
|
|
.ToDictionary(l => JobWire.Kind(l.Key), l => Wire(l.Value), StringComparer.Ordinal);
|
|
}
|
|
}
|