feat: owner review 2 phase 5b: the server as a project home (#17)
CI / Build and test (push) Successful in 9m58s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 14m8s
CI / PostgreSQL 16 integration (push) Successful in 8m55s
CI / PostgreSQL 17 integration (push) Successful in 8m50s
CI / Docker image (push) Successful in 57s
Release / Server image (push) Successful in 1m22s
CI / Build and test (push) Successful in 9m58s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 14m8s
CI / PostgreSQL 16 integration (push) Successful in 8m55s
CI / PostgreSQL 17 integration (push) Successful in 8m50s
CI / Docker image (push) Successful in 57s
Release / Server image (push) Successful in 1m22s
A Chronos Server keeps its sequences' projects, as a PC's archive does: Main and named projects with versions under /api/v1/sequences/{id}/projects, the projects capability and the projectChanged event. The desktop opens a server's sequences in the workspace (proxies downloaded, hash-checked, resumable) with Save version, Sync, conflicts, History, Restore and the project actions against the server. Renders can be of a project version. The admin panel lists a sequence's projects and versions. Strings in all 38 languages.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #17.
This commit is contained in:
1 parent
255e149087
commit
81c303f484
325 files changed
+11477
-516
No files matched your search
@@ -387,6 +387,15 @@ textarea.input.notes { height: auto; min-height: 64px; padding: 6px 10px; resize
|
||||
.frame-badge.hero { color: var(--sun); }
|
||||
.frame-badge.warn { top: auto; bottom: 4px; inset-inline: 4px auto; color: var(--warning); }
|
||||
.table.renders { grid-template-columns: minmax(100px, 1.4fr) max-content max-content max-content max-content max-content; }
|
||||
/* A sequence's projects (owner review 2, R8) and, under one opened, its versions. Their table sits in a wrapper, a grid item
|
||||
of the panel: it may be narrower than the table, so the table's columns fit (fitColumns) instead of the panel scrolling. */
|
||||
.panel > .table-host { min-width: 0; }
|
||||
.table.projects { grid-template-columns: minmax(120px, 1.4fr) max-content minmax(120px, 1.2fr) max-content; }
|
||||
.table.projects.fit-1 { grid-template-columns: minmax(120px, 1fr) max-content max-content; }
|
||||
.table.projects.fit-1 .col-changed { display: none; }
|
||||
.versions { display: grid; grid-template-columns: max-content max-content minmax(0, max-content) minmax(0, 1fr) max-content; gap: 6px 18px; align-items: center; padding: 10px 18px; background: var(--bg); border-top: 1px solid var(--stroke); }
|
||||
.versions .version { display: contents; }
|
||||
.versions .more { grid-column: 1 / -1; justify-self: start; }
|
||||
.table.trash { grid-template-columns: max-content minmax(140px, 2fr) max-content max-content minmax(60px, 1fr) max-content max-content; }
|
||||
.table.trash.fit-1 { grid-template-columns: max-content minmax(120px, 2fr) max-content max-content max-content max-content; }
|
||||
.table.trash.fit-1 .col-by { display: none; }
|
||||
|
||||
@@ -0,0 +1,254 @@
|
||||
// The projects of a sequence on the panel (owner review 2, R8): each project's name, Main marked, its newest version and who
|
||||
// changed it last, its versions on demand, and Rename and Delete for the projects other than Main. Nothing here edits a
|
||||
// project's settings: the desktop does, in its workspace, and pushes its versions here. The list follows projectChanged.
|
||||
import { t, plural, applyStrings } from "../../i18n.js";
|
||||
import { escapeHtml, attr, formatNumber, formatDateTime, toast, errorText, confirmDialog, formDialog, emptyState, markInvalid, redraw, fitColumns } from "../../ui.js";
|
||||
|
||||
/** Versions read at a time when a project is opened, and with each Load more. */
|
||||
const VERSION_PAGE = 20;
|
||||
/** The most versions one read of them gives (ProjectEndpoints.MaxPage): a refresh reads again as many as are shown, up to this. */
|
||||
const MAX_PAGE = 200;
|
||||
/** A project name, as the server takes it (ProjectService.MaxNameLength). */
|
||||
const MAX_NAME = 60;
|
||||
/** The name every sequence's Main project is made with; shown in the panel's language. */
|
||||
const MAIN_NAME = "Main";
|
||||
/** How long after a projectChanged the projects are read again; one read covers every change meanwhile. */
|
||||
const AFTER_CHANGE_MS = 500;
|
||||
/** Version kinds the panel names (ProjectVersionKinds). */
|
||||
const KINDS = { commit: "Commit", merge: "Merge", restore: "Restore", imported: "Imported", external: "External" };
|
||||
|
||||
/**
|
||||
* Draws the Projects section of sequence sequenceId into host (its count into meta) and keeps it current. Returns
|
||||
* onEvent(e), which the sequence page passes every server event to.
|
||||
*/
|
||||
export function projectsSection(host, meta, { api, sequenceId }) {
|
||||
const base = `/api/v1/sequences/${encodeURIComponent(sequenceId)}/projects`;
|
||||
let projects = [];
|
||||
/** The versions read of each project opened: { items, more }. */
|
||||
const versions = new Map();
|
||||
const expanded = new Set();
|
||||
let timer = null;
|
||||
let failed = null;
|
||||
/** The projects a projectChanged named since the last read; null after a reconnection, when any may have changed. */
|
||||
let changed = new Set();
|
||||
|
||||
host.innerHTML = `
|
||||
<div class="table projects" data-levels="1" role="table" aria-labelledby="projects-title">
|
||||
<div class="thead" role="row"><span role="columnheader" data-i18n="Admin.Projects.Name"></span>
|
||||
<span role="columnheader" class="end" data-i18n="Admin.Projects.Head"></span>
|
||||
<span role="columnheader" class="col-changed" data-i18n="Admin.Projects.Changed"></span>
|
||||
<span role="columnheader"><span class="sr-only" data-i18n="Admin.Common.Actions"></span></span></div>
|
||||
<div role="rowgroup" id="project-rows"></div>
|
||||
</div>`;
|
||||
applyStrings(host);
|
||||
const table = host.querySelector(".table");
|
||||
const rows = host.querySelector("#project-rows");
|
||||
|
||||
/**
|
||||
* Reads the projects again, and the versions of those opened that may have changed (every one when stale is null): as
|
||||
* many as are shown, so the pages Load more read stay.
|
||||
*/
|
||||
async function load(stale = null) {
|
||||
try {
|
||||
projects = await api.get(base);
|
||||
failed = null;
|
||||
// A project deleted meanwhile (here or elsewhere) closes; the versions of the others that changed are read again.
|
||||
for (const id of expanded) {
|
||||
if (!projects.some((p) => p.id === id)) forget(id);
|
||||
}
|
||||
const again = [...expanded].filter((id) => stale === null || stale.has(id));
|
||||
await Promise.all(again.map((id) => reloadVersions(id).catch(() => forget(id))));
|
||||
} catch (err) {
|
||||
projects = [];
|
||||
failed = errorText(err);
|
||||
}
|
||||
if (!host.isConnected) return;
|
||||
meta.textContent = failed ? "" : plural("Admin.Projects.Count", projects.length);
|
||||
draw();
|
||||
}
|
||||
|
||||
/**
|
||||
* The newest versions of a project, or with more the next page after those shown. Read without their settings
|
||||
* (summary): the panel shows none, and a large sequence's versions are megabytes each.
|
||||
*/
|
||||
async function loadVersions(id, more = false, limit = VERSION_PAGE) {
|
||||
const known = versions.get(id);
|
||||
const before = more && known?.items.length ? `&before=${known.items[known.items.length - 1].number}` : "";
|
||||
const page = await api.get(`${base}/${encodeURIComponent(id)}/versions?limit=${limit}${before}&summary=true`);
|
||||
versions.set(id, { items: more && known ? known.items.concat(page.items) : page.items, more: page.more });
|
||||
}
|
||||
|
||||
/** The newest versions of a project read again, as many as are shown (at least a page, at most MAX_PAGE). */
|
||||
function reloadVersions(id) {
|
||||
const shown = versions.get(id)?.items.length ?? 0;
|
||||
return loadVersions(id, false, Math.min(MAX_PAGE, Math.max(VERSION_PAGE, shown)));
|
||||
}
|
||||
|
||||
function forget(id) {
|
||||
expanded.delete(id);
|
||||
versions.delete(id);
|
||||
}
|
||||
|
||||
function draw() {
|
||||
redraw(rows, () => {
|
||||
rows.innerHTML = failed
|
||||
? `<div class="row" role="row"><div class="wide empty" role="cell">${escapeHtml(failed)}</div></div>`
|
||||
: projects.map(rowHtml).join("") || `<div class="row" role="row"><div class="wide" role="cell">${emptyState("Admin.Projects.Empty")}</div></div>`;
|
||||
applyStrings(rows);
|
||||
});
|
||||
rows.querySelectorAll("[data-toggle]").forEach((b) => b.addEventListener("click", () => toggle(b.dataset.toggle)));
|
||||
rows.querySelectorAll("[data-act]").forEach((b) => b.addEventListener("click", () => act(b.dataset.act, find(b.closest("[data-key]").dataset.key))));
|
||||
rows.querySelectorAll("[data-more-versions]").forEach((b) => b.addEventListener("click", () => moreVersions(b.dataset.moreVersions)));
|
||||
fitColumns(table);
|
||||
}
|
||||
|
||||
function find(id) {
|
||||
return projects.find((p) => p.id === id);
|
||||
}
|
||||
|
||||
/** Main keeps the name it was made with in the panel's language; a Main renamed shows its name and the Main mark. */
|
||||
function projectName(p) {
|
||||
return p.isMain && p.name === MAIN_NAME ? t("Admin.Projects.Main") : p.name;
|
||||
}
|
||||
|
||||
function rowHtml(p) {
|
||||
const name = projectName(p);
|
||||
const mark = p.isMain && p.name !== MAIN_NAME
|
||||
? `<span class="pill" data-i18n="Admin.Projects.Main" data-i18n-title="Admin.Projects.Main.Tip"></span>` : "";
|
||||
const who = p.headAuthor ?? p.headClientName ?? "";
|
||||
const when = p.headStoredAt ?? p.updatedAt;
|
||||
const actions = p.isMain ? "" : `
|
||||
<button class="btn small" data-act="rename" data-i18n="Admin.Projects.Rename" data-i18n-title="Admin.Projects.Rename.Tip"></button>
|
||||
<button class="btn small destructive" data-act="delete" data-i18n="Admin.Projects.Delete" data-i18n-title="Admin.Projects.Delete.Tip"></button>`;
|
||||
return `
|
||||
<div class="row" role="row" data-key="${attr(p.id)}">
|
||||
<span class="name-cell" role="cell"><button class="link ellipsis" data-toggle="${attr(p.id)}" aria-expanded="${expanded.has(p.id)}"
|
||||
aria-controls="versions-${attr(p.id)}" dir="auto" title="${attr(p.isMain ? `${t("Admin.Projects.Main.Tip")}\n${t("Admin.Projects.Versions.Tip")}` : t("Admin.Projects.Versions.Tip"))}">${escapeHtml(name)}</button>${mark}</span>
|
||||
<span class="num end nowrap" role="cell" title="${attr(t("Admin.Projects.Head.Tip"))}">${escapeHtml(formatNumber(p.headNumber))}</span>
|
||||
<span class="col-changed ellipsis" role="cell" title="${attr([kindTip(p.headKind), p.headClientName].filter(Boolean).join("\n"))}"><span dir="auto">${escapeHtml(who)}</span>${who ? " · " : ""}<span class="muted nowrap">${escapeHtml(formatDateTime(when))}</span></span>
|
||||
<span class="row-actions" role="cell">${actions}</span>
|
||||
</div>
|
||||
${expanded.has(p.id) ? `<div class="row log-row" role="row"><div class="wide" role="cell" id="versions-${attr(p.id)}">${versionsHtml(p)}</div></div>` : ""}`;
|
||||
}
|
||||
|
||||
function versionsHtml(p) {
|
||||
const page = versions.get(p.id);
|
||||
if (!page) return "";
|
||||
if (page.items.length === 0) return emptyState("Admin.Projects.NoVersions");
|
||||
return `<div class="versions" role="list" aria-label="${attr(t("Admin.Projects.VersionsOf", projectName(p)))}">${page.items.map((v) => `
|
||||
<div class="version" role="listitem">
|
||||
<span class="num nowrap">${escapeHtml(t("Admin.Projects.Number", formatNumber(v.number)))}${v.id === p.headVersionId
|
||||
? ` <span class="pill" data-i18n="Admin.Projects.Current" data-i18n-title="Admin.Projects.Current.Tip"></span>` : ""}</span>
|
||||
<span class="nowrap" title="${attr(kindTip(v.kind))}">${escapeHtml(kindLabel(v.kind))}</span>
|
||||
<span class="ellipsis" dir="auto" title="${attr([v.author, v.clientName].filter(Boolean).join(" · "))}">${escapeHtml(v.author ?? v.clientName ?? "")}</span>
|
||||
<span class="ellipsis muted" dir="auto" title="${attr(v.note ?? "")}">${escapeHtml(v.note ?? "")}</span>
|
||||
<span class="muted nowrap num">${escapeHtml(formatDateTime(v.storedAt ?? v.createdAt))}</span>
|
||||
</div>`).join("")}
|
||||
${page.more ? `<button class="btn small more" data-more-versions="${attr(p.id)}" data-i18n="Admin.Common.More" data-i18n-title="Admin.Common.More.Tip"></button>` : ""}</div>`;
|
||||
}
|
||||
|
||||
function kindLabel(kind) {
|
||||
return KINDS[kind] ? t(`Admin.Projects.Kind.${KINDS[kind]}`) : String(kind ?? "");
|
||||
}
|
||||
|
||||
function kindTip(kind) {
|
||||
return KINDS[kind] ? t(`Admin.Projects.Kind.${KINDS[kind]}.Tip`) : "";
|
||||
}
|
||||
|
||||
async function toggle(id) {
|
||||
if (expanded.delete(id)) {
|
||||
versions.delete(id);
|
||||
draw();
|
||||
return;
|
||||
}
|
||||
expanded.add(id);
|
||||
try {
|
||||
await loadVersions(id);
|
||||
} catch (err) {
|
||||
forget(id);
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
draw();
|
||||
}
|
||||
|
||||
async function moreVersions(id) {
|
||||
try {
|
||||
await loadVersions(id, true);
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
return;
|
||||
}
|
||||
draw();
|
||||
}
|
||||
|
||||
async function act(action, project) {
|
||||
if (!project || project.isMain) return;
|
||||
if (action === "rename") return rename(project);
|
||||
if (action === "delete") return remove(project);
|
||||
}
|
||||
|
||||
async function rename(project) {
|
||||
const data = await formDialog({
|
||||
title: t("Admin.Projects.RenameTitle", project.name),
|
||||
fields: `<label><span data-i18n="Admin.Projects.NewName"></span><input class="input" name="name" dir="auto" maxlength="${MAX_NAME}" autocomplete="off"
|
||||
value="${attr(project.name)}" data-i18n-title="Admin.Projects.NewName.Tip"></label>`,
|
||||
confirm: t("Admin.Projects.RenameConfirm"),
|
||||
confirmTip: t("Admin.Projects.RenameConfirm.Tip"),
|
||||
cancel: t("Admin.Common.Cancel"),
|
||||
cancelTip: t("Admin.Common.Cancel.Tip"),
|
||||
check: (form) => {
|
||||
const name = form.elements.name.value.trim();
|
||||
if (name.length > 0 && name.length <= MAX_NAME) return true;
|
||||
markInvalid(form.elements.name, t("Admin.Projects.NameInvalid"));
|
||||
form.elements.name.focus();
|
||||
return false;
|
||||
},
|
||||
});
|
||||
const name = String(data?.get("name") ?? "").trim();
|
||||
if (!data || name === project.name) return;
|
||||
try {
|
||||
await api.patch(`${base}/${encodeURIComponent(project.id)}`, { name });
|
||||
toast(t("Admin.Projects.Renamed", name));
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
await load();
|
||||
}
|
||||
|
||||
/** Delete, after a confirmation that names the project: it and its versions move to the sequence's project trash. */
|
||||
async function remove(project) {
|
||||
const confirmed = await confirmDialog({
|
||||
title: t("Admin.Projects.DeleteTitle", project.name),
|
||||
body: t("Admin.Projects.DeleteBody", formatNumber(project.headNumber)),
|
||||
confirm: t("Admin.Projects.DeleteConfirm"),
|
||||
confirmTip: t("Admin.Projects.DeleteConfirm.Tip"),
|
||||
cancel: t("Admin.Common.Keep"),
|
||||
cancelTip: t("Admin.Common.Keep.Tip"),
|
||||
});
|
||||
if (!confirmed) return;
|
||||
try {
|
||||
await api.del(`${base}/${encodeURIComponent(project.id)}`);
|
||||
toast(t("Admin.Projects.Deleted", project.name));
|
||||
forget(project.id);
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
await load();
|
||||
}
|
||||
|
||||
load();
|
||||
return {
|
||||
onEvent(e) {
|
||||
if ((e.type === "projectChanged" && e.sequenceId === sequenceId) || e.type === "reconnected") {
|
||||
if (e.type === "reconnected") changed = null;
|
||||
else changed?.add(e.projectId);
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
const stale = changed;
|
||||
changed = new Set();
|
||||
if (host.isConnected) load(stale);
|
||||
}, AFTER_CHANGE_MS);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
// A sequence on the panel (owner review 2, R6): its title, tags, rating and notes, its facts, its photos in pages of 500
|
||||
// with lazy thumbnails, a photo viewer, deleting photos into the archive's trash, the cover, the originals and the renders
|
||||
// to download. Nothing here edits the video: its settings and its renders are the desktop editor's.
|
||||
// with lazy thumbnails, a photo viewer, deleting photos into the archive's trash, the cover, its projects (R8: listed,
|
||||
// renamed and deleted, projects.js), the originals and the renders to download. Nothing here edits the video: its settings,
|
||||
// its projects' versions and its renders are the desktop editor's.
|
||||
import { t, plural, applyStrings } from "../../i18n.js";
|
||||
import { escapeHtml, attr, formatBytes, formatNumber, formatDateTime, toast, errorText, confirmDialog, emptyState, escapeReverts, redraw } from "../../ui.js";
|
||||
import { icons } from "../../icons.js";
|
||||
import { captureText, cameraLabel, spanText, statusChips, lazyImage, watchImages, retryImages, Selection, selectableGrid, tagList, coverUrl, listed, JOB_ENDED } from "./shared.js";
|
||||
import { projectsSection } from "./projects.js";
|
||||
|
||||
const FRAME_PAGE = 500;
|
||||
/** Deleting more photos than this asks for their number to be typed. */
|
||||
@@ -80,11 +82,16 @@ export async function renderSequence(root, { api, setMeta, actions, onEvent }, i
|
||||
<div class="table-foot" id="frames-foot" hidden><span class="muted" id="frames-shown"></span><span class="spacer"></span>
|
||||
<button class="btn small" id="frames-more" data-i18n="Admin.Common.More" data-i18n-title="Admin.Sequence.MorePhotos.Tip"></button></div>
|
||||
</section>
|
||||
<section class="panel flush" aria-labelledby="projects-title">
|
||||
<div class="panel-head padded"><h2 id="projects-title" data-i18n="Admin.Projects.Title"></h2><span class="muted ellipsis" id="projects-meta"></span></div>
|
||||
<div id="projects" class="table-host"></div>
|
||||
</section>
|
||||
<section class="panel flush" aria-labelledby="renders-title">
|
||||
<div class="panel-head padded"><h2 id="renders-title" data-i18n="Admin.Sequence.Renders"></h2></div>
|
||||
<div id="renders"></div>
|
||||
</section>`;
|
||||
applyStrings(root);
|
||||
const projects = projectsSection(root.querySelector("#projects"), root.querySelector("#projects-meta"), { api, sequenceId: id });
|
||||
const form = root.querySelector("form");
|
||||
const framesHost = root.querySelector("#frames");
|
||||
escapeReverts(form);
|
||||
@@ -573,6 +580,7 @@ export async function renderSequence(root, { api, setMeta, actions, onEvent }, i
|
||||
}
|
||||
|
||||
onEvent((e) => {
|
||||
projects.onEvent(e);
|
||||
if (e.type === "jobState") {
|
||||
// The event names neither the job's kind nor its targets: any job that ended may have worked on this one. The timer
|
||||
// is not pushed back by the next one, so a busy queue still refreshes the page every AFTER_JOB_MS; a refresh that
|
||||
|
||||
@@ -98,7 +98,7 @@ public static class ArchiveEndpoints
|
||||
return InvalidTemplate(context, problem);
|
||||
if (!ProxyPolicy.IsValidLongEdge(request.ProxyLongEdge)) return ApiErrors.BadRequest(context, "bad_request", "proxyLongEdge");
|
||||
|
||||
var engine = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner });
|
||||
var engine = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner, IndexSettingsMayLead = true });
|
||||
Archive updated;
|
||||
try
|
||||
{
|
||||
|
||||
@@ -80,7 +80,7 @@ public static class FileEndpoints
|
||||
using var body = new MemoryStream();
|
||||
await context.Request.Body.CopyToAsync(body, ct);
|
||||
|
||||
var images = (archives ?? new ArchiveService(db, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner })).Images;
|
||||
var images = (archives ?? new ArchiveService(db, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner, IndexSettingsMayLead = true })).Images;
|
||||
ProxyStoreResult result;
|
||||
try
|
||||
{
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Chronos.Core.Model;
|
||||
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;
|
||||
@@ -11,8 +14,9 @@ using Chronos.Server.Storage;
|
||||
namespace Chronos.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// <c>/api/v1/jobs</c> (BRIEF §14.6): queue and history, creation (a render batch becomes one job per preset), cancel,
|
||||
/// pause, resume, retry and priority. Changes reach WebSocket clients as <c>jobState</c> and <c>jobProgress</c> events.
|
||||
/// <c>/api/v1/jobs</c> (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 <c>jobState</c> and <c>jobProgress</c> events.
|
||||
/// </summary>
|
||||
public static class JobEndpoints
|
||||
{
|
||||
@@ -62,7 +66,7 @@ public static class JobEndpoints
|
||||
}
|
||||
|
||||
private static async Task<IResult> CreateAsync(HttpContext context, Caller caller, CreateJobRequest request, ChronosDatabase db, JobEngineHost host,
|
||||
ServerArchive archive, AuditLog audit)
|
||||
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)))
|
||||
@@ -94,7 +98,11 @@ public static class JobEndpoints
|
||||
List<(string Label, string? Parameters)> queue = [(label, request.ParametersJson)];
|
||||
if (kind == JobKind.Render)
|
||||
{
|
||||
if (ParseRender(request.ParametersJson) is not { } render) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
|
||||
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);
|
||||
var parametersJson = render == parsed ? request.ParametersJson : JsonSerializer.Serialize(render, ProtocolJson.Default.RenderJobParameters);
|
||||
// A batch becomes one job per preset: a render node uploads one file per job (BRIEF §14.7).
|
||||
IReadOnlyList<RenderJobParameters> each;
|
||||
try
|
||||
@@ -107,7 +115,7 @@ public static class JobEndpoints
|
||||
return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
|
||||
}
|
||||
queue = each.Count == 1
|
||||
? [(ProcessingJobs.RenderLabel(label, render.PresetName), request.ParametersJson)]
|
||||
? [(ProcessingJobs.RenderLabel(label, render.PresetName), 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))
|
||||
@@ -133,6 +141,51 @@ public static class JobEndpoints
|
||||
return Results.Json(Dto.Job(first!), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The render of a project version (owner review 2, R8), checked against the sequence: <see cref="RenderJobParameters.ProjectVersionId"/>
|
||||
/// must be a version of one of its projects (that project, when <see cref="RenderJobParameters.ProjectId"/> 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 (<c>project</c>, <c>version</c>) instead of the parameters when they do not hold.
|
||||
/// </summary>
|
||||
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<ProjectVersion?> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The oriented size of the sequence's first frame (what a preset without a fixed size renders at), else the job's size.</summary>
|
||||
private 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
|
||||
|
||||
@@ -56,7 +56,13 @@ public static class LibraryEndpoints
|
||||
|
||||
/// <summary>The archive engine behind these operations: the host's, or one of its own (a test host that replaces the archive module).</summary>
|
||||
internal static ArchiveService Engine(ArchiveService? archives, ChronosDatabase db) =>
|
||||
archives ?? new ArchiveService(db, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner, ImagesAfterScan = true });
|
||||
archives ?? new ArchiveService(db, new ArchiveServiceOptions
|
||||
{
|
||||
Decoder = DecoderFactory.CreateServer(),
|
||||
Owner = ArchiveModule.Owner,
|
||||
ImagesAfterScan = true,
|
||||
IndexSettingsMayLead = true
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// What the Library's filters offer (the panel's Year, Camera and Tag menus): the years of first capture, the camera
|
||||
|
||||
@@ -0,0 +1,416 @@
|
||||
using Microsoft.AspNetCore.Http.Metadata;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Server.Security;
|
||||
|
||||
namespace Chronos.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// <c>/api/v1/sequences/{sid}/projects</c> (owner review 2, R8): the server as the home of its sequences' projects, through
|
||||
/// the archive engine's <see cref="ProjectService"/> as a desktop's archive is (its sidecars first, then the index, under the
|
||||
/// sequence's lease). The live projects with Main first (made from the sequence's settings when first read), a new project
|
||||
/// from a version or from Main's head, rename, delete into the sequence's projects' trash (never Main), the head, the versions
|
||||
/// a page at a time and one by id, the push of a version made in a workspace (a fast-forward, a repeated push answered again,
|
||||
/// a divergence, a hash, format or portability refusal), and the frames a workspace places the positions on. Reading needs
|
||||
/// Read; making, renaming and pushing Edit; deleting Delete, and the sequence's owner, the project's maker or an admin.
|
||||
/// Every change is told to connected clients as <c>projectChanged</c> (the archive module, from what each call changed) and
|
||||
/// creations, renames and deletions are audited. Nothing here edits a video on the admin panel: it lists and manages.
|
||||
/// </summary>
|
||||
public static class ProjectEndpoints
|
||||
{
|
||||
/// <summary>
|
||||
/// The body a push may take: a version's portable settings name each exclusion and camera keyframe by photo (about 150
|
||||
/// bytes each), so the project of a large sequence outgrows the 1 MiB of other JSON bodies. Any version this PC's archive
|
||||
/// or a workspace stores (a file up to <see cref="ProjectSidecars.MaxVersionBytes"/>; the body, with its settings as one
|
||||
/// JSON string, is no larger) fits, with room for the rest of the version: a server and a PC's archive take the same.
|
||||
/// </summary>
|
||||
public const long PushBodyLimit = ProjectSidecars.MaxVersionBytes + (1L << 20);
|
||||
|
||||
/// <summary>Versions one page may hold.</summary>
|
||||
public const int MaxPage = 200;
|
||||
|
||||
/// <summary>
|
||||
/// The settings one page of versions carries at most (in characters): a page stops before the version that would pass it
|
||||
/// (one version always goes) and says more follow, so an answer stays a few MiB however large a sequence's versions are.
|
||||
/// </summary>
|
||||
internal const long PageSettingsChars = 4L << 20;
|
||||
|
||||
private const int MaxNote = 2000;
|
||||
private const int MaxClientName = 200;
|
||||
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var projects = api.MapGroup("/sequences/{sid:guid}/projects").Requires(Permissions.Read);
|
||||
projects.MapGet("", ListAsync);
|
||||
projects.MapPost("", CreateAsync).Requires(Permissions.Edit);
|
||||
projects.MapGet("/frames", FramesAsync);
|
||||
projects.MapGet("/frames/stamp", FramesStampAsync);
|
||||
projects.MapGet("/{pid:guid}", GetAsync);
|
||||
projects.MapPatch("/{pid:guid}", RenameAsync).Requires(Permissions.Edit);
|
||||
projects.MapDelete("/{pid:guid}", DeleteAsync).Requires(Permissions.Delete);
|
||||
projects.MapGet("/{pid:guid}/head", HeadAsync);
|
||||
projects.MapGet("/{pid:guid}/versions", VersionsAsync);
|
||||
projects.MapGet("/{pid:guid}/versions/{vid:guid}", VersionAsync);
|
||||
projects.MapPost("/{pid:guid}/versions", PushAsync).Requires(Permissions.Edit).WithMetadata(new PushBodyLimitMetadata());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The sequence's live projects, Main first, each with its head's kind, author, client and time. A sequence without a Main
|
||||
/// project gets it now, from its settings; while that cannot be done (the archive offline, the sequence busy, its folder
|
||||
/// elsewhere) the answer is 503 with the reason's code. A sequence with no photo yet (no folder in the archive) has none:
|
||||
/// the list is empty, and nothing is written.
|
||||
/// </summary>
|
||||
private static async Task<IResult> ListAsync(HttpContext context, Guid sid, ChronosDatabase db, [FromServices] ArchiveService? archives) =>
|
||||
await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
if (await db.Sequences.GetAsync(sid, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var list = await projects.ListAsync(sid, ct);
|
||||
if (!list.Any(p => p.IsMain) && !string.IsNullOrEmpty(sequence.Folder))
|
||||
{
|
||||
await projects.MainAsync(sid, ct);
|
||||
list = await projects.ListAsync(sid, ct);
|
||||
}
|
||||
var items = new List<ProjectDto>(list.Count);
|
||||
foreach (var project in list) items.Add(ProjectWire.ToDto(project, await HeadOfAsync(projects, project, ct)));
|
||||
return Results.Json(items, ProtocolJson.Default.ListProjectDto);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// A new project (201): its first version, kind <c>imported</c>, copies <c>fromVersionId</c> (a version of any project of the
|
||||
/// sequence; 404 <c>not_found</c> <c>version</c> otherwise) or Main's head. 400 <c>invalid_name</c>, 409 <c>name_taken</c>,
|
||||
/// <c>sequence_empty</c> (no photo yet). Audited <c>project_created</c>.
|
||||
/// </summary>
|
||||
private static async Task<IResult> CreateAsync(HttpContext context, Caller caller, Guid sid, CreateProjectRequest request, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives, AuditLog audit, IHostApplicationLifetime lifetime) =>
|
||||
await RunAsync(context, "version", async ct =>
|
||||
{
|
||||
if (await db.Sequences.GetAsync(sid, ct) is null) return ApiErrors.NotFound(context, "sequence");
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
// Once asked for, the project is made even if the client goes away meanwhile.
|
||||
var created = await projects.CreateAsync(sid, request.Name ?? string.Empty, request.FromVersionId, caller.User.Username, lifetime.ApplicationStopping);
|
||||
await audit.WriteAsync(caller, context, "project_created", "project", created.Id.ToString(), $"{sid} · {created.Name}");
|
||||
return Results.Json(ProjectWire.ToDto(created, await HeadOfAsync(projects, created, ct)), ProtocolJson.Default.ProjectDto,
|
||||
statusCode: StatusCodes.Status201Created);
|
||||
});
|
||||
|
||||
/// <summary>A live project of the sequence; 404 <c>not_found</c> <c>project</c> for one deleted or of another sequence.</summary>
|
||||
private static async Task<IResult> GetAsync(HttpContext context, Guid sid, Guid pid, ChronosDatabase db, [FromServices] ArchiveService? archives) =>
|
||||
await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
var project = known.Project!;
|
||||
if (project.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
return Results.Json(ProjectWire.ToDto(project, await HeadOfAsync(projects, project, ct)), ProtocolJson.Default.ProjectDto);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Gives a project another name (Main too: the desktop shows Main in its own language either way). 400 <c>invalid_name</c>,
|
||||
/// 409 <c>name_taken</c>. Audited <c>project_renamed</c> when the name changed.
|
||||
/// </summary>
|
||||
private static async Task<IResult> RenameAsync(HttpContext context, Caller caller, Guid sid, Guid pid, RenameProjectRequest request, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives, AuditLog audit, IHostApplicationLifetime lifetime) =>
|
||||
await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
var before = known.Project!;
|
||||
if (before.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
var renamed = await projects.RenameAsync(pid, request.Name ?? string.Empty, lifetime.ApplicationStopping);
|
||||
if (renamed.Name != before.Name)
|
||||
await audit.WriteAsync(caller, context, "project_renamed", "project", pid.ToString(), $"{sid} · {before.Name} → {renamed.Name}");
|
||||
return Results.Json(ProjectWire.ToDto(renamed, await HeadOfAsync(projects, renamed, ct)), ProtocolJson.Default.ProjectDto);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a project (204): its record says so and its folder, with every version, moves to the sequence's
|
||||
/// <c>.chronos/projects/.trash</c>, kept and never erased by Chronos. The Main project is refused (409
|
||||
/// <c>main_cannot_be_deleted</c>); so is another account's project of another account's sequence, for anyone but an admin
|
||||
/// (403). Audited <c>project_deleted</c>.
|
||||
/// </summary>
|
||||
private static async Task<IResult> DeleteAsync(HttpContext context, Caller caller, Guid sid, Guid pid, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives, AuditLog audit, IHostApplicationLifetime lifetime) =>
|
||||
await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
var project = known.Project!;
|
||||
if (project.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
if (project.IsMain) return ApiErrors.Conflict(context, ProjectErrors.MainCannotBeDeleted);
|
||||
if (!caller.IsAdmin && known.Sequence!.OwnerUserId != caller.Id && !MadeBy(project, caller.User)) return ApiErrors.Forbidden(context);
|
||||
await projects.DeleteAsync(pid, lifetime.ApplicationStopping);
|
||||
await audit.WriteAsync(caller, context, "project_deleted", "project", pid.ToString(), $"{sid} · {project.Name}");
|
||||
return Results.NoContent();
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// The newest version of a live project; 404 <c>not_found</c> <c>project</c> once it is deleted (its versions are still
|
||||
/// listed: a workspace's copy of it reads as gone, never as in step), <c>version</c> when it has none.
|
||||
/// </summary>
|
||||
private static async Task<IResult> HeadAsync(HttpContext context, Guid sid, Guid pid, ChronosDatabase db, [FromServices] ArchiveService? archives) =>
|
||||
await RunAsync(context, "version", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
if (known.Project!.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
return Results.Json(ProjectWire.ToDto(await projects.HeadAsync(pid, ct)), ProtocolJson.Default.ProjectVersionDto);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// One page of the project's versions: newest first, numbered below <paramref name="before"/> (from the head when absent),
|
||||
/// or with <paramref name="after"/> oldest first, numbered above it. <paramref name="limit"/> 1–<see cref="MaxPage"/> (50 newest
|
||||
/// first, 100 oldest first); a page also ends before its settings pass <see cref="PageSettingsChars"/>, with <c>more</c>.
|
||||
/// <paramref name="summary"/>: the versions without their settings (<c>settingsJson</c> empty), what a list of them shows.
|
||||
/// 400 <c>bad_request</c> (<c>after</c> with <c>before</c>, a number below 0).
|
||||
/// </summary>
|
||||
private static async Task<IResult> VersionsAsync(HttpContext context, Guid sid, Guid pid, int? before, int? after, int? limit, bool? summary, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives)
|
||||
{
|
||||
if (before is not null && after is not null) return ApiErrors.BadRequest(context, "bad_request", "after");
|
||||
if (before is < 0) return ApiErrors.BadRequest(context, "bad_request", "before");
|
||||
if (after is < 0) return ApiErrors.BadRequest(context, "bad_request", "after");
|
||||
var take = Math.Clamp(limit ?? (after is null ? 50 : 100), 1, MaxPage);
|
||||
return await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
var page = after is { } from
|
||||
? await projects.VersionsAfterAsync(pid, from, take + 1, ct)
|
||||
: await projects.HistoryAsync(pid, before, take + 1, ct);
|
||||
// The head as the page was read: the call above caught the project up first.
|
||||
var head = (await projects.GetAsync(pid, ct))?.HeadNumber ?? known.Project!.HeadNumber;
|
||||
var items = new List<ProjectVersionDto>(Math.Min(take, page.Count));
|
||||
var chars = 0L;
|
||||
foreach (var version in page.Take(take))
|
||||
{
|
||||
if (summary != true && items.Count > 0 && chars + version.SettingsJson.Length > PageSettingsChars) break;
|
||||
chars += version.SettingsJson.Length;
|
||||
var dto = ProjectWire.ToDto(version);
|
||||
items.Add(summary == true ? dto with { SettingsJson = string.Empty } : dto);
|
||||
}
|
||||
return Results.Json(new ProjectVersionPageDto(items, head, page.Count > items.Count), ProtocolJson.Default.ProjectVersionPageDto);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A version of the project by id; one the index does not know yet is looked for in the sequence's sidecars first. With
|
||||
/// <paramref name="stored"/>, only a version the archive holds durably (<see cref="ProjectService.StoredAsync"/>: its
|
||||
/// file read back now, with that content hash) of a live project: what a workspace asks before it drops its own copy;
|
||||
/// 404 <c>project</c> for a deleted project, <c>version</c> for a file missing or damaged.
|
||||
/// </summary>
|
||||
private static async Task<IResult> VersionAsync(HttpContext context, Guid sid, Guid pid, Guid vid, bool? stored, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives) =>
|
||||
await RunAsync(context, "version", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
if (stored == true && known.Project!.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
if (await projects.GetVersionAsync(vid, ct) is not { } version)
|
||||
{
|
||||
await projects.ListAsync(sid, ct);
|
||||
version = await projects.GetVersionAsync(vid, ct);
|
||||
}
|
||||
if (version is null || version.ProjectId != pid || (stored == true && !await projects.StoredAsync(pid, version, ct)))
|
||||
return ApiErrors.NotFound(context, "version");
|
||||
return Results.Json(ProjectWire.ToDto(version), ProtocolJson.Default.ProjectVersionDto);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Stores a version made in a workspace from the project's head (<see cref="ProjectService.PushAsync"/>), recording the
|
||||
/// account that pushed it as its author: 201 <c>stored</c> (a fast-forward: numbered head + 1, Main's mirrored into the
|
||||
/// sequence's settings), 200 <c>already_stored</c> (the same id and content stored before: a push repeated after its
|
||||
/// answer was lost), 409 <c>project_diverged</c> (the head moved past its parent; the answer carries the head), 422
|
||||
/// <c>hash_mismatch</c>, <c>newer_format</c>, <c>invalid_version</c> or <c>too_large</c> (a version file over 64 MiB), each answered as a
|
||||
/// <see cref="ProjectPushResultDto"/> and logged. 400 <c>bad_request</c> (<c>id</c>, <c>settingsJson</c>, <c>contentHash</c>,
|
||||
/// <c>note</c>, <c>clientName</c>); 413 <c>too_large</c> above <see cref="PushBodyLimit"/>.
|
||||
/// </summary>
|
||||
private static async Task<IResult> PushAsync(HttpContext context, Caller caller, Guid sid, Guid pid, ProjectVersionDto request, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives, ILoggerFactory loggers, IHostApplicationLifetime lifetime)
|
||||
{
|
||||
if (request is null) return ApiErrors.BadRequest(context, "bad_request");
|
||||
if (request.Id == Guid.Empty) return ApiErrors.BadRequest(context, "bad_request", "id");
|
||||
if (string.IsNullOrEmpty(request.SettingsJson)) return ApiErrors.BadRequest(context, "bad_request", "settingsJson");
|
||||
if (string.IsNullOrEmpty(request.ContentHash)) return ApiErrors.BadRequest(context, "bad_request", "contentHash");
|
||||
if (request.Note is { Length: > MaxNote }) return ApiErrors.BadRequest(context, "bad_request", "note");
|
||||
if (request.ClientName is { Length: > MaxClientName }) return ApiErrors.BadRequest(context, "bad_request", "clientName");
|
||||
return await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
if (known.Project!.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
var client = string.IsNullOrWhiteSpace(request.ClientName) ? caller.ClientName : request.ClientName.Trim();
|
||||
var version = ProjectWire.ToVersion(request) with
|
||||
{
|
||||
ProjectId = pid,
|
||||
Author = caller.User.Username,
|
||||
ClientName = client,
|
||||
Note = string.IsNullOrWhiteSpace(request.Note) ? null : request.Note.Trim(),
|
||||
CreatedAt = request.CreatedAt == default ? DateTimeOffset.UtcNow : request.CreatedAt,
|
||||
StoredAt = null,
|
||||
};
|
||||
// Once sent, the version is stored even if the client goes away meanwhile; a client that asks again is answered already_stored.
|
||||
var result = await projects.PushAsync(pid, version, lifetime.ApplicationStopping);
|
||||
var outcome = ProjectWire.Outcome(result.Outcome);
|
||||
loggers.CreateLogger("Chronos.Server.Projects").LogInformation(
|
||||
"Version {Version} of project {Project} ({Name}) of sequence {Sequence} pushed by {User} from {Client}: {Outcome}{Number}", request.Id, pid,
|
||||
known.Project.Name, sid, caller.User.Username, client ?? "-", outcome, result.Version is { } stored ? $" as number {stored.Number}" : string.Empty);
|
||||
var (status, code, message) = result.Outcome switch
|
||||
{
|
||||
PushOutcome.Stored => (StatusCodes.Status201Created, (string?)null, (string?)null),
|
||||
PushOutcome.AlreadyStored => (StatusCodes.Status200OK, null, null),
|
||||
PushOutcome.Diverged => (StatusCodes.Status409Conflict, ProjectErrors.Diverged, ApiErrors.Message(context, ProjectErrors.Diverged)),
|
||||
PushOutcome.HashMismatch => (StatusCodes.Status422UnprocessableEntity, ProjectErrors.HashMismatch, ApiErrors.Message(context, "version_hash_mismatch")),
|
||||
PushOutcome.NewerFormat => (StatusCodes.Status422UnprocessableEntity, ProjectErrors.NewerFormat, ApiErrors.Message(context, ProjectErrors.NewerFormat)),
|
||||
PushOutcome.TooLarge => (StatusCodes.Status422UnprocessableEntity, "too_large", ApiErrors.Message(context, "too_large")),
|
||||
_ => (StatusCodes.Status422UnprocessableEntity, ProjectErrors.InvalidVersion, ApiErrors.Message(context, ProjectErrors.InvalidVersion)),
|
||||
};
|
||||
return Results.Json(new ProjectPushResultDto(outcome, result.Version is { } v ? ProjectWire.ToDto(v) : null,
|
||||
result.Head is { } h ? ProjectWire.ToDto(h) : null, code, message), ProtocolJson.Default.ProjectPushResultDto, statusCode: status);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The sequence's frames in index order and the photo aliases of its conversions, as of one frames version
|
||||
/// (<see cref="SequenceEndpoints.SnapshotOfAsync"/>): what a workspace places a version's positions on.
|
||||
/// </summary>
|
||||
private static async Task<IResult> FramesAsync(HttpContext context, Guid sid, ChronosDatabase db)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await SequenceEndpoints.SnapshotOfAsync(db, sid, async (on, sequence, c) =>
|
||||
(Frames: await AllFramesAsync(on, sequence.Id, c), Removals: await on.FrameRemovals.ListBySequenceAsync(sequence.Id, c)), ct) is not { } snapshot)
|
||||
return ApiErrors.NotFound(context, "sequence");
|
||||
var (row, (frames, removals)) = snapshot;
|
||||
var aliases = ProjectWire.Aliases(removals);
|
||||
return Results.Json(new ProjectFramesDto(sid, row.FramesVersion, [.. frames.Select(Dto.Frame)], aliases.Count == 0 ? null : aliases),
|
||||
ProtocolJson.Default.ProjectFramesDto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A cheap stamp of what <see cref="FramesAsync"/> answers (<see cref="ArchiveProjectHome.StampOfAsync"/>: the frames
|
||||
/// version, the photos hashed and their checksum, the removals): a workspace reads the frames again only when it moved.
|
||||
/// </summary>
|
||||
private static async Task<IResult> FramesStampAsync(HttpContext context, Guid sid, ChronosDatabase db)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Results.Json(new ProjectFramesStampDto(sid, await ArchiveProjectHome.StampOfAsync(db, sid, context.RequestAborted)),
|
||||
ProtocolJson.Default.ProjectFramesStampDto);
|
||||
}
|
||||
catch (ProjectException)
|
||||
{
|
||||
return ApiErrors.NotFound(context, "sequence");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether <paramref name="user"/> made <paramref name="project"/>: the name it records, by an account that existed when
|
||||
/// the project was made. An account made since under the name of a deleted one is not its maker.
|
||||
/// </summary>
|
||||
internal static bool MadeBy(SequenceProject project, User user) =>
|
||||
project.CreatedBy is { } maker && string.Equals(maker, user.Username, StringComparison.Ordinal) && user.Created <= project.CreatedAt;
|
||||
|
||||
/// <summary>Every frame of a sequence in index order, read a page at a time.</summary>
|
||||
internal static async Task<List<Frame>> AllFramesAsync(ChronosRepositories db, Guid sequenceId, CancellationToken cancellationToken)
|
||||
{
|
||||
const int pageSize = 1000;
|
||||
var frames = new List<Frame>();
|
||||
for (var after = -1; ;)
|
||||
{
|
||||
var page = await db.Frames.ListBySequenceAsync(sequenceId, after, pageSize, cancellationToken);
|
||||
frames.AddRange(page);
|
||||
if (page.Count < pageSize) return frames;
|
||||
after = page[^1].Index;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The sequence and a project of it (deleted ones too, whose versions are kept); one the index does not know yet (an index
|
||||
/// taken back from a backup, a project another program sharing the archive made) is looked for in the sequence's sidecars
|
||||
/// first. Otherwise the refusal: 404 <c>not_found</c> <c>sequence</c> or <c>project</c>.
|
||||
/// </summary>
|
||||
private static async Task<(Sequence? Sequence, SequenceProject? Project, IResult? Refused)> KnownAsync(HttpContext context, ChronosDatabase db,
|
||||
ProjectService projects, Guid sid, Guid pid, CancellationToken ct)
|
||||
{
|
||||
if (await db.Sequences.GetAsync(sid, ct) is not { } sequence) return (null, null, ApiErrors.NotFound(context, "sequence"));
|
||||
if (await projects.GetAsync(pid, ct) is not { } project)
|
||||
{
|
||||
await projects.ListAsync(sid, ct);
|
||||
project = await projects.GetAsync(pid, ct);
|
||||
}
|
||||
return project is not null && project.SequenceId == sid ? (sequence, project, null) : (sequence, null, ApiErrors.NotFound(context, "project"));
|
||||
}
|
||||
|
||||
private static async Task<ProjectVersion?> HeadOfAsync(ProjectService projects, SequenceProject project, CancellationToken ct) =>
|
||||
project.HeadVersionId is { } id ? await projects.GetVersionAsync(id, ct) : null;
|
||||
|
||||
/// <summary>
|
||||
/// Runs a project call with its refusals answered by their codes: 404 <c>not_found</c> (<c>sequence</c>, or
|
||||
/// <paramref name="notFound"/> for a project or version the engine does not know), 400 <c>invalid_name</c>, 409
|
||||
/// <c>name_taken</c>, <c>main_cannot_be_deleted</c>, <c>sequence_empty</c>; 503 <c>archive_offline</c> (offline or read-only), <c>sequence_busy</c>
|
||||
/// (another writer held it past the lease wait), <c>folder_moved</c> (its folder is not where the index has it),
|
||||
/// <c>projects_unreadable</c> (a project record or version this Chronos cannot read now: nothing was written); 413
|
||||
/// <c>too_large</c> for a version file over 64 MiB; 409 <c>folder_in_use</c> when a project's file cannot be written.
|
||||
/// </summary>
|
||||
private static async Task<IResult> RunAsync(HttpContext context, string notFound, Func<CancellationToken, Task<IResult>> call)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await call(context.RequestAborted);
|
||||
}
|
||||
catch (ProjectException ex)
|
||||
{
|
||||
return ex.Reason switch
|
||||
{
|
||||
ProjectError.SequenceNotFound => ApiErrors.NotFound(context, "sequence"),
|
||||
ProjectError.InvalidName => ApiErrors.BadRequest(context, ProjectErrors.InvalidName),
|
||||
ProjectError.NameTaken => ApiErrors.Conflict(context, ProjectErrors.NameTaken),
|
||||
ProjectError.MainCannotBeDeleted => ApiErrors.Conflict(context, ProjectErrors.MainCannotBeDeleted),
|
||||
ProjectError.SequenceEmpty => ApiErrors.Conflict(context, ProjectErrors.SequenceEmpty),
|
||||
_ => ApiErrors.NotFound(context, notFound),
|
||||
};
|
||||
}
|
||||
catch (ArchiveOfflineException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
|
||||
}
|
||||
catch (LeaseUnavailableException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "sequence_busy");
|
||||
}
|
||||
catch (SequenceFolderMovedException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "folder_moved");
|
||||
}
|
||||
catch (ProjectsUnreadableException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "projects_unreadable");
|
||||
}
|
||||
catch (ProjectVersionTooLargeException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status413PayloadTooLarge, "too_large");
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return ApiErrors.Conflict(context, "folder_in_use");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The push route's body limit (<see cref="PushBodyLimit"/>), applied by routing as it selects the endpoint.</summary>
|
||||
private sealed class PushBodyLimitMetadata : IRequestSizeLimitMetadata
|
||||
{
|
||||
public long? MaxRequestBodySize => PushBodyLimit;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,9 @@ public static class SequenceEndpoints
|
||||
{
|
||||
private const int MaxTitle = 200;
|
||||
private const int MaxNotes = 2000;
|
||||
|
||||
/// <summary>How long a settings edit waits for the sequence's lease (another writer filing photos or analysing it) before it answers 503 <c>sequence_busy</c>.</summary>
|
||||
private static readonly TimeSpan SettingsLeaseWait = TimeSpan.FromSeconds(20);
|
||||
/// <summary>Tags a sequence may carry.</summary>
|
||||
internal const int MaxTags = 50;
|
||||
|
||||
@@ -128,8 +131,15 @@ public static class SequenceEndpoints
|
||||
return Results.Json(Dto.Sequence(sequence), ProtocolJson.Default.SequenceDto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Edits a sequence's title, tags, rating, notes and settings in the index (the settings version as an optional check). New
|
||||
/// settings also go to the sequence's <c>project.json</c>, under its lease (<see cref="ArchiveService.EditSettingsAsync"/>),
|
||||
/// as the archive's other writers of the settings do, so the projects never take an older file for the sequence's
|
||||
/// settings (owner review 2, R8): 503 <c>archive_offline</c>, <c>sequence_busy</c> or <c>folder_moved</c>, and 409
|
||||
/// <c>folder_in_use</c>, when that cannot be done now, with nothing changed.
|
||||
/// </summary>
|
||||
private static async Task<IResult> UpdateAsync(HttpContext context, Caller caller, Guid id, UpdateSequenceRequest request, ChronosDatabase db,
|
||||
AuditLog audit, EventHub events)
|
||||
AuditLog audit, EventHub events, ProjectCatchUp projects, [FromServices] ArchiveService? archives)
|
||||
{
|
||||
if (request.Title is { Length: > MaxTitle }) return ApiErrors.BadRequest(context, "bad_request", "title");
|
||||
if (request.Notes is { Length: > MaxNotes }) return ApiErrors.BadRequest(context, "bad_request", "notes");
|
||||
@@ -150,7 +160,39 @@ public static class SequenceEndpoints
|
||||
HeroFrameId = request.HeroFrameId
|
||||
?? (request.SettingsJson is { } settings ? await SettingsHeroAsync(db, id, settings, context.RequestAborted) : null),
|
||||
};
|
||||
var result = await db.Sequences.EditAsync(id, edit, expected, request.ExpectedFramesVersion, context.RequestAborted);
|
||||
SequenceEditResult result;
|
||||
if (request.SettingsJson is { } written && archives is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = await archives.EditSettingsAsync(id, written, ct => db.Sequences.EditAsync(id, edit, expected, request.ExpectedFramesVersion, ct),
|
||||
SettingsLeaseWait, context.RequestAborted);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return ApiErrors.NotFound(context, "sequence");
|
||||
}
|
||||
catch (Chronos.Core.Archiving.ArchiveOfflineException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
|
||||
}
|
||||
catch (Chronos.Core.Archiving.LeaseUnavailableException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "sequence_busy");
|
||||
}
|
||||
catch (SequenceFolderMovedException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "folder_moved");
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return ApiErrors.Conflict(context, "folder_in_use");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await db.Sequences.EditAsync(id, edit, expected, request.ExpectedFramesVersion, context.RequestAborted);
|
||||
}
|
||||
switch (result.Outcome)
|
||||
{
|
||||
case SequenceEditOutcome.NotFound:
|
||||
@@ -166,6 +208,9 @@ public static class SequenceEndpoints
|
||||
var sequence = result.Sequence!;
|
||||
await audit.WriteAsync(caller, context, "sequence_updated", "sequence", id.ToString(), Changes(request));
|
||||
events.Publish(new SequenceChangedEvent(id, "settings", sequence.FramesVersion, sequence.SettingsVersion));
|
||||
// Settings written outside the projects (an older client, the sync of a desktop's copy) become Main's next version now,
|
||||
// so a workspace with Main checked out hears that its home moved (owner review 2, R8).
|
||||
if (request.SettingsJson is not null) projects.Touch(id);
|
||||
context.Response.Headers.ETag = ETag(sequence);
|
||||
return Results.Json(Dto.Sequence(sequence), ProtocolJson.Default.SequenceDto);
|
||||
}
|
||||
@@ -183,7 +228,7 @@ public static class SequenceEndpoints
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
|
||||
if (!caller.IsAdmin && sequence.OwnerUserId != caller.Id) return ApiErrors.Forbidden(context);
|
||||
var trash = archives?.Trash ?? new SequenceTrash(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner });
|
||||
var trash = archives?.Trash ?? new SequenceTrash(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner, IndexSettingsMayLead = true });
|
||||
DeletePlan plan;
|
||||
try
|
||||
{
|
||||
@@ -266,19 +311,28 @@ public static class SequenceEndpoints
|
||||
/// the sequence is gone.
|
||||
/// </summary>
|
||||
internal static async Task<(Sequence Sequence, List<ManifestFrame> Frames)?> SnapshotAsync(ChronosDatabase db, Guid id,
|
||||
Func<ChronosRepositories, Sequence, CancellationToken, Task<List<ManifestFrame>>> listFrames, CancellationToken cancellationToken)
|
||||
Func<ChronosRepositories, Sequence, CancellationToken, Task<List<ManifestFrame>>> listFrames, CancellationToken cancellationToken) =>
|
||||
await SnapshotOfAsync(db, id, listFrames, cancellationToken) is { } snapshot ? (snapshot.Sequence, snapshot.Value) : null;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="SnapshotAsync"/> for anything read from the sequence's frames (the frames a render node renders and the photos
|
||||
/// a project version is placed on, the frames and photo aliases of a project home): <paramref name="read"/> runs again while
|
||||
/// the frames version moved during it, then inside the transaction holding the sequence's row. Null when the sequence is gone.
|
||||
/// </summary>
|
||||
internal static async Task<(Sequence Sequence, T Value)?> SnapshotOfAsync<T>(ChronosDatabase db, Guid id,
|
||||
Func<ChronosRepositories, Sequence, CancellationToken, Task<T>> read, CancellationToken cancellationToken)
|
||||
{
|
||||
for (var attempt = 0; attempt < SnapshotAttempts; attempt++)
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, cancellationToken) is not { } sequence) return null;
|
||||
var frames = await listFrames(db, sequence, cancellationToken);
|
||||
var value = await read(db, sequence, cancellationToken);
|
||||
if (await db.Sequences.GetAsync(id, cancellationToken) is not { } after) return null;
|
||||
if (after.FramesVersion == sequence.FramesVersion) return (sequence, frames);
|
||||
if (after.FramesVersion == sequence.FramesVersion) return (sequence, value);
|
||||
}
|
||||
// Commits keep landing (an upload of earlier photos): the frames are listed while none can.
|
||||
await using var tx = await db.BeginAsync(cancellationToken);
|
||||
if (await tx.Sequences.GetForUpdateAsync(id, cancellationToken) is not { } locked) return null;
|
||||
return (locked, await listFrames(tx, locked, cancellationToken));
|
||||
return (locked, await read(tx, locked, cancellationToken));
|
||||
}
|
||||
|
||||
private static async Task<IResult> FramesAsync(HttpContext context, Guid id, string? cursor, int? limit, ChronosDatabase db)
|
||||
|
||||
@@ -104,7 +104,8 @@ public sealed class ServerState
|
||||
/// <summary>Engine and version of the database ("PostgreSQL 17.2", "SQLite 3.45.1").</summary>
|
||||
public string Database { get; init; } = string.Empty;
|
||||
|
||||
public List<string> Capabilities { get; } = ["uploads", "sync", "jobs", "render-nodes", "events"];
|
||||
/// <summary>What this server offers clients (<see cref="ServerInfo.Capabilities"/>; <see cref="ServerCapabilities"/> names those clients act on).</summary>
|
||||
public List<string> Capabilities { get; } = ["uploads", "sync", "jobs", "render-nodes", "events", ServerCapabilities.Projects];
|
||||
|
||||
public DateTimeOffset StartedAt { get; } = DateTimeOffset.UtcNow;
|
||||
}
|
||||
@@ -163,6 +163,9 @@ public static class ServerHost
|
||||
services.AddSingleton<UploadMaintenance>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<UploadMaintenance>());
|
||||
services.AddSingleton<ICleanupStep, UploadCleanupStep>();
|
||||
// Main takes settings written outside the projects soon after they are (owner review 2, R8).
|
||||
services.AddSingleton<ProjectCatchUp>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<ProjectCatchUp>());
|
||||
services.AddSingleton<DiscoveryBeaconService>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<DiscoveryBeaconService>());
|
||||
}
|
||||
@@ -181,6 +184,7 @@ public static class ServerHost
|
||||
AuthEndpoints.Map(api);
|
||||
UserEndpoints.Map(api);
|
||||
SequenceEndpoints.Map(api);
|
||||
ProjectEndpoints.Map(api);
|
||||
LibraryEndpoints.Map(api);
|
||||
FileEndpoints.Map(api);
|
||||
UploadEndpoints.Map(api);
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Data.Repositories;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Server.Api;
|
||||
@@ -20,7 +23,9 @@ namespace Chronos.Server.Jobs;
|
||||
/// render jobs they can encode, report progress, and upload the output in resumable chunks that become a render of
|
||||
/// the sequence. A claimed job lists every frame with its flags (a node renders none missing, corrupt or excluded) and
|
||||
/// the proxies the server asks for (<see cref="ServerProxies"/>), which the node sends from the originals it fetched.
|
||||
/// A node that stops heart-beating loses its jobs to the queue (<see cref="RenderNodeMonitor"/>).
|
||||
/// A node that stops heart-beating loses its jobs to the queue (<see cref="RenderNodeMonitor"/>). A job of a project version
|
||||
/// (owner review 2, R8) gets that version's settings placed on the frames it lists, and its render records the project and
|
||||
/// the version.
|
||||
/// </summary>
|
||||
public sealed class RenderNodeService(
|
||||
ChronosDatabase db,
|
||||
@@ -90,14 +95,62 @@ public sealed class RenderNodeService(
|
||||
continue;
|
||||
}
|
||||
await TouchAsync(node, cancellationToken);
|
||||
if (await SettingsAsync(parameters, sequence, cancellationToken) is not { } settings)
|
||||
{
|
||||
await FinishAsync(node, job, JobState.Failed, Loc.L("Errors.Render.ProjectVersion"), cancellationToken);
|
||||
continue;
|
||||
}
|
||||
var spec = ServerProxies.Spec(archive.Current);
|
||||
return new ClaimedJob(Dto.Job(job), sequence.Id, parameters.SettingsJson ?? sequence.SettingsJson ?? "{}", parameters.PresetJson,
|
||||
parameters.UseProxies, await ServerProxies.FramesAsync(db, archive.Current, sequence, spec, cancellationToken), spec);
|
||||
// The frames the node renders and the photos a project version's positions are placed on, of one frames version.
|
||||
var snapshot = await SequenceEndpoints.SnapshotOfAsync(db, sequence.Id, async (on, row, ct) => (
|
||||
Frames: await ServerProxies.FramesAsync(on, archive.Current, row, spec, ct),
|
||||
Photos: settings.Portable ? HomePhotos.Of(await ProjectEndpoints.AllFramesAsync(on, row.Id, ct), await HomePhotos.AliasesAsync(on, row.Id, ct)) : null),
|
||||
cancellationToken);
|
||||
if (snapshot is not { } taken)
|
||||
{
|
||||
await FinishAsync(node, job, JobState.Failed, "The sequence no longer exists.", cancellationToken);
|
||||
continue;
|
||||
}
|
||||
var (frames, photos) = taken.Value;
|
||||
return new ClaimedJob(Dto.Job(job), sequence.Id, photos is null ? settings.Json : Placed(settings.Json, photos), parameters.PresetJson,
|
||||
parameters.UseProxies, frames, spec);
|
||||
}
|
||||
await TouchAsync(node, cancellationToken);
|
||||
return (ClaimedJob?)null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The settings a node renders: the job's own (a project version's portable form among them, whose positions are placed on
|
||||
/// the frames the node gets), else those of the project version the job names (owner review 2, R8), else the sequence's.
|
||||
/// Null when the job names a version that is not one of the sequence's projects'.
|
||||
/// </summary>
|
||||
private async Task<(string Json, bool Portable)?> SettingsAsync(RenderJobParameters parameters, Sequence sequence, CancellationToken cancellationToken)
|
||||
{
|
||||
if (parameters.SettingsJson is { } embedded) return (embedded, Waits(embedded));
|
||||
if (parameters.ProjectVersionId is not { } versionId) return (sequence.SettingsJson ?? "{}", false);
|
||||
return await db.Projects.GetVersionAsync(versionId, cancellationToken) is { } version
|
||||
&& await db.Projects.GetAsync(version.ProjectId, cancellationToken) is { } project && project.SequenceId == sequence.Id
|
||||
? (version.SettingsJson, Waits(version.SettingsJson))
|
||||
: null;
|
||||
}
|
||||
|
||||
/// <summary>Settings with positions that wait for their photos (a project version's portable form, settings synced from another copy).</summary>
|
||||
private static bool Waits(string settingsJson) => settingsJson.Contains("pendingFrames", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>The settings with their waiting positions placed on <paramref name="photos"/>, as the server's own render does; others as they are.</summary>
|
||||
private static string Placed(string settingsJson, FramePhotos photos)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ProjectContent.Place(settingsJson, photos);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
// Not a project: the node says so as it reads it.
|
||||
return settingsJson;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Outcome<bool>> ProgressAsync(Caller caller, Guid nodeId, Guid jobId, NodeProgress progress, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome<bool>.Fail(404, "not_found", "node");
|
||||
@@ -176,10 +229,15 @@ public sealed class RenderNodeService(
|
||||
if (await uploads.StageVerifiedAsync(upload, cancellationToken) is not { } staged) return Outcome<RenderDto>.Fail(422, "hash_mismatch");
|
||||
var output = JsonSerializer.Deserialize(upload.RenderJson, ProtocolJson.Default.RenderResultRequest)!;
|
||||
var sequence = await db.Sequences.GetAsync(job.TargetIds[0], cancellationToken);
|
||||
// The render records the project and the version its job rendered (owner review 2, R8).
|
||||
var rendered = JobEndpoints.ParseRender(job.ParametersJson);
|
||||
Render? render;
|
||||
try
|
||||
{
|
||||
render = sequence is null ? null : await renders.CommitAsync(sequence, staged, output, node.ClientName, cancellationToken);
|
||||
render = sequence is null
|
||||
? null
|
||||
: await renders.CommitAsync(sequence, staged, output, node.ClientName, Guid.CreateVersion7(), rendered?.ProjectId, rendered?.ProjectVersionId,
|
||||
cancellationToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is LeaseUnavailableException or ArchiveOfflineException or FolderMovedException)
|
||||
{
|
||||
|
||||
@@ -14,13 +14,17 @@ namespace Chronos.Server.Storage;
|
||||
/// thumbnails, and its job handlers (scan, rebuild, verify, import, thumbnails, proxies, maintenance, the duplicates and
|
||||
/// orphans reports, index backups and clean-ups with the server's own steps, the <see cref="ICleanupStep"/>s in DI, Reorganize's
|
||||
/// <c>move</c> jobs and the <c>convert</c> jobs, which tell connected clients of the sequences they change or make, and check
|
||||
/// their space against the space volume's free bytes, <see cref="VolumeSpace.Report"/>); a scan
|
||||
/// their space against the space volume's free bytes, <see cref="VolumeSpace.Report"/>); the projects of its sequences, every
|
||||
/// change of them told to connected clients as <c>projectChanged</c> (owner review 2, R8); a scan
|
||||
/// queues thumbnails and proxies of what it indexes, as an upload does. Decoding is managed only (BRIEF §4.2). Recurring
|
||||
/// work (the periodic scan, the nightly backup of an SQLite index, clean-ups) is queued by the server's system schedules
|
||||
/// (<see cref="Jobs.ServerScheduleHost"/>, owner review 2, R10).
|
||||
/// </summary>
|
||||
public static class ArchiveModule
|
||||
{
|
||||
/// <summary>The change a conversion's Replace tells about a sequence (<c>sequenceChanged</c>).</summary>
|
||||
private const string ConvertedChange = "converted";
|
||||
|
||||
/// <summary>Name the server writes into the archive's lease files.</summary>
|
||||
public static string Owner { get; } = "server@" + Environment.MachineName;
|
||||
|
||||
@@ -40,6 +44,18 @@ public static class ArchiveModule
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, ct) is { } sequence)
|
||||
events.Publish(new SequenceChangedEvent(id, change, sequence.FramesVersion, sequence.SettingsVersion));
|
||||
// A conversion may leave settings Main has not taken yet (owner review 2, R8).
|
||||
if (change == ConvertedChange) sp.GetService<ProjectCatchUp>()?.Touch(id);
|
||||
},
|
||||
// Every change of a sequence's projects reaches connected clients (owner review 2, R8): a workspace with that
|
||||
// project checked out knows its home moved, the admin panel lists the projects again. Main's head written into the
|
||||
// sequence's settings is a settings change for the clients that read those (an older client, a desktop's sync).
|
||||
ProjectsChanged = async (changed, ct) =>
|
||||
{
|
||||
foreach (var change in changed.Changes)
|
||||
events.Publish(new ProjectChangedEvent(changed.SequenceId, change.ProjectId, change.Change, change.HeadNumber, change.HeadVersionId));
|
||||
if (changed.SettingsChanged && await db.Sequences.GetAsync(changed.SequenceId, ct) is { } sequence)
|
||||
events.Publish(new SequenceChangedEvent(sequence.Id, "settings", sequence.FramesVersion, sequence.SettingsVersion));
|
||||
},
|
||||
Decoder = DecoderFactory.CreateServer(),
|
||||
// Where a conversion writes (owner review 2, R7): the tighter of what the panel shows as the archive's free space
|
||||
@@ -48,6 +64,8 @@ public static class ArchiveModule
|
||||
FreeSpace = volumes is null ? null : volumes.WritableFree,
|
||||
Owner = Owner,
|
||||
ImagesAfterScan = true,
|
||||
// An earlier release's PATCH wrote settings into the index alone: they are never reverted to project.json's.
|
||||
IndexSettingsMayLead = true,
|
||||
Parallelism = () => options.Parallelism,
|
||||
CleanupSteps = [.. sp.GetServices<ICleanupStep>()],
|
||||
// Next to the index in the data folder, where the daily backups always went.
|
||||
|
||||
@@ -18,7 +18,8 @@ namespace Chronos.Server.Storage;
|
||||
/// 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 (<see cref="RenderNodeAvailability"/>), 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 <c>sequenceChanged</c> events (§14.3).
|
||||
/// clients as <c>sequenceChanged</c> events (§14.3); Auto values changed become Main's next version (owner review 2, R8). A
|
||||
/// render of a project version places that version's settings on the sequence's frames and records the project and the version.
|
||||
/// </summary>
|
||||
public static class ProcessingModule
|
||||
{
|
||||
@@ -32,7 +33,7 @@ public static class ProcessingModule
|
||||
var options = sp.GetRequiredService<ServerOptions>();
|
||||
return new ProcessingService(db, new ProcessingOptions
|
||||
{
|
||||
Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner, Parallelism = () => options.Parallelism },
|
||||
Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner, IndexSettingsMayLead = true, Parallelism = () => options.Parallelism },
|
||||
// Analyses and renders take the parallelism as it is when they start, and the one job that runs has the host's
|
||||
// memory budget for its frame window, as on the desktop (owner review 1).
|
||||
Tune = basis =>
|
||||
@@ -48,6 +49,8 @@ public static class ProcessingModule
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, ct) is { } sequence)
|
||||
events.Publish(new SequenceChangedEvent(id, change, sequence.FramesVersion, sequence.SettingsVersion));
|
||||
// Auto values an analysis wrote into the settings become Main's next version now (owner review 2, R8).
|
||||
if (change == ProcessingOptions.SettingsChange) sp.GetService<ProjectCatchUp>()?.Touch(id);
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Channels;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Projects;
|
||||
|
||||
namespace Chronos.Server.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Brings a sequence's projects in step soon after its settings were written outside the projects (owner review 2, R8): an
|
||||
/// older client's or a desktop sync's <c>PATCH /sequences/{id}</c>, an analysis's Auto values, a conversion. Main takes them
|
||||
/// as an external version at once (<see cref="ProjectService.CatchUpAsync"/>), and connected clients hear
|
||||
/// <c>projectChanged</c> without waiting for the next project call, which would catch up all the same. Only a sequence that
|
||||
/// has its Main project already: Main is made when the projects are first read, never for every sequence an analysis or a
|
||||
/// sync touches. One sequence at a time in the background; a sequence asked for again while it waits is caught up once; one
|
||||
/// another writer holds is tried again a few times, then left to that next call. Nothing happens without the archive engine
|
||||
/// (a test host without it).
|
||||
/// </summary>
|
||||
public sealed class ProjectCatchUp(IServiceProvider services, ILogger<ProjectCatchUp> logger) : BackgroundService
|
||||
{
|
||||
/// <summary>The waits before each new try of a sequence another writer holds.</summary>
|
||||
private static readonly TimeSpan[] Retries = [TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(2)];
|
||||
|
||||
private readonly Channel<(Guid Id, int Attempt)> _queue = Channel.CreateUnbounded<(Guid Id, int Attempt)>(new UnboundedChannelOptions { SingleReader = true });
|
||||
private readonly ConcurrentDictionary<Guid, byte> _waiting = new();
|
||||
private CancellationToken _stopping;
|
||||
|
||||
/// <summary>Catches the projects of <paramref name="sequenceId"/> up soon (at once when nothing waits).</summary>
|
||||
public void Touch(Guid sequenceId) => Enqueue(sequenceId, 0);
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_stopping = stoppingToken;
|
||||
try
|
||||
{
|
||||
await foreach (var (id, attempt) in _queue.Reader.ReadAllAsync(stoppingToken))
|
||||
{
|
||||
_waiting.TryRemove(id, out _);
|
||||
await CatchUpAsync(id, attempt, stoppingToken);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
// The server stops: what waits is caught up by the next project call.
|
||||
}
|
||||
}
|
||||
|
||||
private void Enqueue(Guid id, int attempt)
|
||||
{
|
||||
if (_waiting.TryAdd(id, 0)) _queue.Writer.TryWrite((id, attempt));
|
||||
}
|
||||
|
||||
private async Task CatchUpAsync(Guid id, int attempt, CancellationToken cancellationToken)
|
||||
{
|
||||
if (services.GetService<ArchiveService>() is not { } archives) return;
|
||||
try
|
||||
{
|
||||
if (await services.GetRequiredService<ChronosDatabase>().Projects.GetMainAsync(id, cancellationToken) is null) return;
|
||||
await archives.Projects.CatchUpAsync(id, cancellationToken);
|
||||
}
|
||||
catch (LeaseUnavailableException) when (attempt < Retries.Length)
|
||||
{
|
||||
_ = RetryLaterAsync(id, attempt);
|
||||
}
|
||||
catch (Exception ex) when (ex is ProjectException or ArchiveOfflineException or SequenceFolderMovedException or LeaseUnavailableException)
|
||||
{
|
||||
// The sequence is gone, its archive offline or its folder elsewhere, or it stayed busy: the next project call catches up.
|
||||
logger.LogDebug("The projects of sequence {Sequence} are caught up later: {Reason}", id, ex.Message);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogWarning(ex, "The projects of sequence {Sequence} could not be caught up", id);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RetryLaterAsync(Guid id, int attempt)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(Retries[attempt], _stopping);
|
||||
Enqueue(id, attempt + 1);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Stopping.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ namespace Chronos.Server.Storage;
|
||||
/// <param name="archives">The archive engine whose sequence lease the filing takes; one of its own when none is registered.</param>
|
||||
public sealed class RenderStore(ChronosDatabase db, ServerArchive archive, TimeProvider time, ArchiveService? archives = null)
|
||||
{
|
||||
private readonly ArchiveService _archives = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner });
|
||||
private readonly ArchiveService _archives = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner, IndexSettingsMayLead = true });
|
||||
|
||||
/// <summary>
|
||||
/// Moves <paramref name="stagedFile"/> (on the archive volume) to <c><sequence folder>/exports/</c>, never
|
||||
@@ -31,6 +31,15 @@ public sealed class RenderStore(ChronosDatabase db, ServerArchive archive, TimeP
|
||||
/// <exception cref="FolderMovedException">The sequence is not in the folder the index has.</exception>
|
||||
public Task<Render?> CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, Guid id,
|
||||
CancellationToken cancellationToken) =>
|
||||
CommitAsync(sequence, stagedFile, output, producedBy, id, null, null, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// As above, recording the project and the version rendered (owner review 2, R8: a render node's output of a job that names
|
||||
/// them); null for a render of the sequence's own settings.
|
||||
/// </summary>
|
||||
/// <exception cref="FolderMovedException">The sequence is not in the folder the index has.</exception>
|
||||
public Task<Render?> CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, Guid id, Guid? projectId,
|
||||
Guid? projectVersionId, CancellationToken cancellationToken) =>
|
||||
_archives.UnderLeaseAsync(sequence, async row =>
|
||||
{
|
||||
if (row is null || string.IsNullOrEmpty(row.Folder) || ArchivePathGuard.Resolve(archive.Root, row.Folder.TrimEnd('/') + "/exports") is not { } exports)
|
||||
@@ -55,6 +64,8 @@ public sealed class RenderStore(ChronosDatabase db, ServerArchive archive, TimeP
|
||||
Bytes = output.Size,
|
||||
Created = time.GetUtcNow(),
|
||||
ProducedBy = producedBy,
|
||||
ProjectId = projectId,
|
||||
ProjectVersionId = projectVersionId,
|
||||
};
|
||||
await db.Renders.InsertAsync(render, cancellationToken);
|
||||
return (Render?)render;
|
||||
|
||||
Reference in new issue
Block a user