E5: archivio delle copie automatiche, segnale e domande 48–55

La tappa E5 copia da sola i file nuovi nelle cartelle con «Automatica».
Questo pacchetto (WP2) dà al motore che verrà ciò che gli serve dal
database, senza migrazioni:

- ExportSelection.Automatic: i file dovuti, non copiati («Tolto» e
  «Superato» mai), nuovi dal «Solo i nuovi», di elementi assestati (niente
  file da scaricare, riga del giornale scritta); i «Non riuscito» solo al
  giro di recupero, tutti e ogni giro (risposta 50 di Alberto), così un
  file che non riesce mai non fa un lavoro ogni due minuti.
- HasAutomaticCandidatesAsync, CountAutomaticBacklogAsync (la domanda
  «Copia anche gli N», i file in più, una cartella nuova), i candidati di
  «Riconosci le copie già presenti» (un «Tolto» resta «Tolto», risposta
  51) e i byte del primo file in attesa di un lavoro in pausa.
- La pausa di una destinazione si scrive solo con metodi suoi, così un
  salvataggio dall'interfaccia non la annulla; l'evento resta uno per
  passaggio.
- AutoExportSignal, un'istanza per le prove, Default per l'applicazione.

Prove nuove su un database vero, confrontate con il piano di «Copia in…»,
e misure su 200.000 elementi entro 2 s senza indici nuovi.

