using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Jobs;
using Chronos.Data.Processing;
using Chronos.Data.Projects;
using Chronos.Protocol;
using Chronos.Server.Jobs;
using Chronos.Server.Security;
using Chronos.Server.Storage;
namespace Chronos.Server.Api;
///
/// /api/v1/jobs (BRIEF §14.6): queue and history, creation (a render batch becomes one job per preset; a render of a
/// project version is checked against the sequence's projects, owner review 2, R8), cancel, pause, resume, retry and
/// priority. Changes reach WebSocket clients as jobState and jobProgress events.
///
public static class JobEndpoints
{
/// Kinds that work on sequences (targets are sequence ids) and need the edit right.
private static readonly HashSet SequenceKinds = [JobKind.LightAnalyse, JobKind.DeepAnalyse, JobKind.Proxies, JobKind.Thumbs, JobKind.Verify];
/// Archive-wide maintenance (target: the archive) for administrators.
private static readonly HashSet ArchiveKinds = [JobKind.Scan, JobKind.Rebuild, JobKind.Maintenance, JobKind.Duplicates, JobKind.Orphans];
public static void Map(RouteGroupBuilder api)
{
var jobs = api.MapGroup("/jobs").Requires(Permissions.Read);
jobs.MapGet("", ListAsync);
jobs.MapPost("", CreateAsync);
jobs.MapGet("/{id:guid}", async (HttpContext context, Guid id, ChronosDatabase db) =>
await db.Jobs.GetAsync(id, context.RequestAborted) is { } job
? Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto)
: ApiErrors.NotFound(context, "job"));
jobs.MapPost("/{id:guid}/cancel", (HttpContext c, Caller caller, Guid id, ChronosDatabase db, JobEngineHost host) =>
ActAsync(c, caller, id, db, [JobState.Queued, JobState.Running, JobState.Paused], (job, ct) => host.Engine.CancelAsync(job.Id, ct)));
jobs.MapPost("/{id:guid}/pause", (HttpContext c, Caller caller, Guid id, ChronosDatabase db, JobEngineHost host) =>
ActAsync(c, caller, id, db, [JobState.Queued, JobState.Running], (job, ct) => host.Engine.PauseAsync(job.Id, ct)));
jobs.MapPost("/{id:guid}/resume", (HttpContext c, Caller caller, Guid id, ChronosDatabase db, JobEngineHost host) =>
ActAsync(c, caller, id, db, [JobState.Paused], (job, ct) => host.Engine.ResumeAsync(job.Id, ct)));
jobs.MapPost("/{id:guid}/retry", (HttpContext c, Caller caller, Guid id, ChronosDatabase db, JobEngineHost host) =>
ActAsync(c, caller, id, db, [JobState.Failed, JobState.Cancelled], (job, ct) => host.Engine.ResumeAsync(job.Id, ct)));
jobs.MapPost("/{id:guid}/priority", PriorityAsync);
}
private static async Task ListAsync(HttpContext context, ChronosDatabase db, string? state, string? kind, string? cursor, int? limit)
{
JobState? stateFilter = null;
JobKind? kindFilter = null;
if (!string.IsNullOrEmpty(state))
{
if (!JobWire.TryParseState(state, out var s)) return ApiErrors.BadRequest(context, "invalid_filter", "state");
stateFilter = s;
}
if (!string.IsNullOrEmpty(kind))
{
if (!JobWire.TryParseKind(kind, out var k)) return ApiErrors.BadRequest(context, "invalid_filter", "kind");
kindFilter = k;
}
// A job's answer holds no parameters (a conversion's or an import's can name every sequence or file): the list reads none.
var page = await db.Jobs.ListBriefAsync(stateFilter, kindFilter, limit ?? 50, string.IsNullOrEmpty(cursor) ? null : cursor, context.RequestAborted);
return Results.Json(new Page([.. page.Items.Select(Dto.Job)], page.NextCursor, null), ProtocolJson.Default.PageJobDto);
}
private static async Task CreateAsync(HttpContext context, Caller caller, CreateJobRequest request, ChronosDatabase db, JobEngineHost host,
ServerArchive archive, AuditLog audit, [FromServices] ArchiveService? archives)
{
var ct = context.RequestAborted;
if (!JobWire.TryParseKind(request.Kind, out var kind) || !(kind == JobKind.Render || SequenceKinds.Contains(kind) || ArchiveKinds.Contains(kind)))
return ApiErrors.BadRequest(context, "invalid_job_kind");
var needed = kind == JobKind.Render ? Permissions.Render : SequenceKinds.Contains(kind) ? Permissions.Edit : Permissions.ManageArchive;
if (!caller.Has(needed)) return ApiErrors.Forbidden(context);
if (!host.CanRun(kind)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(kind));
IReadOnlyList targets;
string label;
if (ArchiveKinds.Contains(kind))
{
targets = [archive.Id];
label = archive.Current.Name;
}
else
{
var ids = request.Target ?? [];
if (ids.Count == 0 || (kind == JobKind.Render && ids.Count != 1)) return ApiErrors.BadRequest(context, "invalid_parameters", "target");
var titles = new List(ids.Count);
foreach (var id in ids)
{
if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
titles.Add(sequence.DisplayTitle);
}
targets = ids;
label = titles.Count == 1 ? titles[0] : string.Join(", ", titles.Take(3)) + (titles.Count > 3 ? " …" : string.Empty);
}
List<(string Label, string? Parameters)> queue = [(label, request.ParametersJson)];
if (kind == JobKind.Render)
{
if (ParseRender(request.ParametersJson) is not { } parsed) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
// A render of a project version (owner review 2, R8): of the sequence's projects, a project alone pinned to its head.
var (render, missing) = await ProjectRenderAsync(db, archives, targets[0], parsed, ct);
if (missing is not null) return ApiErrors.NotFound(context, missing);
// A batch becomes one job per preset: a render node uploads one file per job (BRIEF §14.7). Each gets the size and the
// settings format of the settings it renders (owner review 1, phase 4), so it goes only to a node that renders it as asked.
IReadOnlyList each;
try
{
var (width, height) = await SourceSizeAsync(db, targets[0], render, ct);
each = ProcessingJobs.Split(render, width, height, await RenderedSettingsAsync(db, targets[0], render, ct));
}
catch (InvalidDataException)
{
return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
}
queue = each is [var one] && one == parsed
? [(ProcessingJobs.RenderLabel(label, render.PresetName), request.ParametersJson)]
: [.. each.Select(p => (ProcessingJobs.RenderLabel(label, p.PresetName), (string?)JsonSerializer.Serialize(p, ProtocolJson.Default.RenderJobParameters)))];
}
else if (request.ParametersJson is not null && !IsJsonObject(request.ParametersJson))
{
return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
}
if (kind == JobKind.Verify)
{
// A verify of sequences targets their archive and names them in its parameters, as its handler reads it.
var verify = LibraryEndpoints.SequenceJob(kind, archive.Current, targets, label);
targets = verify.Targets;
queue = [(label, verify.ParametersJson)];
}
Job? first = null;
foreach (var (jobLabel, parameters) in queue)
{
var job = await host.Engine.EnqueueAsync(kind, targets, jobLabel, parameters, JobPriority.User, caller.Id, ct);
await audit.WriteAsync(caller, context, "job_created", "job", job.Id.ToString(), $"{JobWire.Kind(kind)} · {jobLabel}");
first ??= job;
}
return Results.Json(Dto.Job(first!), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted);
}
///
/// The render of a project version (owner review 2, R8), checked against the sequence:
/// must be a version of one of its projects (that project, when names one too) and
/// fills in the project; a live project alone, without settings of its own, renders its head as it is now, which the job then
/// names, so the render records the version it rendered; embedded settings with a project render as they are, recording
/// the project. A deleted project's versions still render (they are kept). Without either, the parameters as they are.
/// Returns what is missing (project, version) instead of the parameters when they do not hold.
///
internal static async Task<(RenderJobParameters Parameters, string? Missing)> ProjectRenderAsync(ChronosDatabase db, ArchiveService? archives, Guid sequenceId,
RenderJobParameters render, CancellationToken ct)
{
if (render.ProjectId is null && render.ProjectVersionId is null) return (render, null);
var projects = LibraryEndpoints.Engine(archives, db).Projects;
ProjectVersion? version = null;
if (render.ProjectVersionId is { } versionId && (version = await KnownVersionAsync(versionId)) is null) return (render, "version");
var projectId = render.ProjectId ?? version!.ProjectId;
if (await db.Projects.GetAsync(projectId, ct) is not { } project)
{
// A project the index does not know yet may be in the sequence's sidecars: listing reads them first.
await projects.ListAsync(sequenceId, ct);
project = await db.Projects.GetAsync(projectId, ct);
}
if (project is null || project.SequenceId != sequenceId) return (render, "project");
if (version is not null) return version.ProjectId == project.Id ? (render with { ProjectId = project.Id }, null) : (render, "version");
if (render.SettingsJson is not null) return (render with { ProjectId = project.Id }, null);
try
{
// Caught up first: Main's head is the sequence's settings as they are now.
var head = await projects.HeadAsync(project.Id, ct);
return (render with { ProjectId = project.Id, ProjectVersionId = head.Id }, null);
}
catch (ProjectException)
{
// A deleted project has no head to render; its versions, named by id, still render (they are kept).
return (render, project.DeletedAt is null ? "version" : "project");
}
async Task KnownVersionAsync(Guid id)
{
if (await db.Projects.GetVersionAsync(id, ct) is { } known) return known;
await projects.ListAsync(sequenceId, ct);
return await db.Projects.GetVersionAsync(id, ct);
}
}
///
/// The settings a render job renders, for its size and settings format: those it embeds, else the project version's it
/// names, else the sequence's own; a newer Chronos's too (their format stands). Null when they cannot be read (or the
/// version is not there): the job keeps the size it was posted with, and its render says why it cannot read them.
///
internal static async Task RenderedSettingsAsync(ChronosDatabase db, Guid sequenceId, RenderJobParameters render, CancellationToken ct)
{
if (render.SettingsJson is { } embedded) return ProcessingJobs.ReadSettings(embedded);
if (render.ProjectVersionId is { } versionId)
return await db.Projects.GetVersionAsync(versionId, ct) is { } version ? ProcessingJobs.ReadSettings(version.SettingsJson) : null;
if (await db.Sequences.GetAsync(sequenceId, ct) is not { } sequence) return null;
return sequence.SettingsJson is null ? new ProjectSettings() : ProcessingJobs.ReadSettings(sequence.SettingsJson);
}
/// The oriented size of the sequence's first frame (what a preset without a fixed size renders at), else the job's size.
internal static async Task<(int Width, int Height)> SourceSizeAsync(ChronosDatabase db, Guid sequenceId, RenderJobParameters render, CancellationToken ct) =>
(await db.Frames.ListBySequenceAsync(sequenceId, limit: 1, cancellationToken: ct)).FirstOrDefault() is { Width: > 0, Height: > 0 } frame
? frame.Orientation is >= 5 and <= 8 ? (frame.Height, frame.Width) : (frame.Width, frame.Height)
: (render.Width, render.Height);
private static async Task PriorityAsync(HttpContext context, Caller caller, Guid id, JobPriorityRequest request, ChronosDatabase db, JobEngineHost host)
{
if (!JobWire.TryParsePriority(request.Priority, out var priority)) return ApiErrors.BadRequest(context, "invalid_priority");
return await ActAsync(context, caller, id, db, [JobState.Queued, JobState.Paused], async (job, ct) =>
{
await db.Jobs.SetPriorityAsync(job.Id, priority, ct);
host.Publish(job with { Priority = priority });
});
}
/// Applies when the job is in one of states and the caller created it or administers the archive.
private static async Task ActAsync(HttpContext context, Caller caller, Guid id, ChronosDatabase db, JobState[] allowed, Func action)
{
var ct = context.RequestAborted;
if (await db.Jobs.GetAsync(id, ct) is not { } job) return ApiErrors.NotFound(context, "job");
if (job.CreatedBy != caller.Id && !caller.Has(Permissions.ManageArchive)) return ApiErrors.Forbidden(context);
if (!allowed.Contains(job.State)) return ApiErrors.Conflict(context, "invalid_state", JobWire.State(job.State));
await action(job, ct);
var updated = await db.Jobs.GetAsync(id, ct) ?? job;
return Results.Json(Dto.Job(updated), ProtocolJson.Default.JobDto);
}
/// Render parameters, or null when they are missing or incomplete.
public static RenderJobParameters? ParseRender(string? json)
{
if (string.IsNullOrWhiteSpace(json)) return null;
try
{
var p = JsonSerializer.Deserialize(json, ProtocolJson.Default.RenderJobParameters);
return p is { PresetName.Length: > 0, PresetJson.Length: > 0, Codec.Length: > 0, Width: > 0, Height: > 0 } && IsJsonObject(p.PresetJson) ? p : null;
}
catch (JsonException)
{
return null;
}
}
private static bool IsJsonObject(string json)
{
try
{
using var document = JsonDocument.Parse(json);
return document.RootElement.ValueKind == JsonValueKind.Object;
}
catch (JsonException)
{
return false;
}
}
}