From 0ff748db6f33c0e68269d0c20110d3a9a2959728 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Fri, 4 Dec 2026 21:16:35 +0100 Subject: [PATCH] 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 --- src/Febe.Core/Export/ExportService.cs | 48 +++++++++++++++++-- .../Export/ExportServiceAutomaticTests.cs | 8 ++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/Febe.Core/Export/ExportService.cs b/src/Febe.Core/Export/ExportService.cs index ee07805..eeb7480 100644 --- a/src/Febe.Core/Export/ExportService.cs +++ b/src/Febe.Core/Export/ExportService.cs @@ -25,6 +25,7 @@ public sealed partial class ExportService : IAsyncDisposable /// A frames request over . 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, ). 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 or - /// without side effects; nothing to copy gives - /// 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. + /// without side effects; nothing to copy (also when the files due are + /// recorded but not on the disk, which the plan leaves out) gives + /// 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. /// public async Task 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); } + /// + /// The check before an automatic start takes its lease: whether a file due for is in the + /// library and on the disk too. One EXISTS when nothing is due (); + /// 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, ) do not pre-empt the library conversion at + /// every pass. + /// + private async Task 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 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; + } + } + /// /// Resumes unfinished automatic runs (tappa E5, D7.3) as an automatic job, with a yielding lease. Only runs of /// whose destination is still automatic and not paused are taken; the others diff --git a/tests/Febe.Tests/Export/ExportServiceAutomaticTests.cs b/tests/Febe.Tests/Export/ExportServiceAutomaticTests.cs index be2b605..010e746 100644 --- a/tests/Febe.Tests/Export/ExportServiceAutomaticTests.cs +++ b/tests/Febe.Tests/Export/ExportServiceAutomaticTests.cs @@ -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);