fix(server): keep convert dry runs like Reorganize's, say what a new sequence lacks
The convert endpoint planned the whole selection inside the HTTP request, again for every mode, quality keystroke and confirmation, with no limit: behind a proxy an all-sequence conversion could never get past its dry run or its apply. - ReorganizePlanCache becomes PlanCache, shared by Reorganize and Convert: one plan per key made apart from the request, two at once across both kinds, eight kept, 15-minute timeout, 5-minute lifetime, 503 plan_dropped. Keys are typed records (ReorganizePlanKey, ConvertPlanKey) that also say which exceptions are refusals. - A conversion plan is keyed by archive, a convert stamp of the index (SequenceRepository.GetConvertStampAsync: the Reorganize stamp plus, per planned sequence, the photos' count, sizes, formats, pixel sizes and missing, corrupt, preview-only and undecodable flags), the sequences and the target with the quality that counts. Not the mode: the request's mode and options are set on the kept plan, and the free space is measured again at every request through the engine. A plan made while the volume was away is not kept (503 archive_offline). - New sequence holds the converted copies alone: the dry run says the skipped photos are not copied into it (Archive.Convert.NotCopied.*, Server.Convert.OnPcNew), the panel's totals, column and confirmation give the count, and so does the desktop's summary. - Panel: the preview is asked in Replace's neutral words; a dry run cut off by a proxy is asked again while the totals show how long it takes, plan_dropped is retried once; an apply the server let go plans again, a changed one shows Admin.Convert.PlanChanged, and one with no answer is not resent (its job may be queued). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit f0b1e379ce7c08b6d3459ab25ca6214c8f366f6c)
This commit is contained in:
1 parent
d0bbd77c92
commit
8174852a2c
22 files changed
+927
-342
No files matched your search
@@ -124,6 +124,11 @@ public sealed class ConvertDialogViewModel : ArchiveDialogViewModel
|
||||
{
|
||||
if (value is { } mode && !Enum.IsDefined(mode)) return;
|
||||
if (!Set(ref _mode, value)) return;
|
||||
// The rows say what becomes of the photos skipped in the mode's words: not copied into a new sequence.
|
||||
lock (_gate)
|
||||
{
|
||||
if (_run.Plan is { } plan) _run = _run with { Rows = RowsOf(plan) };
|
||||
}
|
||||
OnPropertyChanged(nameof(IsReplace));
|
||||
OnPropertyChanged(nameof(IsNewSequence));
|
||||
OnPlanChanged();
|
||||
@@ -218,7 +223,7 @@ public sealed class ConvertDialogViewModel : ArchiveDialogViewModel
|
||||
{
|
||||
_planning?.Cancel();
|
||||
_planning = null;
|
||||
_run = new DryRun(Current(), plan, ConvertPlans.Rows(plan));
|
||||
_run = new DryRun(Current(), plan, RowsOf(plan));
|
||||
}
|
||||
BusyText = null;
|
||||
ClearError();
|
||||
@@ -238,11 +243,14 @@ public sealed class ConvertDialogViewModel : ArchiveDialogViewModel
|
||||
Quality.Refresh();
|
||||
lock (_gate)
|
||||
{
|
||||
if (_run.Plan is { } plan) _run = _run with { Rows = ConvertPlans.Rows(plan) };
|
||||
if (_run.Plan is { } plan) _run = _run with { Rows = RowsOf(plan) };
|
||||
}
|
||||
base.OnLanguageChanged();
|
||||
}
|
||||
|
||||
/// <summary>The rows of <paramref name="plan"/> in the mode chosen (as planned while none is: the skipped photos stay as they are).</summary>
|
||||
private IReadOnlyList<ConvertRow> RowsOf(ConvertPlan plan) => ConvertPlans.Rows(_mode is { } mode ? plan with { Mode = mode } : plan);
|
||||
|
||||
/// <summary>Typing makes the dry run on screen old at once (Apply waits) and plans again once the typing pauses.</summary>
|
||||
private void OnTyped()
|
||||
{
|
||||
@@ -290,7 +298,8 @@ public sealed class ConvertDialogViewModel : ArchiveDialogViewModel
|
||||
try
|
||||
{
|
||||
var plan = await Task.Run(() => _archives.PlanConvertAsync(_archive.Id, set, options!, mode, cts.Token), cts.Token);
|
||||
result = new DryRun(options, plan, ConvertPlans.Rows(plan));
|
||||
// In the mode chosen now: it may have been chosen while the plan was made.
|
||||
result = new DryRun(options, plan, RowsOf(plan));
|
||||
}
|
||||
catch (OperationCanceledException) when (cts.IsCancellationRequested)
|
||||
{
|
||||
|
||||
@@ -42,14 +42,22 @@ public static class ConvertPlans
|
||||
/// <summary>A valid quality as typed (see <see cref="ValidateQuality"/>).</summary>
|
||||
public static int ParseQuality(string text) => int.Parse(text.Trim(), NumberStyles.Integer, Loc.Culture);
|
||||
|
||||
/// <summary>The rows of a plan in its order: every sequence that converts, or whose photos stay as they are, with why.</summary>
|
||||
/// <summary>
|
||||
/// The rows of a plan in its order: every sequence that converts, or whose photos stay as they are, with why in the
|
||||
/// words of the plan's mode (<see cref="Row"/>).
|
||||
/// </summary>
|
||||
public static IReadOnlyList<ConvertRow> Rows(ConvertPlan plan)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(plan);
|
||||
return [.. plan.Sequences.Where(s => s.Frames.Count > 0 || s.Skipped.Count > 0 || s.Blocked.Count > 0).Select(Row)];
|
||||
return [.. plan.Sequences.Where(s => s.Frames.Count > 0 || s.Skipped.Count > 0 || s.Blocked.Count > 0).Select(s => Row(s, plan.Mode))];
|
||||
}
|
||||
|
||||
public static ConvertRow Row(ConvertSequencePlan step)
|
||||
/// <summary>
|
||||
/// One sequence of a plan: what converts, then what blocks it, then the photos skipped with why. With
|
||||
/// <see cref="ConvertMode.NewSequence"/> those are not copied into the new sequence, which holds the converted copies
|
||||
/// alone; with <see cref="ConvertMode.Replace"/> they stay as they are.
|
||||
/// </summary>
|
||||
public static ConvertRow Row(ConvertSequencePlan step, ConvertMode mode = ConvertMode.Replace)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(step);
|
||||
var named = !string.IsNullOrWhiteSpace(step.Title);
|
||||
@@ -59,7 +67,8 @@ public static class ConvertPlans
|
||||
: string.Empty;
|
||||
// What blocks the whole sequence comes first: the photos it would skip anyway do not matter until it can convert.
|
||||
List<string> kept = [.. step.Blocked.Distinct().Select(b => Loc.L("Archive.Convert.Blocked." + b))];
|
||||
kept.AddRange(step.Skipped.Select(s => Loc.L("Archive.Convert.Skipped." + s.Reason) + Loc.Separator + Loc.Plural("Shell.Status.Photos", s.Frames)));
|
||||
var why = mode == ConvertMode.NewSequence ? "Archive.Convert.NotCopied." : "Archive.Convert.Skipped.";
|
||||
kept.AddRange(step.Skipped.Select(s => Loc.L(why + s.Reason) + Loc.Separator + Loc.Plural("Shell.Status.Photos", s.Frames)));
|
||||
return new ConvertRow(step.SequenceId, name, !named, note, kept, step.Blocked.Count > 0);
|
||||
}
|
||||
|
||||
@@ -68,7 +77,8 @@ public static class ConvertPlans
|
||||
|
||||
/// <summary>
|
||||
/// "12 of 36 change · 3,600 photos · 41 GB → 32 GB · 20 skipped": the sequences converted of those planned, then the
|
||||
/// photos and their sizes when something converts, the photos skipped when there are any.
|
||||
/// photos and their sizes when something converts, the photos skipped when there are any ("20 not copied" with
|
||||
/// <see cref="ConvertMode.NewSequence"/>: the new sequences lack them).
|
||||
/// </summary>
|
||||
public static string Summary(ConvertPlan plan)
|
||||
{
|
||||
@@ -79,7 +89,8 @@ public static class ConvertPlans
|
||||
parts.Add(Loc.Plural("Shell.Status.Photos", plan.Frames));
|
||||
parts.Add(Sizes(plan.BytesBefore, plan.BytesAfter));
|
||||
}
|
||||
if (plan.Skipped > 0) parts.Add(Loc.Plural("Archive.Convert.SkippedPhotos", plan.Skipped));
|
||||
if (plan.Skipped > 0)
|
||||
parts.Add(Loc.Plural(plan.Mode == ConvertMode.NewSequence ? "Archive.Convert.NotCopiedPhotos" : "Archive.Convert.SkippedPhotos", plan.Skipped));
|
||||
return string.Join(Loc.Separator, parts);
|
||||
}
|
||||
|
||||
|
||||
@@ -469,6 +469,50 @@ public sealed class SequenceRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A digest of what a conversion of <paramref name="sequenceIds"/> (null: every sequence) plans from in the index of
|
||||
/// <paramref name="archiveId"/> (owner review 2, R7): what a Reorganize's does (<see cref="GetStampAsync"/>: every sequence
|
||||
/// and its folder, which decide what "every sequence" is and its order; of the sequences planned, their title, photo
|
||||
/// count, frames version and how many of their photos are hashed), and of the photos of the sequences planned, what
|
||||
/// decides whether each converts and the size it is estimated at: their format, size and pixel size, and the flags a
|
||||
/// scan, a verify or a decode records without another frames version (missing, corrupt, read only from the preview,
|
||||
/// undecodable). Another value once one of these changes. A change made on disk alone, which the index does not have
|
||||
/// (a file gone before a scan finds it), leaves it as it was.
|
||||
/// </summary>
|
||||
public async Task<string> GetConvertStampAsync(Guid archiveId, IReadOnlyCollection<Guid>? sequenceIds, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var stamp = await GetStampAsync(archiveId, sequenceIds, cancellationToken).ConfigureAwait(false);
|
||||
// Per sequence planned: its photos, their bytes, formats and pixel sizes, and how many carry each flag that decides.
|
||||
var columns = "count(*), COALESCE(SUM(f.size), 0), COALESCE(SUM(f.format), 0), COALESCE(SUM(f.width), 0), COALESCE(SUM(f.height), 0), " +
|
||||
string.Join(", ", new[] { FrameFlags.Missing, FrameFlags.Corrupt, FrameFlags.PreviewOnly, FrameFlags.Undecodable }
|
||||
.Select(flag => $"COALESCE(SUM(CASE WHEN (f.flags & {(int)flag}) <> 0 THEN 1 ELSE 0 END), 0)"));
|
||||
static (Guid Id, string Photos) Read(DbRow r) =>
|
||||
(r.GetGuid(0), string.Join('\u001f', Enumerable.Range(1, 9).Select(i => r.GetInt64(i).ToString(CultureInfo.InvariantCulture))));
|
||||
var sums = new List<(Guid Id, string Photos)>();
|
||||
if (sequenceIds is null)
|
||||
{
|
||||
sums.AddRange(await _db.ListAsync(
|
||||
$"SELECT f.sequence_id, {columns} FROM frames f JOIN sequences s ON s.id = f.sequence_id WHERE s.archive_id = $1 GROUP BY f.sequence_id",
|
||||
[archiveId], Read, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
else
|
||||
{
|
||||
// 500 ids per statement stays below SQLite's historical 999-variable limit.
|
||||
foreach (var chunk in sequenceIds.Distinct().Chunk(500))
|
||||
{
|
||||
var p = new SqlParameters();
|
||||
var ids = string.Join(", ", chunk.Select(id => p.Add(id)));
|
||||
sums.AddRange(await _db.ListAsync($"SELECT f.sequence_id, {columns} FROM frames f WHERE f.sequence_id IN ({ids}) GROUP BY f.sequence_id",
|
||||
p.ToArray(), Read, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
}
|
||||
using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
|
||||
hash.AppendData(Encoding.UTF8.GetBytes(stamp));
|
||||
foreach (var (id, photos) in sums.OrderBy(s => s.Id))
|
||||
hash.AppendData(Encoding.UTF8.GetBytes(string.Create(CultureInfo.InvariantCulture, $"\n{id:N}\u001f{photos}")));
|
||||
return Convert.ToHexStringLower(hash.GetHashAndReset());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// How many photos of each of <paramref name="sequenceIds"/> (null: every sequence of <paramref name="archiveId"/>) have
|
||||
/// their SHA-256 in the index; a sequence without one is not listed.
|
||||
|
||||
@@ -131,9 +131,13 @@ public sealed record ReorganizePlanDto(IReadOnlyList<ReorganizeStepDto> Steps, i
|
||||
/// <see cref="Target"/> (<c>jpeg</c>, <c>tiff16</c> or <c>linear-dng</c>, the archival choice) with <see cref="Quality"/> 1–100
|
||||
/// for a JPEG (default 95, ignored otherwise). <see cref="Mode"/>, chosen at every run: <c>replace</c> (the converted files
|
||||
/// become the frames, the originals go to the archive's trash) or <c>new-sequence</c> (copies in a new sequence, the source
|
||||
/// untouched). <see cref="DryRun"/> answers the plan (<see cref="ConvertPlanDto"/>) and changes nothing; the change itself
|
||||
/// sends that plan's <see cref="Fingerprint"/> back (409 <c>plan_changed</c> when the archive changed since) and runs as a
|
||||
/// <c>convert</c> job (202).
|
||||
/// untouched, the photos it leaves as they are not copied into it). <see cref="DryRun"/> answers the plan
|
||||
/// (<see cref="ConvertPlanDto"/>) and changes nothing; the server keeps it a few minutes, a refusal too, so the same dry run
|
||||
/// again (also after one cut off, in either mode, at any quality of a lossless target) is answered from it with the free
|
||||
/// space measured again; 503 <c>plan_dropped</c> when the server stopped making it (it took too long, or too many were
|
||||
/// asked at once). The change itself sends that plan's <see cref="Fingerprint"/> back (409 <c>plan_changed</c> when the
|
||||
/// index changed since in what the plan reads: the sequences' rows, and the formats, sizes, hashes and flags of their
|
||||
/// photos) and runs as a <c>convert</c> job (202), which plans again and leaves as it is a sequence that no longer fits.
|
||||
/// </summary>
|
||||
public sealed record ConvertRequest(IReadOnlyList<Guid>? SequenceIds, bool All, string? Target, int? Quality, string? Mode, bool DryRun,
|
||||
string? Fingerprint = null);
|
||||
@@ -143,7 +147,8 @@ public sealed record ConvertRequest(IReadOnlyList<Guid>? SequenceIds, bool All,
|
||||
/// server cannot read them at full resolution: a CR3, a HEIC, a body its RAW decoder does not support), <c>undecodable</c>,
|
||||
/// <c>missing</c>, <c>unhashed</c> (never verified), <c>eight_bit</c> (a RAW the host develops at 8 bits only, for a 16-bit
|
||||
/// target), <c>color_profile</c> (a colour profile the target can neither keep nor convert to) or <c>too_large</c>, how
|
||||
/// many, and the reason as one localised sentence.
|
||||
/// many, and the reason as one localised sentence: with <c>new-sequence</c>, that the new sequence lacks them (it holds the
|
||||
/// converted copies alone).
|
||||
/// </summary>
|
||||
public sealed record ConvertSkippedDto(string Reason, int Photos, string Message);
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
// Convert dialog of the admin panel (owner review 2, R7; BRIEF §12.5, §14.8): the photos of the Library's selection, or of
|
||||
// every sequence, converted to JPEG, TIFF-16 or linear DNG. What happens to the originals is chosen at every run, never
|
||||
// preselected: Replace (the converted files become the sequence's photos, the originals go to the archive's trash) or New
|
||||
// sequence (converted copies in a new sequence, the source untouched). Each change asks the server for a dry run (nothing
|
||||
// is touched) and the table shows, per sequence, what converts, what stays as it is and why, and the size before and
|
||||
// after. Apply confirms that very plan by its fingerprint (Replace by typing a word), and the server runs it as a job
|
||||
// (kind "convert") that verifies every converted file before anything changes.
|
||||
// sequence (converted copies in a new sequence, the source untouched; the photos left as they are are not in it). Each
|
||||
// change asks the server for a dry run (nothing is touched) and the table shows, per sequence, what converts, what stays
|
||||
// as it is and why, and the size before and after. The server makes a dry run apart from the request and keeps it a few
|
||||
// minutes, so one that takes longer than a proxy lets a request last is asked for again until it is made. Apply confirms
|
||||
// that very plan by its fingerprint (Replace by typing a word), and the server runs it as a job (kind "convert") that
|
||||
// verifies every converted file before anything changes.
|
||||
import { t, plural, applyStrings } from "./i18n.js";
|
||||
import { escapeHtml, attr, formatBytes, formatNumber, toast, errorText, confirmDialog, markInvalid, clearInvalid } from "./ui.js";
|
||||
import { escapeHtml, attr, formatBytes, formatNumber, formatDuration, toast, errorText, confirmDialog, markInvalid, clearInvalid } from "./ui.js";
|
||||
import { listed } from "./pages/library/shared.js";
|
||||
|
||||
const ENDPOINT = "/api/v1/archive/convert";
|
||||
@@ -14,6 +16,16 @@ const ENDPOINT = "/api/v1/archive/convert";
|
||||
const PLAN_DELAY = 400;
|
||||
const PAGE = 200;
|
||||
const DEFAULT_QUALITY = 95;
|
||||
/**
|
||||
* A dry run the server is still making: a request cut off after CUT_AFTER ms or by a proxy's timeout (502, 504, 524, or
|
||||
* no answer) is asked again after RETRY_DELAY ms, for up to PLAN_PATIENCE ms (the server stops a plan after 15 minutes);
|
||||
* one the server stopped (503 plan_dropped) DROPPED_RETRIES times more. Past SLOW_PLAN seconds the totals tell how long.
|
||||
*/
|
||||
const CUT_AFTER = 20000;
|
||||
const RETRY_DELAY = 2000;
|
||||
const PLAN_PATIENCE = 16 * 60 * 1000;
|
||||
const DROPPED_RETRIES = 1;
|
||||
const SLOW_PLAN = 10;
|
||||
/** The formats, as the API names them, with their strings; linear DNG, the archival choice, is the one chosen first. */
|
||||
const TARGETS = [
|
||||
{ value: "jpeg", label: "Admin.Convert.Format.Jpeg", tip: "Admin.Convert.Format.Jpeg.Tip" },
|
||||
@@ -27,10 +39,12 @@ const MODES = [
|
||||
{ value: "new-sequence", label: "Admin.Convert.Mode.NewSequence", tip: "Admin.Convert.Mode.NewSequence.Tip" },
|
||||
];
|
||||
/**
|
||||
* The mode the dry run is asked with while none is chosen: what converts does not depend on the mode, so the table shows
|
||||
* the plan at once; Apply waits for a choice, and a choice other than this one plans again (the fingerprint names it).
|
||||
* The mode the dry run is asked with while none is chosen, whose words say only that photos stay as they are: what
|
||||
* converts does not depend on the mode, so the table shows the plan at once; Apply waits for a choice, and New sequence
|
||||
* asks again (the fingerprint names the mode, and the rows say what the new sequence lacks), answered from the plan the
|
||||
* server keeps.
|
||||
*/
|
||||
const PREVIEW_MODE = "new-sequence";
|
||||
const PREVIEW_MODE = "replace";
|
||||
|
||||
let dialogs = 0;
|
||||
|
||||
@@ -80,7 +94,7 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
<div class="table convert" role="table" aria-label="${attr(t("Admin.Convert.Preview"))}">
|
||||
<div class="thead" role="row"><span role="columnheader" data-i18n="Admin.Convert.Column.Sequence"></span>
|
||||
<span role="columnheader" class="end" data-i18n="Admin.Convert.Column.Converted"></span>
|
||||
<span role="columnheader" data-i18n="Admin.Convert.Column.Unchanged"></span>
|
||||
<span role="columnheader" class="convert-left" data-i18n="Admin.Convert.Column.Unchanged"></span>
|
||||
<span role="columnheader" class="end" data-i18n="Admin.Convert.Column.Size"></span></div>
|
||||
<div role="rowgroup" class="convert-rows"></div>
|
||||
</div>
|
||||
@@ -105,6 +119,7 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
const next = dialog.querySelector(".convert-next");
|
||||
const preview = dialog.querySelector(".plan-preview");
|
||||
const rows = dialog.querySelector(".convert-rows");
|
||||
const leftHeader = dialog.querySelector(".convert-left");
|
||||
const foot = dialog.querySelector(".table-foot");
|
||||
const shownText = dialog.querySelector(".convert-shown");
|
||||
const errorLine = dialog.querySelector(".error");
|
||||
@@ -212,10 +227,18 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
const body = request();
|
||||
const key = JSON.stringify(body);
|
||||
planning = true;
|
||||
totals.textContent = t("Admin.Convert.Planning");
|
||||
// Waiting: how long it has taken, once that is long, with why in the tooltip.
|
||||
const started = Date.now();
|
||||
const showWait = () => {
|
||||
const seconds = Math.floor((Date.now() - started) / 1000);
|
||||
totals.textContent = seconds < SLOW_PLAN ? t("Admin.Convert.Planning") : t("Admin.Convert.PlanningFor", formatDuration(seconds));
|
||||
};
|
||||
const ticking = setInterval(showWait, 1000);
|
||||
showWait();
|
||||
totals.title = t("Admin.Convert.Planning.Tip");
|
||||
drawButtons();
|
||||
try {
|
||||
const answer = await api.post(ENDPOINT, { ...body, dryRun: true });
|
||||
const answer = await dryRun(body, key);
|
||||
if (!dialog.open) return;
|
||||
plan = { ...answer, key };
|
||||
shown = PAGE;
|
||||
@@ -228,6 +251,8 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
if (key === keyNow()) showProblem(err);
|
||||
} finally {
|
||||
planning = false;
|
||||
clearInterval(ticking);
|
||||
totals.title = "";
|
||||
}
|
||||
if (again) {
|
||||
again = false;
|
||||
@@ -238,6 +263,28 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
drawPlan();
|
||||
}
|
||||
|
||||
/**
|
||||
* The dry run of body. The server makes it apart from the request and keeps it a few minutes, so a request cut off on
|
||||
* the way (a proxy's timeout) is asked again, and answered once the plan is made; a plan the server stopped (let go for
|
||||
* newer ones, past its time) is asked for once more. Gives up past PLAN_PATIENCE, or once the fields changed or the
|
||||
* dialog closed: the last failure is thrown.
|
||||
*/
|
||||
async function dryRun(body, key) {
|
||||
const started = Date.now();
|
||||
let dropped = 0;
|
||||
for (;;) {
|
||||
const sent = Date.now();
|
||||
try {
|
||||
return await api.post(ENDPOINT, { ...body, dryRun: true });
|
||||
} catch (err) {
|
||||
const retry = cutOff(err, Date.now() - sent) || (err?.code === "plan_dropped" && dropped++ < DROPPED_RETRIES);
|
||||
if (!retry || Date.now() - started > PLAN_PATIENCE || !dialog.open || key !== keyNow()) throw err;
|
||||
await new Promise((done) => setTimeout(done, RETRY_DELAY));
|
||||
if (!dialog.open || key !== keyNow()) throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawPlan() {
|
||||
const current = plan !== null && plan.key === keyNow();
|
||||
preview.classList.toggle("stale", !current);
|
||||
@@ -247,8 +294,11 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
next.hidden = true;
|
||||
preview.hidden = true;
|
||||
} else {
|
||||
// New sequence's words: the photos left as they are are not copied into the new sequence.
|
||||
const copies = plan.mode === "new-sequence";
|
||||
leftHeader.textContent = t(copies ? "Admin.Convert.Column.NotCopied" : "Admin.Convert.Column.Unchanged");
|
||||
totals.textContent = plan.photos === 0 ? t("Admin.Convert.NothingToConvert")
|
||||
: t("Admin.Convert.Totals", formatNumber(plan.sequences), formatNumber(plan.photos), formatNumber(plan.skipped),
|
||||
: t(copies ? "Admin.Convert.TotalsNew" : "Admin.Convert.Totals", formatNumber(plan.sequences), formatNumber(plan.photos), formatNumber(plan.skipped),
|
||||
formatBytes(plan.bytesBefore), formatBytes(plan.bytesAfter));
|
||||
space.hidden = plan.photos === 0;
|
||||
space.textContent = plan.space.message;
|
||||
@@ -309,9 +359,9 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
|
||||
/**
|
||||
* Replace asks for a word to be typed (the originals leave the sequence for the trash); New sequence for a plain
|
||||
* confirmation. The plan sent back is the one the table showed. The fields cannot change meanwhile, but a dry run
|
||||
* scheduled before may answer during it: the same plan again (same request and fingerprint) is applied all the same; a
|
||||
* different one is shown instead, and Apply waits for the user.
|
||||
* confirmation that says how many photos the new sequences lack. The plan sent back is the one the table showed. The
|
||||
* fields cannot change meanwhile, but a dry run scheduled before may answer during it: the same plan again (same
|
||||
* request and fingerprint) is applied all the same; a different one is shown instead, and Apply waits for the user.
|
||||
*/
|
||||
async function apply() {
|
||||
if (applyButton.disabled) return;
|
||||
@@ -321,6 +371,8 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
const going = shownPlan.steps.filter((s) => (s.blocked ?? []).length === 0 && s.photos > 0);
|
||||
const list = listed(going.map((s) => t("Admin.Convert.PlanItem", names.get(s.sequenceId) || s.title || folderName(s.folder),
|
||||
plural("Admin.Dashboard.PhotoCount", s.photos), t("Admin.Convert.SizeChange", formatBytes(s.bytesBefore), formatBytes(s.bytesAfter)))));
|
||||
// The photos of the sequences converted that stay as they are: their new sequences hold the converted copies alone.
|
||||
const notCopied = going.reduce((sum, s) => sum + (s.skipped ?? []).reduce((photos, k) => photos + k.photos, 0), 0);
|
||||
const replace = body.mode === "replace";
|
||||
const confirmed = await confirmDialog(replace ? {
|
||||
title: t("Admin.Convert.ConfirmReplaceTitle", format),
|
||||
@@ -334,7 +386,8 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
typedKind: "word",
|
||||
} : {
|
||||
title: t("Admin.Convert.ConfirmNewTitle", format),
|
||||
body: plural("Admin.Convert.ConfirmNew", shownPlan.photos),
|
||||
body: [plural("Admin.Convert.ConfirmNew", shownPlan.photos), notCopied > 0 ? plural("Admin.Convert.ConfirmNewNotCopied", notCopied) : ""]
|
||||
.filter(Boolean).join(" "),
|
||||
list,
|
||||
confirm: t("Admin.Convert.Confirm.New"),
|
||||
confirmTip: t("Admin.Convert.Confirm.New.Tip"),
|
||||
@@ -360,11 +413,15 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
} catch (err) {
|
||||
busy = false;
|
||||
if (!dialog.open) return;
|
||||
if (err?.code === "plan_changed" || err?.code === "not_enough_space" || err?.code === "nothing_to_convert") {
|
||||
// The archive changed since the dry run (or its volume filled up): the new plan is shown, and Apply waits for it.
|
||||
notice = err.code === "plan_changed" ? err.message || t("Admin.Error.Unreachable") : errorText(err);
|
||||
if (["plan_changed", "plan_dropped", "not_enough_space", "nothing_to_convert"].includes(err?.code)) {
|
||||
// The archive changed since the dry run (or its volume filled up, or the server let the plan go before it was
|
||||
// confirmed): nothing was queued; the plan is made again and shown, and Apply waits for it.
|
||||
notice = err.code === "plan_changed" ? t("Admin.Convert.PlanChanged") : errorText(err);
|
||||
plan = null;
|
||||
schedule(0);
|
||||
} else if (cutOff(err, Infinity)) {
|
||||
// No answer: the job may have been queued all the same, and Apply again would queue another one.
|
||||
showError(t("Admin.Convert.NoAnswer"));
|
||||
} else {
|
||||
showProblem(err);
|
||||
}
|
||||
@@ -394,6 +451,14 @@ export function openConvert({ api, target, names = new Map() } = {}) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a request that failed after ms milliseconds was cut off on the way rather than answered: a proxy's timeout (504,
|
||||
* or Cloudflare's 524), or a closed connection or a 502 once it had lasted CUT_AFTER (sooner, the server is not there).
|
||||
*/
|
||||
function cutOff(err, ms) {
|
||||
return err?.status === 504 || err?.status === 524 || ((err?.status === 0 || err?.status === 502) && ms >= CUT_AFTER);
|
||||
}
|
||||
|
||||
/** A format's name in the panel's language ("jpeg" → "JPEG", "linear-dng" → "Linear DNG"). */
|
||||
function targetLabel(value) {
|
||||
const known = TARGETS.find((o) => o.value === value);
|
||||
|
||||
@@ -161,14 +161,14 @@ public static class ArchiveEndpoints
|
||||
/// completes as it starts, no one: no client is connected yet). Photos a sequence receives between the dry run and the
|
||||
/// confirmation under the names the plan leaves them keep the fingerprint as it was. A dry run's plan is made apart from
|
||||
/// the request, a few at once and each for a limited time, and kept a few minutes, a refusal too, while what it reads
|
||||
/// from the index stays as it was (<see cref="ReorganizePlanCache"/>; the sequences' rows, and how many photos of those
|
||||
/// planned are hashed): the same dry run again (also after the first was cut off), and its confirmation, take it instead
|
||||
/// of planning again. So <c>plan_changed</c> answers a change the index has, photos a verify hashed among them; one made
|
||||
/// from the index stays as it was (<see cref="PlanCache"/>; the sequences' rows, and how many photos of those planned
|
||||
/// are hashed): the same dry run again (also after the first was cut off), and its confirmation, take it instead of
|
||||
/// planning again. So <c>plan_changed</c> answers a change the index has, photos a verify hashed among them; one made
|
||||
/// on disk alone since the dry run (a folder made by hand, a desktop writing to a shared archive before a scan) is found
|
||||
/// by the job, which then fails and changes nothing.
|
||||
/// </summary>
|
||||
private static async Task<IResult> ReorganizeAsync(HttpContext context, Caller caller, ReorganizeRequest request, ChronosDatabase db, ServerArchive archive,
|
||||
[FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit, ReorganizePlanCache plans)
|
||||
[FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit, PlanCache plans)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (request.All == (request.SequenceIds is not null) || request.SequenceIds is { Count: 0 or > LibraryEndpoints.MaxBulkIds })
|
||||
@@ -181,7 +181,7 @@ public static class ArchiveEndpoints
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
|
||||
var sequences = request.All ? null : request.SequenceIds;
|
||||
// Stamped before planning: a change committed while the plan is made gives the next request another key.
|
||||
var key = ReorganizePlanCache.Key.Of(archive.Id, await db.Sequences.GetStampAsync(archive.Id, sequences, ct), sequences, templates);
|
||||
var key = ReorganizePlanKey.Of(archive.Id, await db.Sequences.GetStampAsync(archive.Id, sequences, ct), sequences, templates);
|
||||
RenamePlan plan;
|
||||
try
|
||||
{
|
||||
@@ -258,19 +258,28 @@ public static class ArchiveEndpoints
|
||||
/// Convert: the photos of <c>sequenceIds</c> or, with <c>all</c>, of every sequence of the archive, converted to
|
||||
/// <c>target</c> (<c>jpeg</c>, <c>tiff16</c>, <c>linear-dng</c>) at <c>quality</c> (JPEG, 1–100, default 95) in <c>mode</c>
|
||||
/// (<c>replace</c> or <c>new-sequence</c>; BRIEF §12.5). With <c>dryRun</c> the plan, nothing touched: per sequence the
|
||||
/// photos converted, their size and the estimate after, the photos left as they are with why (<c>convert_on_pc</c> for a
|
||||
/// format the server cannot read at full resolution), what blocks it; the totals; the space needed and free; the
|
||||
/// fingerprint. Otherwise the request carries that fingerprint and the server queues a <c>convert</c> job (202) that
|
||||
/// applies exactly that plan, leaving as it is (and reporting) a sequence that changed before it ran. 400
|
||||
/// <c>bad_request</c> (details <c>sequenceIds</c>: neither or both of them, none, or more than
|
||||
/// <see cref="LibraryEndpoints.MaxBulkIds"/>; <c>target</c>, <c>quality</c>, <c>mode</c>, <c>fingerprint</c>); 404
|
||||
/// <c>not_found</c> (<c>sequence</c>: not in the archive); 409 <c>plan_changed</c> (details: the fingerprint now),
|
||||
/// <c>nothing_to_convert</c>, <c>not_enough_space</c> (details: the bytes needed), <c>job_unavailable</c>; 503
|
||||
/// <c>archive_offline</c>. Audited (<c>archive_converted</c>: sequences · photos · target · mode); the job tells each sequence
|
||||
/// it replaces the frames of as <c>sequenceChanged</c> <c>converted</c>, each new one as <c>created</c>.
|
||||
/// photos converted, their size and the estimate after, the photos left as they are with why in the mode's words (with
|
||||
/// <c>new-sequence</c>, that the new sequence lacks them; <c>convert_on_pc</c> for a format the server cannot read at
|
||||
/// full resolution), what blocks it; the totals; the space needed and free; the fingerprint. Otherwise the request
|
||||
/// carries that fingerprint and the server queues a <c>convert</c> job (202) that applies exactly that plan, leaving as it
|
||||
/// is (and reporting) a sequence that changed before it ran. 400 <c>bad_request</c> (details <c>sequenceIds</c>: neither
|
||||
/// or both of them, none, or more than <see cref="LibraryEndpoints.MaxBulkIds"/>; <c>target</c>, <c>quality</c>,
|
||||
/// <c>mode</c>, <c>fingerprint</c>); 404 <c>not_found</c> (<c>sequence</c>: not in the archive); 409 <c>plan_changed</c>
|
||||
/// (details: the fingerprint now), <c>nothing_to_convert</c>, <c>not_enough_space</c> (details: the bytes needed),
|
||||
/// <c>job_unavailable</c>; 503 <c>archive_offline</c>, <c>plan_dropped</c> (the plan was stopped: past its time, let go
|
||||
/// for newer ones, or the server stopping). Audited (<c>archive_converted</c>: sequences · photos · target · mode); the job
|
||||
/// tells each sequence it replaces the frames of as <c>sequenceChanged</c> <c>converted</c>, each new one as
|
||||
/// <c>created</c>. A dry run's plan is made apart from the request, a few at once (Reorganize's among them) and each for
|
||||
/// a limited time, and kept a few minutes, a refusal too, while what it reads from the index stays as it was
|
||||
/// (<see cref="PlanCache"/>, <see cref="ConvertPlanKey"/>: the sequences' rows, and of the photos of those planned their
|
||||
/// formats, sizes, hashes and the flags that decide): the same dry run again (also after the first was cut off), in
|
||||
/// either mode and at any quality of a lossless target, and its confirmation, take it instead of planning again; only
|
||||
/// the free space is measured again at every request. So <c>plan_changed</c> answers a change the index has. One made on
|
||||
/// disk alone since the plan, or a job that started or ended on its sequences (which a kept plan shows as it found
|
||||
/// them, busy or not), is found by the job, which plans again and leaves as it is a sequence that no longer fits.
|
||||
/// </summary>
|
||||
private static async Task<IResult> ConvertAsync(HttpContext context, Caller caller, ConvertRequest request, ChronosDatabase db, ServerArchive archive,
|
||||
[FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit)
|
||||
[FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit, PlanCache plans)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (request.All == (request.SequenceIds is not null) || request.SequenceIds is { Count: 0 or > LibraryEndpoints.MaxBulkIds })
|
||||
@@ -282,16 +291,33 @@ public static class ArchiveEndpoints
|
||||
if (!request.DryRun && string.IsNullOrWhiteSpace(request.Fingerprint)) return ApiErrors.BadRequest(context, "bad_request", "fingerprint");
|
||||
if (ArchiveService.GetAvailability(archive.Current) != ArchiveAvailability.Online)
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
|
||||
var options = new ConvertOptions(target, quality);
|
||||
var sequences = request.All ? null : request.SequenceIds;
|
||||
var engine = LibraryEndpoints.Engine(archives, db);
|
||||
// Stamped before planning: a change committed while the plan is made gives the next request another key.
|
||||
var key = ConvertPlanKey.Of(archive.Id, await db.Sequences.GetConvertStampAsync(archive.Id, sequences, ct), sequences, options);
|
||||
ConvertPlan plan;
|
||||
try
|
||||
{
|
||||
plan = await LibraryEndpoints.Engine(archives, db).PlanConvertAsync(archive.Id, request.All ? null : request.SequenceIds,
|
||||
new ConvertOptions(target, quality), mode, ct);
|
||||
// Made whatever becomes of this request, until the cache stops it: the same request again takes it, in either mode.
|
||||
var kept = await plans.GetOrPlanAsync(key, stop => PlanConvertAsync(engine, archive.Current, sequences, options, stop), ct);
|
||||
// What converts is the same in either mode and at any quality of a lossless target: the request's own are set on
|
||||
// it, and the free space is the volume's now.
|
||||
plan = kept with { Options = options, Mode = mode, Space = kept.Space with { Free = await FreeSpaceAsync(engine, archive.Id, options, ct) } };
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return ApiErrors.NotFound(context, "sequence");
|
||||
}
|
||||
catch (ArchiveOfflineException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
|
||||
}
|
||||
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
|
||||
{
|
||||
// Stopped: past its time (a read that hangs), let go for newer plans, or the server stopping.
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "plan_dropped");
|
||||
}
|
||||
var fingerprint = plan.Fingerprint();
|
||||
if (request.DryRun) return Results.Json(Plan(context, plan, fingerprint), ProtocolJson.Default.ConvertPlanDto);
|
||||
if (!string.Equals(fingerprint, request.Fingerprint, StringComparison.Ordinal)) return ApiErrors.Conflict(context, "plan_changed", fingerprint);
|
||||
@@ -299,12 +325,33 @@ public static class ArchiveEndpoints
|
||||
if (!plan.Space.Enough) return ApiErrors.Conflict(context, "not_enough_space", plan.Space.Required.ToString(CultureInfo.InvariantCulture));
|
||||
if (!host.CanRun(JobKind.Convert)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(JobKind.Convert));
|
||||
var job = await host.Engine.EnqueueAsync(ArchiveJobs.Convert(archive.Current, plan), caller.Id, ct);
|
||||
plans.Forget(key);
|
||||
await audit.WriteAsync(caller, context, "archive_converted", "job", job.Id.ToString(),
|
||||
$"{plan.Converted.Count()} · {plan.Frames} · {Wire(target)} · {Wire(mode)}");
|
||||
return Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted);
|
||||
}
|
||||
|
||||
/// <summary>The conversion dry run as the API answers it, each reason in the request's language.</summary>
|
||||
/// <summary>
|
||||
/// The conversion plan <see cref="PlanCache"/> keeps for a <see cref="ConvertPlanKey"/>, made in one mode for both (the
|
||||
/// request's is set on it). One made while the archive's volume was away read nothing of it: it is not kept, and the
|
||||
/// request is answered <c>archive_offline</c>.
|
||||
/// </summary>
|
||||
private static async Task<ConvertPlan> PlanConvertAsync(ArchiveService engine, Archive archive, IReadOnlyList<Guid>? sequences, ConvertOptions options,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var plan = await engine.PlanConvertAsync(archive.Id, sequences, options, ConvertMode.Replace, cancellationToken);
|
||||
if (plan.Sequences.Any(s => s.Blocked.Contains(ConvertBlock.Offline))) throw new ArchiveOfflineException(archive.RootPath, ArchiveAvailability.Offline);
|
||||
return plan;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The free space of the archive's volume now, as the engine measures it for a conversion's plan and its job
|
||||
/// (<see cref="ArchiveServiceOptions.FreeSpace"/>): the plan of no sequence reads no sidecar and no photo.
|
||||
/// </summary>
|
||||
private static async Task<long?> FreeSpaceAsync(ArchiveService engine, Guid archiveId, ConvertOptions options, CancellationToken cancellationToken) =>
|
||||
(await engine.PlanConvertAsync(archiveId, [], options, ConvertMode.Replace, cancellationToken)).Space.Free;
|
||||
|
||||
/// <summary>The conversion dry run as the API answers it, each reason in the request's language and in its mode's words.</summary>
|
||||
private static ConvertPlanDto Plan(HttpContext context, ConvertPlan plan, string fingerprint)
|
||||
{
|
||||
var culture = RequestCulture.Of(context);
|
||||
@@ -314,7 +361,8 @@ public static class ArchiveEndpoints
|
||||
: Loc.Text(space.Enough ? "Archive.Convert.Space" : "Archive.Convert.SpaceShort", [Size(space.Required, culture), Size(free, culture)], culture);
|
||||
return new ConvertPlanDto(Wire(plan.Options.Target), plan.Options.Quality, Wire(plan.Mode),
|
||||
[.. plan.Sequences.Select(s => new ConvertSequenceDto(s.SequenceId, s.Title, s.Folder, s.FramesVersion, s.Frames.Count, s.BytesBefore, s.BytesAfter,
|
||||
[.. s.Skipped.GroupBy(k => Wire(k.Reason)).Select(g => new ConvertSkippedDto(g.Key, g.Sum(k => k.Frames), SkipMessage(g.First().Reason, culture)))],
|
||||
[.. s.Skipped.GroupBy(k => Wire(k.Reason))
|
||||
.Select(g => new ConvertSkippedDto(g.Key, g.Sum(k => k.Frames), SkipMessage(g.First().Reason, plan.Mode, culture)))],
|
||||
[.. s.Blocked.Select(b => new ConvertBlockedDto(Wire(b), Loc.Get("Archive.Convert.Blocked." + b, culture) ?? Wire(b)))]))],
|
||||
plan.Converted.Count(),
|
||||
plan.Frames,
|
||||
@@ -370,10 +418,19 @@ public static class ArchiveEndpoints
|
||||
private static ConvertMode? ModeOf(string? text) =>
|
||||
Enum.GetValues<ConvertMode>().Select(m => (ConvertMode?)m).FirstOrDefault(m => string.Equals(Wire(m!.Value), text?.Trim(), StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
/// <summary>Why photos are not converted, in <paramref name="culture"/>: on the server, a format it cannot read says to convert on a PC.</summary>
|
||||
private static string SkipMessage(ConvertSkip reason, CultureInfo culture) => reason is ConvertSkip.NoNativeDecoder or ConvertSkip.PreviewOnly
|
||||
? Loc.Get("Server.Convert.OnPc", culture) ?? Wire(reason)
|
||||
: Loc.Get("Archive.Convert.Skipped." + reason, culture) ?? Wire(reason);
|
||||
/// <summary>
|
||||
/// Why photos are not converted, in <paramref name="culture"/>: on the server, a format it cannot read says to convert on
|
||||
/// a PC. With <see cref="ConvertMode.NewSequence"/> it says that the new sequence lacks them (it holds the converted
|
||||
/// copies alone); otherwise that they stay as they are.
|
||||
/// </summary>
|
||||
private static string SkipMessage(ConvertSkip reason, ConvertMode mode, CultureInfo culture)
|
||||
{
|
||||
var onPc = reason is ConvertSkip.NoNativeDecoder or ConvertSkip.PreviewOnly;
|
||||
var key = mode == ConvertMode.NewSequence
|
||||
? onPc ? "Server.Convert.OnPcNew" : "Archive.Convert.NotCopied." + reason
|
||||
: onPc ? "Server.Convert.OnPc" : "Archive.Convert.Skipped." + reason;
|
||||
return Loc.Get(key, culture) ?? Wire(reason);
|
||||
}
|
||||
|
||||
/// <summary>A size as the dry run's space line shows it: GB from one gigabyte, MB below.</summary>
|
||||
private static string Size(long bytes, CultureInfo culture) => bytes >= 1_000_000_000
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Conversion;
|
||||
using Chronos.Data.Repositories;
|
||||
|
||||
namespace Chronos.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// The dry runs of Reorganize and Convert kept a short while (owner review 2, R7): the same dry run asked again, and the
|
||||
/// confirmation that follows it, take the plan made then instead of reading every sidecar and photo folder of the selection
|
||||
/// again (with <c>all</c>, of the whole archive), while what the plan reads from the index is as it was then (the stamp its
|
||||
/// key holds: <see cref="SequenceRepository.GetStampAsync"/>, <see cref="SequenceRepository.GetConvertStampAsync"/>) and for
|
||||
/// at most <see cref="Lifetime"/> once made. A refusal (a folder inside a sequence's, a sequence not in the archive) follows
|
||||
/// from the same reads, so it is kept as a plan is (<see cref="PlanKey.Refuses"/>); a failure is not. A plan is made apart
|
||||
/// from the request that asked for it, once for every request of the same key: a request cut off meanwhile (a proxy's
|
||||
/// timeout) leaves it to be made, and the same request again waits for it or takes it. At most <see cref="Concurrency"/>
|
||||
/// plans are made at once, of either kind; one still being made past <see cref="PlanTimeout"/>, let go for newer ones
|
||||
/// (<see cref="Capacity"/>), or when the server stops, is stopped, and whoever waits for it is told it was dropped
|
||||
/// (<see cref="OperationCanceledException"/>). A change made on disk alone, which the index does not have, is not seen by a
|
||||
/// kept plan, and neither is one of the jobs running: the job that applies a plan plans again and leaves as it is what no
|
||||
/// longer matches it, so nothing changes that the dry run did not show.
|
||||
/// </summary>
|
||||
public sealed class PlanCache : IDisposable
|
||||
{
|
||||
/// <summary>How long a plan is kept once made; then its timer lets it go.</summary>
|
||||
public static readonly TimeSpan Lifetime = TimeSpan.FromMinutes(5);
|
||||
|
||||
/// <summary>
|
||||
/// Plans kept or being made at most, of either kind (each holds a step for every sequence it plans). One more lets go
|
||||
/// the kept plan asked for first; with none kept, the plan being made asked for first, which stops.
|
||||
/// </summary>
|
||||
public const int Capacity = 8;
|
||||
|
||||
/// <summary>Plans made at once at most, of either kind; the others wait for their turn.</summary>
|
||||
public const int Concurrency = 2;
|
||||
|
||||
/// <summary>
|
||||
/// How long a plan may take, from when it was asked (its wait for a turn included): then it stops. A read that hangs
|
||||
/// still holds its turn until it returns.
|
||||
/// </summary>
|
||||
public static readonly TimeSpan PlanTimeout = TimeSpan.FromMinutes(15);
|
||||
|
||||
private readonly TimeProvider _time;
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Dictionary<PlanKey, Entry> _plans = [];
|
||||
private readonly SemaphoreSlim _turns = new(Concurrency);
|
||||
private readonly CancellationTokenRegistration _stopping;
|
||||
private long _order;
|
||||
|
||||
/// <param name="lifetime">The host, whose stopping stops the plans being made.</param>
|
||||
public PlanCache(TimeProvider time, IHostApplicationLifetime? lifetime = null)
|
||||
{
|
||||
_time = time;
|
||||
if (lifetime is not null) _stopping = lifetime.ApplicationStopping.Register(Clear);
|
||||
}
|
||||
|
||||
/// <summary>Plans kept or being made now.</summary>
|
||||
internal int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate) return _plans.Count;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The plan kept for <paramref name="key"/>, the one being made for it, or the one <paramref name="plan"/> starts making
|
||||
/// now, apart from the caller, then kept; <paramref name="plan"/> stops when its token is cancelled. A refusal
|
||||
/// (<see cref="PlanKey.Refuses"/>) is kept as a plan is; a failure is passed to every caller waiting for it and not kept,
|
||||
/// and so is a plan stopped (<see cref="OperationCanceledException"/>). <paramref name="cancellationToken"/> stops the
|
||||
/// wait only: the plan is still made, and kept.
|
||||
/// </summary>
|
||||
public async Task<TPlan> GetOrPlanAsync<TPlan>(PlanKey<TPlan> key, Func<CancellationToken, Task<TPlan>> plan, CancellationToken cancellationToken = default)
|
||||
where TPlan : class
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(key);
|
||||
ArgumentNullException.ThrowIfNull(plan);
|
||||
Entry? entry;
|
||||
bool start;
|
||||
lock (_gate)
|
||||
{
|
||||
// Never past its lifetime, whatever its timer.
|
||||
if (_plans.TryGetValue(key, out entry) && entry.Made is { } made && _time.GetUtcNow() - made >= Lifetime) Remove(key);
|
||||
start = !_plans.TryGetValue(key, out entry);
|
||||
if (start)
|
||||
{
|
||||
if (_plans.Count >= Capacity) Remove(_plans.MinBy(p => (p.Value.Made is null, p.Value.Order)).Key);
|
||||
var asked = new Entry(++_order);
|
||||
asked.Deadline = _time.CreateTimer(_ => TimeOut(key, asked), null, PlanTimeout, Timeout.InfiniteTimeSpan);
|
||||
_plans[key] = entry = asked;
|
||||
}
|
||||
}
|
||||
if (start) _ = MakeAsync(key, entry!, async stop => await plan(stop).ConfigureAwait(false));
|
||||
// The key's kind names the plan's: what was made for it is a TPlan.
|
||||
return (TPlan)await entry!.Plan.Task.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>Forgets the plan kept for <paramref name="key"/>: its job is queued, and what the job changes gives the index another stamp.</summary>
|
||||
public void Forget(PlanKey key)
|
||||
{
|
||||
lock (_gate) Remove(key);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_stopping.Dispose();
|
||||
Clear();
|
||||
}
|
||||
|
||||
/// <summary>Lets every plan go; those being made stop.</summary>
|
||||
private void Clear()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
foreach (var key in _plans.Keys.ToList()) Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task MakeAsync(PlanKey key, Entry entry, Func<CancellationToken, Task<object>> plan)
|
||||
{
|
||||
var stop = entry.Stop.Token;
|
||||
object? made = null;
|
||||
Exception? refused = null;
|
||||
try
|
||||
{
|
||||
await _turns.WaitAsync(stop).ConfigureAwait(false);
|
||||
var making = Task.Run(() => plan(stop), CancellationToken.None);
|
||||
// The turn ends with the work, also when no one waits for it any more (a read that hangs).
|
||||
_ = making.ContinueWith(t =>
|
||||
{
|
||||
_ = t.Exception;
|
||||
_turns.Release();
|
||||
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
|
||||
made = await making.WaitAsync(stop).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException && key.Refuses(ex))
|
||||
{
|
||||
// A folder inside a sequence's, a sequence not in the archive: what the key holds decides it, as it does a plan.
|
||||
refused = ex;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// A failure, or the plan stopped (past its time, let go, the server stopping): the next request plans again.
|
||||
lock (_gate)
|
||||
{
|
||||
if (_plans.GetValueOrDefault(key) == entry) Remove(key);
|
||||
}
|
||||
if (ex is OperationCanceledException canceled) entry.Plan.SetCanceled(canceled.CancellationToken);
|
||||
else entry.Plan.SetException(ex);
|
||||
// Observed here: a request cut off before it ended no longer waits for it.
|
||||
_ = entry.Plan.Task.Exception;
|
||||
return;
|
||||
}
|
||||
lock (_gate)
|
||||
{
|
||||
if (_plans.GetValueOrDefault(key) == entry)
|
||||
{
|
||||
entry.Made = _time.GetUtcNow();
|
||||
entry.Deadline?.Dispose();
|
||||
entry.Expiry = _time.CreateTimer(_ => Expire(key, entry), null, Lifetime, Timeout.InfiniteTimeSpan);
|
||||
}
|
||||
}
|
||||
if (refused is null)
|
||||
{
|
||||
entry.Plan.SetResult(made!);
|
||||
return;
|
||||
}
|
||||
entry.Plan.SetException(refused);
|
||||
_ = entry.Plan.Task.Exception;
|
||||
}
|
||||
|
||||
/// <summary>A plan still being made at <see cref="PlanTimeout"/> stops.</summary>
|
||||
private void TimeOut(PlanKey key, Entry entry)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
// Made as its deadline fired: kept for its lifetime.
|
||||
if (_plans.GetValueOrDefault(key) == entry && entry.Made is null) Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void Expire(PlanKey key, Entry entry)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_plans.GetValueOrDefault(key) == entry) Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void Remove(PlanKey key)
|
||||
{
|
||||
if (!_plans.Remove(key, out var entry)) return;
|
||||
entry.Deadline?.Dispose();
|
||||
entry.Expiry?.Dispose();
|
||||
// Stopped outside this lock: the wait for it ends on the thread pool.
|
||||
if (entry.Made is null) _ = entry.Stop.CancelAsync();
|
||||
}
|
||||
|
||||
/// <summary>A plan being made (<see cref="Made"/> null) or kept, and the order it was asked in.</summary>
|
||||
private sealed class Entry(long order)
|
||||
{
|
||||
public long Order { get; } = order;
|
||||
|
||||
public TaskCompletionSource<object> Plan { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
/// <summary>Stops the plan while it is made; a source with no timer and no parent, so nothing to release.</summary>
|
||||
public CancellationTokenSource Stop { get; } = new();
|
||||
|
||||
public DateTimeOffset? Made { get; set; }
|
||||
|
||||
/// <summary>Stops the plan at <see cref="PlanTimeout"/> while it is made.</summary>
|
||||
public ITimer? Deadline { get; set; }
|
||||
|
||||
/// <summary>Lets the plan go at <see cref="Lifetime"/> once made.</summary>
|
||||
public ITimer? Expiry { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>What a kept plan is kept by (<see cref="PlanCache"/>): everything it is made from, by value, its kind included.</summary>
|
||||
public abstract record PlanKey
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether <paramref name="exception"/>, thrown while the plan is made, refuses the request for what the key holds (kept
|
||||
/// as a plan is: the same reads refuse it again) rather than failing it (not kept).
|
||||
/// </summary>
|
||||
public abstract bool Refuses(Exception exception);
|
||||
|
||||
/// <summary>The sequences of a request as a key holds them: <c>*</c> for every sequence, else the ids in the order given.</summary>
|
||||
protected static string Selection(IReadOnlyList<Guid>? sequenceIds) =>
|
||||
sequenceIds is null ? "*" : string.Join(',', sequenceIds.Select(id => id.ToString("N")));
|
||||
}
|
||||
|
||||
/// <summary>The key of a plan of <typeparamref name="TPlan"/>: what <see cref="PlanCache.GetOrPlanAsync"/> answers for it.</summary>
|
||||
public abstract record PlanKey<TPlan> : PlanKey where TPlan : class;
|
||||
|
||||
/// <summary>
|
||||
/// What a Reorganize plan is kept by: the archive, the stamp of what it reads from the index
|
||||
/// (<see cref="SequenceRepository.GetStampAsync"/>), the sequences (all of them, or the ids in the order given, which decides
|
||||
/// the folder that gets " (2)") and the templates. A folder inside a sequence's (<see cref="NestedRenameException"/>) or a
|
||||
/// sequence not in the archive (<see cref="ArgumentException"/>) refuses it.
|
||||
/// </summary>
|
||||
public sealed record ReorganizePlanKey(Guid ArchiveId, string Stamp, string Sequences, RenameTemplates Templates) : PlanKey<RenamePlan>
|
||||
{
|
||||
/// <param name="sequenceIds">The sequences of the request; null for every sequence of the archive.</param>
|
||||
public static ReorganizePlanKey Of(Guid archiveId, string stamp, IReadOnlyList<Guid>? sequenceIds, RenameTemplates templates) =>
|
||||
new(archiveId, stamp, Selection(sequenceIds), templates);
|
||||
|
||||
public override bool Refuses(Exception exception) => exception is NestedRenameException || exception.GetType() == typeof(ArgumentException);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What a conversion plan is kept by: the archive, the stamp of what it reads from the index
|
||||
/// (<see cref="SequenceRepository.GetConvertStampAsync"/>), the sequences (all of them, or the ids in the order given) and
|
||||
/// the target with the quality that counts (<see cref="ConvertOptions.Key"/>: a lossless target's ignores it). Not the
|
||||
/// mode: what converts is the same in either, so the plan made with one serves the other (the endpoint sets the request's).
|
||||
/// A sequence not in the archive (<see cref="ArgumentException"/>) refuses it.
|
||||
/// </summary>
|
||||
public sealed record ConvertPlanKey(Guid ArchiveId, string Stamp, string Sequences, string Options) : PlanKey<ConvertPlan>
|
||||
{
|
||||
/// <param name="sequenceIds">The sequences of the request; null for every sequence of the archive.</param>
|
||||
public static ConvertPlanKey Of(Guid archiveId, string stamp, IReadOnlyList<Guid>? sequenceIds, ConvertOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
return new(archiveId, stamp, Selection(sequenceIds), options.Key);
|
||||
}
|
||||
|
||||
public override bool Refuses(Exception exception) => exception.GetType() == typeof(ArgumentException);
|
||||
}
|
||||
@@ -1,223 +0,0 @@
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Data.Repositories;
|
||||
|
||||
namespace Chronos.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Reorganize's dry runs kept a short while (owner review 2, R7): the same dry run asked again, and the confirmation that
|
||||
/// follows it, take the plan made then instead of reading every sidecar and photo folder of the selection again (with
|
||||
/// <c>all</c>, of the whole archive), while what the plan reads from the index is as it was then
|
||||
/// (<see cref="SequenceRepository.GetStampAsync"/>) and for at most <see cref="Lifetime"/> once made. A refusal (a folder
|
||||
/// inside a sequence's, a sequence not in the archive) follows from the same reads, so it is kept as a plan is; a failure
|
||||
/// is not. A plan is made apart from the request that asked for it, once for every request of the same key: a request cut
|
||||
/// off meanwhile (a proxy's timeout) leaves it to be made, and the same request again waits for it or takes it. At most
|
||||
/// <see cref="Concurrency"/> plans are made at once; one still being made past <see cref="PlanTimeout"/>, let go for newer
|
||||
/// ones (<see cref="Capacity"/>), or when the server stops, is stopped, and whoever waits for it is told it was dropped
|
||||
/// (<see cref="OperationCanceledException"/>). A change made on disk alone, which the index does not have, is not seen by a
|
||||
/// kept plan: the <c>move</c> job plans again and refuses a plan that no longer matches the archive, so nothing changes
|
||||
/// that the dry run did not show.
|
||||
/// </summary>
|
||||
public sealed class ReorganizePlanCache : IDisposable
|
||||
{
|
||||
/// <summary>How long a plan is kept once made; then its timer lets it go.</summary>
|
||||
public static readonly TimeSpan Lifetime = TimeSpan.FromMinutes(5);
|
||||
|
||||
/// <summary>
|
||||
/// Plans kept or being made at most (each holds a step for every sequence it changes or carries, or whose photos keep
|
||||
/// their names). One more lets go the kept plan asked for first; with none kept, the plan being made asked for first,
|
||||
/// which stops.
|
||||
/// </summary>
|
||||
public const int Capacity = 8;
|
||||
|
||||
/// <summary>Plans made at once at most; the others wait for their turn.</summary>
|
||||
public const int Concurrency = 2;
|
||||
|
||||
/// <summary>
|
||||
/// How long a plan may take, from when it was asked (its wait for a turn included): then it stops. A read that hangs
|
||||
/// still holds its turn until it returns.
|
||||
/// </summary>
|
||||
public static readonly TimeSpan PlanTimeout = TimeSpan.FromMinutes(15);
|
||||
|
||||
private readonly TimeProvider _time;
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Dictionary<Key, Entry> _plans = [];
|
||||
private readonly SemaphoreSlim _turns = new(Concurrency);
|
||||
private readonly CancellationTokenRegistration _stopping;
|
||||
private long _order;
|
||||
|
||||
/// <param name="lifetime">The host, whose stopping stops the plans being made.</param>
|
||||
public ReorganizePlanCache(TimeProvider time, IHostApplicationLifetime? lifetime = null)
|
||||
{
|
||||
_time = time;
|
||||
if (lifetime is not null) _stopping = lifetime.ApplicationStopping.Register(Clear);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What a plan is kept by: the archive, the stamp of what it reads from the index, the sequences (all of them, or the ids
|
||||
/// in the order given, which decides the folder that gets " (2)") and the templates.
|
||||
/// </summary>
|
||||
public readonly record struct Key(Guid ArchiveId, string Stamp, string Sequences, RenameTemplates Templates)
|
||||
{
|
||||
/// <param name="sequenceIds">The sequences of the request; null for every sequence of the archive.</param>
|
||||
public static Key Of(Guid archiveId, string stamp, IReadOnlyList<Guid>? sequenceIds, RenameTemplates templates) =>
|
||||
new(archiveId, stamp, sequenceIds is null ? "*" : string.Join(',', sequenceIds.Select(id => id.ToString("N"))), templates);
|
||||
}
|
||||
|
||||
/// <summary>Plans kept or being made now.</summary>
|
||||
internal int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate) return _plans.Count;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The plan kept for <paramref name="key"/>, the one being made for it, or the one <paramref name="plan"/> starts making
|
||||
/// now, apart from the caller, then kept; <paramref name="plan"/> stops when its token is cancelled. A refusal
|
||||
/// (<see cref="NestedRenameException"/>, <see cref="ArgumentException"/>) is kept as a plan is; a failure is passed to
|
||||
/// every caller waiting for it and not kept, and so is a plan stopped (<see cref="OperationCanceledException"/>).
|
||||
/// <paramref name="cancellationToken"/> stops the wait only: the plan is still made, and kept.
|
||||
/// </summary>
|
||||
public Task<RenamePlan> GetOrPlanAsync(Key key, Func<CancellationToken, Task<RenamePlan>> plan, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(plan);
|
||||
Entry? entry;
|
||||
bool start;
|
||||
lock (_gate)
|
||||
{
|
||||
// Never past its lifetime, whatever its timer.
|
||||
if (_plans.TryGetValue(key, out entry) && entry.Made is { } made && _time.GetUtcNow() - made >= Lifetime) Remove(key);
|
||||
start = !_plans.TryGetValue(key, out entry);
|
||||
if (start)
|
||||
{
|
||||
if (_plans.Count >= Capacity) Remove(_plans.MinBy(p => (p.Value.Made is null, p.Value.Order)).Key);
|
||||
var asked = new Entry(++_order);
|
||||
asked.Deadline = _time.CreateTimer(_ => TimeOut(key, asked), null, PlanTimeout, Timeout.InfiniteTimeSpan);
|
||||
_plans[key] = entry = asked;
|
||||
}
|
||||
}
|
||||
if (start) _ = MakeAsync(key, entry!, plan);
|
||||
return entry!.Plan.Task.WaitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>Forgets the plan kept for <paramref name="key"/>: its job is queued, and what the job changes gives the index another stamp.</summary>
|
||||
public void Forget(Key key)
|
||||
{
|
||||
lock (_gate) Remove(key);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_stopping.Dispose();
|
||||
Clear();
|
||||
}
|
||||
|
||||
/// <summary>Lets every plan go; those being made stop.</summary>
|
||||
private void Clear()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
foreach (var key in _plans.Keys.ToList()) Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task MakeAsync(Key key, Entry entry, Func<CancellationToken, Task<RenamePlan>> plan)
|
||||
{
|
||||
var stop = entry.Stop.Token;
|
||||
RenamePlan? made = null;
|
||||
Exception? refused = null;
|
||||
try
|
||||
{
|
||||
await _turns.WaitAsync(stop).ConfigureAwait(false);
|
||||
var making = Task.Run(() => plan(stop), CancellationToken.None);
|
||||
// The turn ends with the work, also when no one waits for it any more (a read that hangs).
|
||||
_ = making.ContinueWith(t =>
|
||||
{
|
||||
_ = t.Exception;
|
||||
_turns.Release();
|
||||
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
|
||||
made = await making.WaitAsync(stop).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is NestedRenameException || ex.GetType() == typeof(ArgumentException))
|
||||
{
|
||||
// A folder inside a sequence's, a sequence not in the archive: what the key holds decides it, as it does a plan.
|
||||
refused = ex;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// A failure, or the plan stopped (past its time, let go, the server stopping): the next request plans again.
|
||||
lock (_gate)
|
||||
{
|
||||
if (_plans.GetValueOrDefault(key) == entry) Remove(key);
|
||||
}
|
||||
if (ex is OperationCanceledException canceled) entry.Plan.SetCanceled(canceled.CancellationToken);
|
||||
else entry.Plan.SetException(ex);
|
||||
// Observed here: a request cut off before it ended no longer waits for it.
|
||||
_ = entry.Plan.Task.Exception;
|
||||
return;
|
||||
}
|
||||
lock (_gate)
|
||||
{
|
||||
if (_plans.GetValueOrDefault(key) == entry)
|
||||
{
|
||||
entry.Made = _time.GetUtcNow();
|
||||
entry.Deadline?.Dispose();
|
||||
entry.Expiry = _time.CreateTimer(_ => Expire(key, entry), null, Lifetime, Timeout.InfiniteTimeSpan);
|
||||
}
|
||||
}
|
||||
if (refused is null)
|
||||
{
|
||||
entry.Plan.SetResult(made!);
|
||||
return;
|
||||
}
|
||||
entry.Plan.SetException(refused);
|
||||
_ = entry.Plan.Task.Exception;
|
||||
}
|
||||
|
||||
/// <summary>A plan still being made at <see cref="PlanTimeout"/> stops.</summary>
|
||||
private void TimeOut(Key key, Entry entry)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
// Made as its deadline fired: kept for its lifetime.
|
||||
if (_plans.GetValueOrDefault(key) == entry && entry.Made is null) Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void Expire(Key key, Entry entry)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_plans.GetValueOrDefault(key) == entry) Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void Remove(Key key)
|
||||
{
|
||||
if (!_plans.Remove(key, out var entry)) return;
|
||||
entry.Deadline?.Dispose();
|
||||
entry.Expiry?.Dispose();
|
||||
// Stopped outside this lock: the wait for it ends on the thread pool.
|
||||
if (entry.Made is null) _ = entry.Stop.CancelAsync();
|
||||
}
|
||||
|
||||
/// <summary>A plan being made (<see cref="Made"/> null) or kept, and the order it was asked in.</summary>
|
||||
private sealed class Entry(long order)
|
||||
{
|
||||
public long Order { get; } = order;
|
||||
|
||||
public TaskCompletionSource<RenamePlan> Plan { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
/// <summary>Stops the plan while it is made; a source with no timer and no parent, so nothing to release.</summary>
|
||||
public CancellationTokenSource Stop { get; } = new();
|
||||
|
||||
public DateTimeOffset? Made { get; set; }
|
||||
|
||||
/// <summary>Stops the plan at <see cref="PlanTimeout"/> while it is made.</summary>
|
||||
public ITimer? Deadline { get; set; }
|
||||
|
||||
/// <summary>Lets the plan go at <see cref="Lifetime"/> once made.</summary>
|
||||
public ITimer? Expiry { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -142,7 +142,7 @@ public static class ServerHost
|
||||
services.AddSingleton<RenderStore>();
|
||||
services.AddSingleton<RenderUploads>();
|
||||
services.AddSingleton<SequenceJobs>();
|
||||
services.AddSingleton<ReorganizePlanCache>();
|
||||
services.AddSingleton<PlanCache>();
|
||||
services.AddSingleton<RenderNodeService>();
|
||||
services.AddSingleton<JobEngineHost>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<JobEngineHost>());
|
||||
|
||||
Reference in new issue
Block a user