fix(convert): the job converts exactly the photos its preview listed, once
A conversion job could still do something its confirmed dry run did not show, or refuse what it should have converted: - An upload landing between the confirmation and the sequence's turn made the sequence Changed at every retry. Each ConvertSequence now names the photos the plan converts (Photos, their frame ids); the job takes those from its re-plan, checks their content against the confirmed digest and leaves photos that came in since as they are. A photo the plan did not list is never converted, whatever changed on disk or in the index since the dry run. - A file another program held during the dry run was planned as Missing and then converted or not depending on the re-plan. It is now its own reason (inaccessible, "held open or cannot be opened"); a truncated file reads as undecodable. At job time a confirmed photo that cannot be opened leaves the sequence Blocked for the retry; a held photo the job does not convert does not hide a real change. - The same sequences could be converted twice (two dialogs or tabs, a lost answer, no dry run at all). MassConverter.QueueAsync checks the queued, running and paused conversions and queues under one lock, in the caller's context (the desktop's enqueue shows a toast on the UI thread); a job's own re-plan treats sequences of conversions queued before it as busy. - The per-photo space check reserved a full batch of photos even when fewer were left, so a job whose preview had the space could stop at every retry. It now reserves the photo and those after it that may convert at once. - New sequence now says in the dry run, the panel's totals and confirmation, and a new desktop confirmation, how many photos each new sequence lacks; only sequences that are copied use those words. - On the server, a confirmation takes only the plan kept for the index as it is now (503 plan_dropped otherwise, never a plan made inside the request), and busy sequences and free space are as they are at every request. The panel's Apply asks the dry run again first, with Close still available, and confirms only the same fingerprint. - Maintenance keeps another host's conversion staging as long as its journal and purges both together; the unstaged-copy failure and blocked sequences are logged in catalog sentences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit fefd9c34ebfeed1f9e84161610ef286026d38355)
This commit is contained in:
1 parent
5b5acaf307
commit
c669d0504d
25 files changed
+1307
-226
No files matched your search
@@ -185,11 +185,16 @@ public sealed class ConvertTests(TestContext context)
|
||||
var skipped = plan.Steps.Single().Skipped.Single();
|
||||
Assert.Equal(("convert_on_pc", 2), (skipped.Reason, skipped.Photos));
|
||||
Assert.Equal(Loc.Get("Server.Convert.OnPc", italian), skipped.Message);
|
||||
// Into a new sequence, which holds the converted copies alone: the dry run says the copy lacks them.
|
||||
var copies = await DryRunAsync(admin, new ConvertRequest([id], false, "linear-dng", null, "new-sequence", DryRun: true));
|
||||
Assert.Equal((1, 2), (copies.Photos, copies.Skipped));
|
||||
var left = copies.Steps.Single().Skipped.Single();
|
||||
// Into a new sequence, which holds the converted copies alone: the dry run says the copy lacks them. Of a sequence
|
||||
// that converts nothing, no copy is made: its photos stay as they are.
|
||||
var lake = (await Uploads.UploadAsync(editor, Heif(9), "IMG_0009.HEIC", sessionKey: "lake")).SequenceId;
|
||||
await UntilIdleAsync(server);
|
||||
var copies = await DryRunAsync(admin, new ConvertRequest([id, lake], false, "linear-dng", null, "new-sequence", DryRun: true));
|
||||
Assert.Equal((1, 3), (copies.Photos, copies.Skipped));
|
||||
var left = copies.Steps.Single(s => s.SequenceId == id).Skipped.Single();
|
||||
Assert.Equal(("convert_on_pc", 2, Loc.Get("Server.Convert.OnPcNew", italian)), (left.Reason, left.Photos, left.Message));
|
||||
var stays = copies.Steps.Single(s => s.SequenceId == lake).Skipped.Single();
|
||||
Assert.Equal(("convert_on_pc", 1, Loc.Get("Server.Convert.OnPc", italian)), (stays.Reason, stays.Photos, stays.Message));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -251,10 +256,11 @@ public sealed class ConvertTests(TestContext context)
|
||||
await UntilJobAsync(admin, job.Id, "done");
|
||||
Assert.True((await server.Database.Frames.ListBySequenceAsync(id)).All(f => f.Format == ImageFileFormat.Tiff), "converted");
|
||||
|
||||
// Converted: the next dry run is another plan, which says why nothing converts, in New sequence's words too.
|
||||
// Converted: the next dry run is another plan, which says why nothing converts; with New sequence too, which makes no
|
||||
// copy of a sequence that converts nothing, so its photos stay as they are.
|
||||
var done = await DryRunAsync(admin, request with { Mode = "new-sequence" });
|
||||
Assert.Equal((0, 5), (done.Photos, done.Skipped));
|
||||
Assert.Equal(Loc.Get("Archive.Convert.NotCopied.SameFormat", italian), done.Steps.Single().Skipped.Single().Message);
|
||||
Assert.Equal(Loc.Get("Archive.Convert.Skipped.SameFormat", italian), done.Steps.Single().Skipped.Single().Message);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -301,23 +307,140 @@ public sealed class ConvertTests(TestContext context)
|
||||
Assert.True((await server.Database.Frames.ListBySequenceAsync(id)).All(f => f.Format == ImageFileFormat.Dng), "converted");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConfirmationConvertsThePhotosItsDryRunShowedWhateverChangedOnDiskSince()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var admin = await server.AdminAsync();
|
||||
Guid id = default;
|
||||
for (var i = 0; i < 3; i++) id = (await Uploads.UploadAsync(editor, Photo(i), $"IMG_{i + 1:0000}.JPG", sessionKey: "roof")).SequenceId;
|
||||
await UntilIdleAsync(server);
|
||||
var before = await server.Database.Frames.ListBySequenceAsync(id);
|
||||
// Gone on disk alone while the dry run is made (a sync client moving it): the dry run says it stays as it is.
|
||||
var file = Path.Combine(server.Options.ArchivePath, before[0].RelativePath);
|
||||
var bytes = await File.ReadAllBytesAsync(file);
|
||||
File.Delete(file);
|
||||
var request = new ConvertRequest([id], false, "tiff16", null, "replace", DryRun: true);
|
||||
var plan = await DryRunAsync(admin, request);
|
||||
Assert.Equal((2, 1, "missing"), (plan.Photos, plan.Skipped, plan.Steps.Single().Skipped.Single().Reason));
|
||||
|
||||
// Back before the confirmation, which the index cannot tell: the plan kept is confirmed, and its job converts the two
|
||||
// photos it showed, not the third.
|
||||
await File.WriteAllBytesAsync(file, bytes);
|
||||
JobDto job;
|
||||
using (var applied = await admin.PostAsJsonAsync(Url, request with { DryRun = false, Fingerprint = plan.Fingerprint }, ProtocolJson.Default.ConvertRequest))
|
||||
{
|
||||
await applied.ExpectAsync(HttpStatusCode.Accepted);
|
||||
job = await applied.ReadAsync(ProtocolJson.Default.JobDto);
|
||||
}
|
||||
await UntilJobAsync(admin, job.Id, "done");
|
||||
var after = await server.Database.Frames.ListBySequenceAsync(id);
|
||||
Assert.Equal((before[0].Sha256, ImageFileFormat.Jpeg), (after[0].Sha256, after[0].Format));
|
||||
Assert.True(after[1].Format == ImageFileFormat.Tiff && after[2].Format == ImageFileFormat.Tiff, "converted");
|
||||
using var trash = await admin.GetAsync("/api/v1/archive/trash");
|
||||
Assert.Contains("replaced", await trash.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ADryRunAndItsConfirmationSeeTheJobsAsTheyAreNow()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var admin = await server.AdminAsync();
|
||||
var plans = server.Services.GetRequiredService<PlanCache>();
|
||||
Guid id = default;
|
||||
for (var i = 0; i < 2; i++) id = (await Uploads.UploadAsync(editor, Photo(i), $"IMG_{i + 1:0000}.JPG", sessionKey: "roof")).SequenceId;
|
||||
await UntilIdleAsync(server);
|
||||
var request = new ConvertRequest([id], false, "tiff16", null, "replace", DryRun: true);
|
||||
Job Held(JobKind kind) => new()
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Kind = kind,
|
||||
TargetIds = [id],
|
||||
State = JobState.Paused,
|
||||
Created = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
// A paused pass holds the sequence: busy.
|
||||
var thumbs = Held(JobKind.Thumbs);
|
||||
await server.Database.Jobs.EnqueueAsync(thumbs);
|
||||
var busy = await DryRunAsync(admin, request);
|
||||
Assert.Equal((0, "busy"), (busy.Photos, busy.Steps.Single().Blocked.Single().Reason));
|
||||
// It ends: the plan kept shows the sequence free at once, not minutes later.
|
||||
await server.Database.Jobs.SetStateAsync(thumbs.Id, JobState.Done);
|
||||
var free = await DryRunAsync(admin, request);
|
||||
Assert.Equal((2, 0), (free.Photos, free.Steps.Single().Blocked.Count));
|
||||
Assert.Equal(1, plans.Count);
|
||||
|
||||
// Another tab queued a conversion of it since: the confirmation of this dry run is refused with the plan now.
|
||||
await server.Database.Jobs.EnqueueAsync(Held(JobKind.Convert));
|
||||
using (var refused = await admin.PostAsJsonAsync(Url, request with { DryRun = false, Fingerprint = free.Fingerprint }, ProtocolJson.Default.ConvertRequest))
|
||||
Assert.Equal(busy.Fingerprint, (await refused.ExpectErrorAsync(HttpStatusCode.Conflict, "plan_changed")).Details);
|
||||
Assert.Empty((await server.Database.Jobs.ListAsync(JobState.Queued, JobKind.Convert)).Items);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConfirmationTakesOnlyThePlanKeptAndQueuesOneJob()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var admin = await server.AdminAsync();
|
||||
var plans = server.Services.GetRequiredService<PlanCache>();
|
||||
Guid id = default;
|
||||
for (var i = 0; i < 2; i++) id = (await Uploads.UploadAsync(editor, Photo(i), $"IMG_{i + 1:0000}.JPG", sessionKey: "roof")).SequenceId;
|
||||
await UntilIdleAsync(server);
|
||||
var archive = (await server.Database.Archives.ListAsync()).Single().Id;
|
||||
var request = new ConvertRequest([id], false, "tiff16", null, "new-sequence", DryRun: true);
|
||||
var plan = await DryRunAsync(admin, request);
|
||||
var apply = request with { DryRun = false, Fingerprint = plan.Fingerprint };
|
||||
|
||||
// Let go since (past its lifetime): the confirmation does not plan inside its own request; the panel asks the dry run again.
|
||||
plans.Forget(ConvertPlanKey.Of(archive, await server.Database.Sequences.GetConvertStampAsync(archive, [id]), [id],
|
||||
new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Tiff16)));
|
||||
using (var dropped = await admin.PostAsJsonAsync(Url, apply, ProtocolJson.Default.ConvertRequest))
|
||||
await dropped.ExpectErrorAsync(HttpStatusCode.ServiceUnavailable, "plan_dropped");
|
||||
Assert.Equal(0, plans.Count);
|
||||
Assert.Equal(plan.Fingerprint, (await DryRunAsync(admin, request)).Fingerprint);
|
||||
|
||||
// Two tabs confirm it at once: one job, one new sequence.
|
||||
var answers = await Task.WhenAll(admin.PostAsJsonAsync(Url, apply, ProtocolJson.Default.ConvertRequest),
|
||||
admin.PostAsJsonAsync(Url, apply, ProtocolJson.Default.ConvertRequest));
|
||||
try
|
||||
{
|
||||
Assert.SequenceEqual([HttpStatusCode.Accepted], answers.Select(a => a.StatusCode).Where(s => s == HttpStatusCode.Accepted));
|
||||
Assert.True(answers.Any(a => a.StatusCode is HttpStatusCode.Conflict or HttpStatusCode.ServiceUnavailable), "the other is refused");
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach (var answer in answers) answer.Dispose();
|
||||
}
|
||||
var jobs = (await server.Database.Jobs.ListAsync(null, JobKind.Convert)).Items;
|
||||
Assert.Count(1, jobs);
|
||||
await UntilJobAsync(admin, jobs[0].Id, "done");
|
||||
Assert.Equal(2, (await server.Database.Sequences.ListLocationsAsync(archive)).Count);
|
||||
}
|
||||
|
||||
private static async Task<ConvertPlanDto> DryRunAsync(HttpClient client, ConvertRequest request)
|
||||
{
|
||||
using var response = await client.PostAsJsonAsync(Url, request with { DryRun = true }, ProtocolJson.Default.ConvertRequest);
|
||||
return await response.ReadAsync(ProtocolJson.Default.ConvertPlanDto);
|
||||
}
|
||||
|
||||
/// <summary>Every reason a photo stays as it is has its wire name and its sentence in the catalog (en and it).</summary>
|
||||
/// <summary>Every reason a photo stays as it is has its wire name and its sentences in the catalog (en and it), in either mode's words.</summary>
|
||||
[Test]
|
||||
public void EverySkipReasonHasItsWireNameAndSentence()
|
||||
{
|
||||
Assert.SequenceEqual(
|
||||
["same_format", "convert_on_pc", "convert_on_pc", "undecodable", "missing", "unhashed", "eight_bit", "color_profile", "too_large"],
|
||||
["same_format", "convert_on_pc", "convert_on_pc", "undecodable", "missing", "unhashed", "eight_bit", "color_profile", "too_large", "inaccessible"],
|
||||
Enum.GetValues<Chronos.Core.Conversion.ConvertSkip>().Select(Chronos.Server.Api.ArchiveEndpoints.Wire));
|
||||
foreach (var reason in Enum.GetValues<Chronos.Core.Conversion.ConvertSkip>())
|
||||
{
|
||||
foreach (var culture in (string[])["en", "it"])
|
||||
Assert.NotNull(Chronos.Core.Localization.Loc.Get("Archive.Convert.Skipped." + reason, System.Globalization.CultureInfo.GetCultureInfo(culture)));
|
||||
{
|
||||
foreach (var words in (string[])["Skipped", "NotCopied"])
|
||||
Assert.NotNull(Loc.Get($"Archive.Convert.{words}.{reason}", CultureInfo.GetCultureInfo(culture)), $"{words}.{reason} {culture}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -100,6 +100,38 @@ public sealed class PlanCacheTests
|
||||
Assert.Equal(3 + PlanCache.Capacity, made);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConfirmationTakesOnlyAPlanKeptWithinItsLifetime()
|
||||
{
|
||||
var clock = new SteppedTimeProvider();
|
||||
using var plans = new PlanCache(clock);
|
||||
var archive = Guid.NewGuid();
|
||||
var key = ConvertPlanKey.Of(archive, "stamp", null, new ConvertOptions(ConvertTarget.LinearDng));
|
||||
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var made = new ConvertPlan(archive, new ConvertOptions(ConvertTarget.LinearDng), ConvertMode.Replace, [], new ConvertSpace(0, null));
|
||||
async Task<ConvertPlan> Plan(CancellationToken stop)
|
||||
{
|
||||
await release.Task.WaitAsync(stop);
|
||||
return made;
|
||||
}
|
||||
|
||||
// Never asked, then still being made: nothing to confirm, and nothing is planned for the asking.
|
||||
Assert.Null(plans.Kept(key));
|
||||
var making = plans.GetOrPlanAsync(key, Plan);
|
||||
Assert.Null(plans.Kept(key));
|
||||
release.SetResult();
|
||||
await making;
|
||||
Assert.True(ReferenceEquals(made, await plans.Kept(key)!), "kept");
|
||||
// Past its lifetime: let go, whatever its timer.
|
||||
clock.Advance(PlanCache.Lifetime);
|
||||
Assert.Null(plans.Kept(key));
|
||||
Assert.Equal(0, plans.Count);
|
||||
// A refusal kept is a refusal for the confirmation too.
|
||||
var unknown = ConvertPlanKey.Of(archive, "stamp", [Guid.NewGuid()], new ConvertOptions(ConvertTarget.LinearDng));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => plans.GetOrPlanAsync(unknown, _ => throw new ArgumentException("Not in the archive.")));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => plans.Kept(unknown)!);
|
||||
}
|
||||
|
||||
/// <summary>Waits until <paramref name="condition"/> holds.</summary>
|
||||
private static async Task EventuallyAsync(Func<bool> condition, [System.Runtime.CompilerServices.CallerArgumentExpression(nameof(condition))] string? expression = null)
|
||||
{
|
||||
|
||||
@@ -71,6 +71,21 @@ public sealed partial class AdminConvertTests(TestContext context)
|
||||
Assert.Contains(@"const PREVIEW_MODE = ""replace"";", dialog);
|
||||
Assert.Contains(@"t(copies ? ""Admin.Convert.TotalsNew"" : ""Admin.Convert.Totals""", dialog);
|
||||
Assert.Contains(@"plural(""Admin.Convert.ConfirmNewNotCopied"", notCopied)", dialog);
|
||||
// Only the sequences copied lack photos (one that converts nothing gets no copy), counted in the totals, the
|
||||
// confirmation and each of its lines.
|
||||
Assert.Contains("formatNumber(copies ? leftOut(converting(plan.steps)) : plan.skipped)", dialog);
|
||||
Assert.Contains("const notCopied = leftOut(going);", dialog);
|
||||
Assert.Contains(@"t(""Admin.Convert.PlanItemNotCopied"", item, plural(""Admin.Convert.NotCopiedCount"", left))", dialog);
|
||||
// The server confirms only the plan it keeps: Apply asks the dry run first (the totals saying how long it takes when
|
||||
// it is made again), confirms the same fingerprint, shows a different one, and asks again once for a plan let go.
|
||||
Assert.Contains("now = await waiting(() => dryRun(body, shownPlan.key));", dialog);
|
||||
Assert.Contains("if (now.fingerprint !== shownPlan.fingerprint) {", dialog);
|
||||
Assert.Contains(@"if (err?.code === ""plan_dropped"" && attempt < DROPPED_CONFIRMS && dialog.open) continue;", dialog);
|
||||
// Asking that dry run sends nothing to convert: the fields and Apply wait, Close and Esc stay; only the confirmation
|
||||
// itself holds the dialog open.
|
||||
Assert.Contains("applyButton.disabled = busy || checking || !current || reason !== null;", dialog);
|
||||
Assert.Contains("closeButton.disabled = busy;", dialog);
|
||||
Assert.Contains("if (busy) e.preventDefault();", dialog);
|
||||
// A dry run the server is still making is asked again (a proxy cut the request off, or the server let the plan go)
|
||||
// while the totals say how long it takes; a confirmation the server let go plans again, one that changed says so,
|
||||
// and one with no answer is not sent again (its job may be queued).
|
||||
@@ -166,13 +181,14 @@ public sealed partial class AdminConvertTests(TestContext context)
|
||||
Assert.Equal(("same_format", 2), (skipped.GetProperty("reason").GetString(), skipped.GetProperty("photos").GetInt32()));
|
||||
Assert.Equal(Loc.Get("Archive.Convert.Skipped.SameFormat", italian), skipped.GetProperty("message").GetString());
|
||||
}
|
||||
// Into a new sequence, the same photos are not copied into it, and the table says so.
|
||||
// Into a new sequence: none is made of a sequence that converts nothing, so its photos stay as they are, and the table
|
||||
// says so.
|
||||
using (var same = await browser.SendAsync(HttpMethod.Post, Url, Body("jpeg", "90", "new-sequence", "\"dryRun\":true")))
|
||||
{
|
||||
using var plan = JsonDocument.Parse(await same.Content.ReadAsStringAsync());
|
||||
var skipped = plan.RootElement.GetProperty("steps")[0].GetProperty("skipped").EnumerateArray().Single();
|
||||
Assert.Equal(("same_format", 2), (skipped.GetProperty("reason").GetString(), skipped.GetProperty("photos").GetInt32()));
|
||||
Assert.Equal(Loc.Get("Archive.Convert.NotCopied.SameFormat", italian), skipped.GetProperty("message").GetString());
|
||||
Assert.Equal(Loc.Get("Archive.Convert.Skipped.SameFormat", italian), skipped.GetProperty("message").GetString());
|
||||
}
|
||||
// A plan that no longer matches is refused with the one there is now: the dialog plans again.
|
||||
using (var stale = await browser.SendAsync(HttpMethod.Post, Url, Body("linear-dng", "null", "replace", $"\"dryRun\":false,\"fingerprint\":\"{new string('0', 64)}\"")))
|
||||
|
||||
Reference in new issue
Block a user