Documenti: in §16 le righe 48–55 con le risposte del 4 dicembre 2026 e in
decisioni.md la riga con le risposte e le tre letture del progetto.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-04 13:21:56 +01:00
1 parent df50c32e22
commit f2568b5d88
12 files changed
+1212 -63

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
namespace Febe.Core.Export;
/// <summary>
/// Wakes the automatic export (tappa E5, decision D4): a line of <c>_febe.jsonl</c> was written, a rebuild ended, a
/// destination was turned on, a job ended. The signal only makes the next pass sooner: what to copy always comes from
/// the database. An instance, not a static, so parallel tests give their worker one of their own and cannot wake each
/// other; production code uses <see cref="Default"/>.
/// </summary>
public sealed class AutoExportSignal
{
private readonly Lock _gate = new();
private TaskCompletionSource<bool> _next = NewSignal();
private long _count;
/// <summary>The signal of the application (the journal hook, the rebuild, the API).</summary>
public static AutoExportSignal Default { get; } = new();
/// <summary>How many times <see cref="Signal"/> was called (monotonic, for tests).</summary>
public long Count => Interlocked.Read(ref _count);
/// <summary>
/// Completes the task of <see cref="Next"/> with <paramref name="urgent"/> and arms a new one. Urgent (a destination
/// turned on, «Riprendi ora») means the worker skips its settle wait.
/// </summary>
public void Signal(bool urgent = false)
{
TaskCompletionSource<bool> fired;
lock (_gate)
{
fired = _next;
_next = NewSignal();
_count++;
}
fired.TrySetResult(urgent);
}
/// <summary>
/// A task that completes at the next <see cref="Signal"/>, with whether that signal was urgent. Take it before a pass,
/// so a signal that arrives during the pass is not lost; a second signal before the task is awaited arms a new task
/// (take <see cref="Next"/> again to see it, for example during the settle wait).
/// </summary>
public Task<bool> Next()
{
lock (_gate)
{
return _next.Task;
}
}
private static TaskCompletionSource<bool> NewSignal() => new(TaskCreationOptions.RunContinuationsAsynchronously);
}
+18
View File
@@ -21,6 +21,24 @@ public abstract record ExportSelection
/// <summary>Every file of an account (the account card).</summary>
public sealed record Account(long AccountId) : ExportSelection;
/// <summary>
/// The automatic export (tappa E5, D4 and D5): per automatic destination, the files it is the effective destination
/// of, not copied («Tolto» and «Superato» never), new since its «Solo i nuovi» instant, and settled (no file of the
/// entry still to download, the entry not in <paramref name="HeldEntryIds"/>).
/// </summary>
/// <param name="Destinations">The automatic destinations of the pass; one not in use (retired, unknown) gives nothing.</param>
/// <param name="HeldEntryIds">Entries whose line of <c>_febe.jsonl</c> is still to be written (<c>libreria.giornale-da-scrivere</c>).</param>
/// <param name="Round">
/// A recovery round (start-up and every hour): files whose last copy in the destination is «Non riuscito» are
/// candidates too, every round, whatever the error (answer 50 of 4 December 2026). Outside a round they wait for it,
/// so a file that keeps failing does not make a job at every signal.
/// </param>
public sealed record Automatic(IReadOnlyList<AutomaticScope> Destinations, IReadOnlyList<long> HeldEntryIds, bool Round) : ExportSelection;
/// <summary>One automatic destination and its «Solo i nuovi» instant (null: everything due, saved before or after).</summary>
/// <param name="Since">Files saved in the library at or after it (<c>media.downloaded_at</c>); a file without that time is not new.</param>
public sealed record AutomaticScope(long DestinationId, DateTimeOffset? Since);
}
/// <summary>How the instants of «Fotogrammi» are chosen (spec §4.10 «Fotogrammi»).</summary>
+15 -30
View File
@@ -13,29 +13,20 @@ public static class ExportPause
public const string PausedEventCode = "esportazione-in-pausa";
/// <summary>
/// Pauses a destination with an Italian reason (read-modify-write on the stored row; a retired row is left as it
/// is). The event is written only on the transition from running to paused. Returns the stored row.
/// Pauses a destination with an Italian reason. Only the pause and its reason are written, in one statement (tappa
/// E5, D10), so a save of the destination from the interface and a copy or a round never undo each other's writes; a
/// retired row is left as it is. The event is written only on the transition from running to paused. Returns the
/// stored row.
/// </summary>
public static async Task<ExportDestination> PauseAsync(ExportEnvironment env, ExportDestination destination, string message)
{
ArgumentNullException.ThrowIfNull(env);
ArgumentNullException.ThrowIfNull(destination);
ArgumentException.ThrowIfNullOrWhiteSpace(message);
var stored = await env.Storage.Exports.GetDestinationAsync(destination.Id, CancellationToken.None).ConfigureAwait(false) ?? destination;
if (stored.IsRetired)
{
return stored;
}
var wasPaused = stored.PausedAt is not null;
if (wasPaused && string.Equals(stored.LastError, message, StringComparison.Ordinal))
{
return stored;
}
var paused = stored with { PausedAt = stored.PausedAt ?? env.Clock.Now, LastError = message };
await env.Storage.Exports.UpdateDestinationAsync(paused, CancellationToken.None).ConfigureAwait(false);
if (!wasPaused)
var exports = env.Storage.Exports;
var paused = await exports.SetDestinationPausedAsync(destination.Id, env.Clock.Now, message, CancellationToken.None).ConfigureAwait(false);
var stored = await exports.GetDestinationAsync(destination.Id, CancellationToken.None).ConfigureAwait(false) ?? destination;
if (paused)
{
await env.Storage.Events.AppendAsync(
new EventRecord
@@ -43,28 +34,22 @@ public static class ExportPause
At = env.Clock.Now,
Code = PausedEventCode,
Severity = EventSeverity.Warning,
Message = $"{ExportDestinations.Label(paused)} in pausa: {message}",
AccountId = paused.AccountId,
Message = $"{ExportDestinations.Label(stored)} in pausa: {message}",
AccountId = stored.AccountId,
},
CancellationToken.None).ConfigureAwait(false);
}
return paused;
return stored;
}
/// <summary>Clears the pause of a destination after a check succeeded. Returns the stored row.</summary>
/// <summary>Clears the pause of a destination after a check succeeded (only the pause and its reason). Returns the stored row.</summary>
public static async Task<ExportDestination> ClearAsync(ExportEnvironment env, ExportDestination destination)
{
ArgumentNullException.ThrowIfNull(env);
ArgumentNullException.ThrowIfNull(destination);
var stored = await env.Storage.Exports.GetDestinationAsync(destination.Id, CancellationToken.None).ConfigureAwait(false) ?? destination;
if (stored.IsRetired || (stored.PausedAt is null && stored.LastError is null))
{
return stored;
}
var cleared = stored with { PausedAt = null, LastError = null };
await env.Storage.Exports.UpdateDestinationAsync(cleared, CancellationToken.None).ConfigureAwait(false);
return cleared;
var exports = env.Storage.Exports;
await exports.ClearDestinationPauseAsync(destination.Id, CancellationToken.None).ConfigureAwait(false);
return await exports.GetDestinationAsync(destination.Id, CancellationToken.None).ConfigureAwait(false) ?? destination;
}
}
+11
View File
@@ -46,6 +46,14 @@ public static class ExportText
public const string CopiedInAnotherFormat = "CopiedInAnotherFormat";
public const string LostTracks = Mp4Remuxer.LostTracks;
public const string LostVideoMetadata = Mp4Remuxer.LostVideoMetadata;
// Tappa E5. A path too long for the destination: «Non riuscito» like any other, retried by every recovery round
// (answer 50 of 4 December 2026: no class of failures that never retry by themselves).
public const string PathTooLong = "PathTooLong";
// «Riconosci le copie già presenti» (tappa E5, D13): nothing at the paths looked at, or another file there.
public const string NotFound = "NotFound";
public const string Different = "Different";
}
/// <summary>Name of the state of a file of a job.</summary>
@@ -148,6 +156,9 @@ public static class ExportText
Notes.NotStill => "Il video è cambiato: il file di adesso non è un video fermo, o non è stato analizzato",
Notes.TransparencyWhite => "La trasparenza è diventata bianco",
Notes.CopiedInAnotherFormat => "Già copiato in un altro formato",
Notes.PathTooLong => "Percorso troppo lungo per la cartella",
Notes.NotFound => "Non c'è nella cartella",
Notes.Different => "Nel percorso c'è un altro file",
ImageConversionLosses.LostTexts => "Non tenuti: testi",
ImageConversionLosses.LostColorChunks => "Non tenuti: dati del colore",
ImageConversionLosses.LostIcc => "Non tenuto: profilo ICC",