E5: una partenza automatica non ferma la conversione per un file che manca

HasAutomaticCandidatesAsync guarda solo la base dati: un file registrato
ma assente dal disco faceva prendere il cancello a ogni passata, e la
conversione della libreria si fermava per niente, prima che il piano lo
scartasse. Ora, dopo l'EXISTS, la partenza scorre i file dovuti fino al
primo presente sul disco e solo allora prende il cancello.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-04 21:16:35 +01:00
1 parent c3d9e02e22
commit 0ff748db6f
2 files changed
+51 -5

No files matched your search

+43 -5
View File
@@ -25,6 +25,7 @@ public sealed partial class ExportService : IAsyncDisposable
/// <summary>A frames request over <see cref="ExportFrames.MaxPerRequest"/>.</summary>
public const string TooManyFramesText = "Più di 20.000 fotogrammi in una richiesta: scegli meno video o un intervallo più lungo";
private const int ItemBatch = 2000;
private const int CandidatePage = 2000;
private static readonly TimeSpan ProgressInterval = TimeSpan.FromMilliseconds(250);
private readonly ExportEnvironment _env;
@@ -1159,10 +1160,10 @@ public sealed partial class ExportService : IAsyncDisposable
/// unfinished automatic run (that run is resumed instead, <see cref="TryResumeAutomaticAsync"/>). Only files to write
/// are recorded, one run per destination (answer 54). Never throws because Febe is busy: a job, an engine session, a
/// change of destination, Alberto's long work, or a hold give <see cref="AutoExportStartOutcome.Busy"/> or
/// <see cref="AutoExportStartOutcome.Held"/> without side effects; nothing to copy gives
/// <see cref="AutoExportStartOutcome.NothingToDo"/> without taking the lease, so the library conversion is not
/// pre-empted for nothing. Alberto's work arriving during the plan displaces it: the runs recorded so far become
/// «annullato» (nothing was copied) and the answer is Busy.
/// <see cref="AutoExportStartOutcome.Held"/> without side effects; nothing to copy (also when the files due are
/// recorded but not on the disk, which the plan leaves out) gives <see cref="AutoExportStartOutcome.NothingToDo"/>
/// without taking the lease, so the library conversion is not pre-empted for nothing. Alberto's work arriving during
/// the plan displaces it: the runs recorded so far become «annullato» (nothing was copied) and the answer is Busy.
/// </summary>
public async Task<AutoExportStart> TryStartAutomaticAsync(ExportSelection.Automatic selection, CancellationToken ct = default)
{
@@ -1172,7 +1173,7 @@ public sealed partial class ExportService : IAsyncDisposable
return refused;
}
if (selection.Destinations.Count == 0 || !await _env.Storage.Exports.HasAutomaticCandidatesAsync(selection, ct).ConfigureAwait(false))
if (selection.Destinations.Count == 0 || !await AnyAutomaticFileThereAsync(selection, ct).ConfigureAwait(false))
{
return new AutoExportStart(AutoExportStartOutcome.NothingToDo, []);
}
@@ -1196,6 +1197,43 @@ public sealed partial class ExportService : IAsyncDisposable
ct).ConfigureAwait(false);
}
/// <summary>
/// The check before an automatic start takes its lease: whether a file due for <paramref name="selection"/> is in the
/// library and on the disk too. One EXISTS when nothing is due (<see cref="IExportStore.HasAutomaticCandidatesAsync"/>);
/// otherwise the due files a page at a time, until the first that is there, so files the database has and the disk
/// has not (which the plan leaves out, <see cref="SourceIsThereAsync"/>) do not pre-empt the library conversion at
/// every pass.
/// </summary>
private async Task<bool> AnyAutomaticFileThereAsync(ExportSelection.Automatic selection, CancellationToken ct)
{
var exports = _env.Storage.Exports;
if (!await exports.HasAutomaticCandidatesAsync(selection, ct).ConfigureAwait(false))
{
return false;
}
var accounts = new Dictionary<long, Account?>();
long after = 0;
while (true)
{
var page = await exports.ListCandidatesAsync(selection, after, CandidatePage, ct).ConfigureAwait(false);
if (page.Count == 0)
{
return false;
}
foreach (var candidate in page)
{
if (await SourceIsThereAsync(candidate, accounts, ct).ConfigureAwait(false))
{
return true;
}
}
after = page[^1].MediaId;
}
}
/// <summary>
/// Resumes unfinished automatic runs (tappa E5, D7.3) as an automatic job, with a yielding lease. Only runs of
/// <see cref="ExportRunOrigin.Automatic"/> whose destination is still automatic and not paused are taken; the others
@@ -241,6 +241,14 @@ public sealed class ExportServiceAutomaticTests
Assert.IsFalse(conversion.IsPreempted);
Assert.IsTrue(bed.Gate.BackgroundRunning);
// A file the database has and the disk has not: the plan leaves it out, so it takes no lease either.
var gone = await bed.PostAsync(bed.Luca, "m01", 2);
File.Delete(bed.LibraryFile(bed.Luca, gone.Media[0]));
Assert.AreEqual(AutoExportStartOutcome.NothingToDo, (await bed.Service.TryStartAutomaticAsync(Selection(auto, round: false), ct)).Outcome);
Assert.AreEqual(AutoExportStartOutcome.NothingToDo, (await bed.Service.TryStartAutomaticAsync(Selection(auto, round: true), ct)).Outcome);
Assert.IsFalse(conversion.IsPreempted);
Assert.IsEmpty(await bed.Db.Exports.ListRunsAsync(ct));
await bed.PostAsync(bed.Luca, "n01", 1);
Assert.AreEqual(AutoExportStartOutcome.Started, (await bed.Service.TryStartAutomaticAsync(Selection(auto, round: true), ct)).Outcome);
Assert.IsTrue(conversion.IsPreempted);