E5: il servizio avvia, cede e ferma le copie automatiche
Il servizio delle copie impara le partenze automatiche: TryStartAutomaticAsync e TryResumeAutomaticAsync non lanciano mai eccezioni perché Febe è occupata (occupato, niente da fare senza prendere il cancello, ferme), registrano solo i file da scrivere, un lavoro per cartella, e prendono il cancello con il permesso che cede: «Copia in…», «Riprendi», «Controlla le copie» e «Riprova» lo spostano con EnterAsync e aspettano che abbia salvato i suoi stati. I cambi di cartella passano da ChangeDestinationAsync, serializzati con le partenze automatiche: spegnere «Automatica» ferma subito la copia automatica di quella cartella e annulla i suoi lavori (risposta 52); un lavoro automatico non finito non blocca più il cambio di cartella. La copia annulla da sé un lavoro automatico la cui cartella non è più automatica. «Interrompi» di una copia automatica la ferma per un'ora, in memoria, con «Riprendi ora» (risposta 53). La prova di «Riprova» guarda anche lo spazio per il prossimo file prima di togliere una pausa. «Copia automatica in … finita» solo se il lavoro ha fatto qualcosa (risposta 54). I «Non riuscito» restano candidati di ogni giro (risposta 50); il percorso troppo lungo ha la sua nota. La chiusura aspetta il lavoro al più 10 secondi. Il segnale parte dal nucleo quando una cartella si accende o prende più file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cfd0ef88d7
commit
0f69a01f88
10 files changed
+1872
-123
No files matched your search
@@ -52,10 +52,14 @@ public sealed class ExportCopier(ExportEnvironment env)
|
||||
var destination = await store.GetDestinationAsync(run.DestinationId, ct).ConfigureAwait(false);
|
||||
if (destination is null || destination.IsRetired)
|
||||
{
|
||||
run = run with { State = ExportRunState.Cancelled, CompletedAt = _env.Clock.Now, LastError = ExportText.RetiredDestination };
|
||||
await store.UpdateRunAsync(run, none).ConfigureAwait(false);
|
||||
await ExportRunSettings.ClearAsync(_env.Storage.Settings, runId, none).ConfigureAwait(false);
|
||||
return run;
|
||||
return await CancelAsync(run, ExportText.RetiredDestination).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// Tappa E5 (D7.5, answer 52): an automatic run goes on only while its destination is automatic. Retired and
|
||||
// orphan destinations are never automatic (CHECK of 0003), so this also covers them.
|
||||
if (run.Origin == ExportRunOrigin.Automatic && !destination.Automatic)
|
||||
{
|
||||
return await CancelAsync(run, ExportText.AutomaticTurnedOff).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
run = run with { State = ExportRunState.Running, LastError = null };
|
||||
@@ -121,21 +125,33 @@ public sealed class ExportCopier(ExportEnvironment env)
|
||||
var final = await store.GetRunSummaryAsync(runId, none).ConfigureAwait(false);
|
||||
run = run with { State = ExportRunState.Completed, CompletedAt = _env.Clock.Now, LastError = null };
|
||||
await store.UpdateRunAsync(run, none).ConfigureAwait(false);
|
||||
await _env.Storage.Events.AppendAsync(
|
||||
new EventRecord
|
||||
{
|
||||
At = _env.Clock.Now,
|
||||
Code = "esportazione-completata",
|
||||
Message = ExportText.CompletionMessage(final, x.Destination),
|
||||
Severity = final.ByState.GetValueOrDefault(ExportItemState.Failed) > 0 ? EventSeverity.Warning : EventSeverity.Success,
|
||||
AccountId = x.Destination.AccountId,
|
||||
},
|
||||
none).ConfigureAwait(false);
|
||||
var automatic = run.Origin == ExportRunOrigin.Automatic;
|
||||
if (!automatic || ExportText.AutomaticRunIsWorthAnEvent(final))
|
||||
{
|
||||
// Answer 54: an automatic run that copied, adopted and failed nothing leaves no trace in the history.
|
||||
await _env.Storage.Events.AppendAsync(
|
||||
new EventRecord
|
||||
{
|
||||
At = _env.Clock.Now,
|
||||
Code = "esportazione-completata",
|
||||
Message = ExportText.CompletionMessage(final, x.Destination, automatic),
|
||||
Severity = final.ByState.GetValueOrDefault(ExportItemState.Failed) > 0 ? EventSeverity.Warning : EventSeverity.Success,
|
||||
AccountId = x.Destination.AccountId,
|
||||
},
|
||||
none).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await ExportRunSettings.ClearAsync(_env.Storage.Settings, runId, none).ConfigureAwait(false);
|
||||
x.ByState = new Dictionary<ExportItemState, int>(final.ByState);
|
||||
x.Report(_env, ExportPhase.Finished, null);
|
||||
return run;
|
||||
}
|
||||
catch (AutomaticTurnedOffException)
|
||||
{
|
||||
// Between two files: nothing is half written. The files done stay copied (answer 52).
|
||||
x.Report(_env, ExportPhase.Finished, null);
|
||||
return await CancelAsync(run, ExportText.AutomaticTurnedOff).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var cancelled = ex is OperationCanceledException && ct.IsCancellationRequested;
|
||||
@@ -178,18 +194,49 @@ public sealed class ExportCopier(ExportEnvironment env)
|
||||
return problem;
|
||||
}
|
||||
|
||||
/// <summary>Before a file: the probe at the start of each group, then the wait for the end of an engine session.</summary>
|
||||
/// <summary>
|
||||
/// Before a file: the probe at the start of each group, then the wait for the end of an engine session. At the start
|
||||
/// of each group an automatic run also looks whether its destination is still automatic (tappa E5, D7.5): if not it
|
||||
/// ends «annullato» (<see cref="AutomaticTurnedOffException"/>).
|
||||
/// </summary>
|
||||
private async Task<string?> BeforeItemAsync(RunContext x, CancellationToken ct)
|
||||
{
|
||||
if (x.GroupFiles == 0 && await ProbeAsync(x, ct).ConfigureAwait(false) is { } problem)
|
||||
if (x.GroupFiles == 0)
|
||||
{
|
||||
return problem;
|
||||
if (x.Run.Origin == ExportRunOrigin.Automatic
|
||||
&& await _env.Storage.Exports.GetDestinationAsync(x.Destination.Id, ct).ConfigureAwait(false) is not { Automatic: true })
|
||||
{
|
||||
throw new AutomaticTurnedOffException();
|
||||
}
|
||||
|
||||
if (await ProbeAsync(x, ct).ConfigureAwait(false) is { } problem)
|
||||
{
|
||||
return problem;
|
||||
}
|
||||
}
|
||||
|
||||
await WaitOutsideSessionAsync(x, ct).ConfigureAwait(false);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>A run that cannot go on (its destination was retired, or is no longer automatic) becomes «annullato».</summary>
|
||||
private async Task<ExportRun> CancelAsync(ExportRun run, string reason)
|
||||
{
|
||||
run = run with { State = ExportRunState.Cancelled, CompletedAt = _env.Clock.Now, LastError = reason };
|
||||
await _env.Storage.Exports.UpdateRunAsync(run, CancellationToken.None).ConfigureAwait(false);
|
||||
await ExportRunSettings.ClearAsync(_env.Storage.Settings, run.Id, CancellationToken.None).ConfigureAwait(false);
|
||||
return run;
|
||||
}
|
||||
|
||||
/// <summary>The destination of an automatic run was switched off (or retired) while the run worked on it.</summary>
|
||||
private sealed class AutomaticTurnedOffException : Exception
|
||||
{
|
||||
public AutomaticTurnedOffException()
|
||||
: base(ExportText.AutomaticTurnedOff)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The counts of one finished item: progress, states, and the group of the probe.</summary>
|
||||
private void Account(RunContext x, ItemResult result)
|
||||
{
|
||||
@@ -816,7 +863,8 @@ public sealed class ExportCopier(ExportEnvironment env)
|
||||
}
|
||||
catch (PathTooLongException ex)
|
||||
{
|
||||
return await FailAsync(item, file, failed, ex.Message, null).ConfigureAwait(false);
|
||||
// «Non riuscito» like any other failure: the next recovery round retries it (answer 50 of 4 December 2026).
|
||||
return await FailAsync(item, file, failed, ex.Message, ExportText.Notes.PathTooLong).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (_env.Disk.MaxFileBytes(root) is { } max && output.Length > max)
|
||||
@@ -1369,6 +1417,10 @@ public sealed class ExportCopier(ExportEnvironment env)
|
||||
current,
|
||||
ExportDestinations.Label(Destination),
|
||||
new Dictionary<ExportItemState, int>(ByState),
|
||||
env.Clock.Now));
|
||||
env.Clock.Now)
|
||||
{
|
||||
Kind = Run.Kind,
|
||||
Origin = Run.Origin,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace Febe.Core.Export;
|
||||
/// <param name="NamingScheme">General scheme; null: «Come la libreria».</param>
|
||||
/// <param name="HighlightNamingScheme">Scheme of highlight collections; null: «Come la libreria».</param>
|
||||
/// <param name="ProfileNamingScheme">Scheme of profile pictures; null: «Come la libreria».</param>
|
||||
/// <param name="Automatic">«Automatica»: stored only in tappa E2 (tappa E5 acts on it).</param>
|
||||
/// <param name="Automatic">«Automatica»: the files due here are copied by themselves (tappa E5, the automatic export).</param>
|
||||
/// <param name="OnlyNew">«Solo i nuovi» when «Automatica» is turned on.</param>
|
||||
/// <param name="PhotoFormat">Format of the copies of photos here; null: «Come le impostazioni generali» (tappa E3, D1).</param>
|
||||
/// <param name="VideoFormat">Format of the copies of videos here (Original or Mp4); null: «Come le impostazioni generali».</param>
|
||||
@@ -129,7 +129,9 @@ public sealed class ExportDestinations(ExportEnvironment env)
|
||||
var row = Build(spec, full, now) with { AutomaticSince = spec.Automatic && spec.OnlyNew ? now : null };
|
||||
try
|
||||
{
|
||||
return new DestinationSaveResult(await _env.Storage.Exports.AddDestinationAsync(row, ct).ConfigureAwait(false), warnings);
|
||||
var added = await _env.Storage.Exports.AddDestinationAsync(row, ct).ConfigureAwait(false);
|
||||
SignalIf(added.Automatic);
|
||||
return new DestinationSaveResult(added, warnings);
|
||||
}
|
||||
catch (DuplicateRecordException)
|
||||
{
|
||||
@@ -187,6 +189,9 @@ public sealed class ExportDestinations(ExportEnvironment env)
|
||||
LastError = stored.LastError,
|
||||
};
|
||||
await exports.UpdateDestinationAsync(updated, ct).ConfigureAwait(false);
|
||||
|
||||
// Turned on, or more types while on (answer 55: no new question, the same «da quando»).
|
||||
SignalIf(updated.Automatic && (!stored.Automatic || (updated.ContentTypes & ~stored.ContentTypes) != ContentTypes.None));
|
||||
return new DestinationSaveResult(updated, warnings);
|
||||
}
|
||||
|
||||
@@ -201,7 +206,9 @@ public sealed class ExportDestinations(ExportEnvironment env)
|
||||
// Two transactions, as the store wants: the old folder is retired with its copies, then the new one is added.
|
||||
await exports.RetireDestinationAsync(id, now, CancellationToken.None).ConfigureAwait(false);
|
||||
var row = Build(spec, newFolder, now) with { AutomaticSince = spec.Automatic && spec.OnlyNew ? now : null };
|
||||
return new DestinationSaveResult(await exports.AddDestinationAsync(row, CancellationToken.None).ConfigureAwait(false), warnings);
|
||||
var moved = await exports.AddDestinationAsync(row, CancellationToken.None).ConfigureAwait(false);
|
||||
SignalIf(moved.Automatic);
|
||||
return new DestinationSaveResult(moved, warnings);
|
||||
}
|
||||
|
||||
/// <summary>«Togli la cartella»: only the configuration goes; copies and files stay (<see cref="RetiredText"/>).</summary>
|
||||
@@ -217,6 +224,9 @@ public sealed class ExportDestinations(ExportEnvironment env)
|
||||
|
||||
await EnsureNoUnfinishedRunAsync(id, ct).ConfigureAwait(false);
|
||||
await _env.Storage.Exports.RetireDestinationAsync(id, _env.Clock.Now, ct).ConfigureAwait(false);
|
||||
|
||||
// Without its own folder an account's files go to the general folder, which may be automatic.
|
||||
SignalIf(stored.Scope == ExportScope.Account);
|
||||
}
|
||||
|
||||
/// <summary>«Esporta questo account».</summary>
|
||||
@@ -225,6 +235,7 @@ public sealed class ExportDestinations(ExportEnvironment env)
|
||||
{
|
||||
_ = await _env.Storage.Accounts.GetAsync(accountId, ct).ConfigureAwait(false) ?? throw new KeyNotFoundException($"Account {accountId} non trovato.");
|
||||
await _env.Storage.Exports.SetAccountExportEnabledAsync(accountId, enabled, ct).ConfigureAwait(false);
|
||||
SignalIf(enabled);
|
||||
}
|
||||
|
||||
/// <summary>Checks a folder for the dialog, before saving (WP1.3).</summary>
|
||||
@@ -388,15 +399,29 @@ public sealed class ExportDestinations(ExportEnvironment env)
|
||||
return verdict.Problem is { } problem ? throw new ExportFolderException(problem.Message, problem.Kind) : verdict.FullPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A job Alberto started (or resumed) towards the destination blocks a change of folder and «Togli la cartella».
|
||||
/// Automatic runs do not (tappa E5, D7.4): <c>ExportService.ChangeDestinationAsync</c> stops them before the change
|
||||
/// and cancels them after it, and the copier cancels one whose destination is no longer automatic.
|
||||
/// </summary>
|
||||
private async Task EnsureNoUnfinishedRunAsync(long destinationId, CancellationToken ct)
|
||||
{
|
||||
var unfinished = await _env.Storage.Exports.ListUnfinishedRunsAsync(ct).ConfigureAwait(false);
|
||||
if (unfinished.Any(r => r.DestinationId == destinationId))
|
||||
if (unfinished.Any(r => r.DestinationId == destinationId && r.Origin != ExportRunOrigin.Automatic))
|
||||
{
|
||||
throw new InvalidOperationException(UnfinishedRunText);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Wakes the automatic export at once (no settle wait): what goes by itself may have grown.</summary>
|
||||
private void SignalIf(bool grew)
|
||||
{
|
||||
if (grew)
|
||||
{
|
||||
_env.AutoSignal.Signal(urgent: true);
|
||||
}
|
||||
}
|
||||
|
||||
private static ExportDestination Build(DestinationSpec spec, string root, DateTimeOffset createdAt) => new()
|
||||
{
|
||||
Scope = spec.Scope,
|
||||
|
||||
@@ -65,6 +65,12 @@ public sealed record ExportEnvironment
|
||||
/// <summary>One conversion at a time in the process (tappa E3, D16): shared with the library conversion.</summary>
|
||||
public ConversionThrottle Throttle { get; init; } = ConversionThrottle.Shared;
|
||||
|
||||
/// <summary>
|
||||
/// Woken when what goes by itself may have grown (tappa E5, D3/D4): «Automatica» turned on, types added, a folder
|
||||
/// changed or removed, «Esporta questo account» turned on (<see cref="ExportDestinations"/>). Tests give their own instance.
|
||||
/// </summary>
|
||||
public AutoExportSignal AutoSignal { get; init; } = AutoExportSignal.Default;
|
||||
|
||||
/// <summary>The checker of destination folders of this environment.</summary>
|
||||
public ExportFolderCheck CreateFolderCheck() => new(Root.Mounts, FolderCheckTimeout, WriteProbe);
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Domain;
|
||||
using Febe.Core.Storage;
|
||||
|
||||
@@ -204,8 +205,40 @@ public sealed record ExportProgress(
|
||||
public double Fraction => BytesTotal > 0
|
||||
? Math.Clamp((double)BytesDone / BytesTotal, 0, 1)
|
||||
: FilesTotal > 0 ? Math.Clamp((double)FilesDone / FilesTotal, 0, 1) : 0;
|
||||
|
||||
/// <summary>
|
||||
/// The kind of <see cref="RunId"/>'s run (tappa E5, D7.8): taken from the run being worked on, not from the job; null
|
||||
/// for «Controlla le copie» and before a run is reached.
|
||||
/// </summary>
|
||||
public ExportRunKind? Kind { get; init; }
|
||||
|
||||
/// <summary>Who started <see cref="RunId"/>'s run: an automatic run resumed with «Riprendi» stays automatic here.</summary>
|
||||
public ExportRunOrigin? Origin { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>What an automatic start did (tappa E5, D7): never an exception because Febe is busy.</summary>
|
||||
public enum AutoExportStartOutcome
|
||||
{
|
||||
/// <summary>A job started: <see cref="AutoExportStart.Runs"/> are its runs.</summary>
|
||||
Started,
|
||||
|
||||
/// <summary>Nothing to copy (or to resume): no lease was taken, no run recorded.</summary>
|
||||
NothingToDo,
|
||||
|
||||
/// <summary>
|
||||
/// Something else comes first: a job, an engine session, a change of destination, other long work
|
||||
/// (<see cref="AutoExportStart.BusyWith"/>), or Alberto's work displaced the start while it planned.
|
||||
/// </summary>
|
||||
Busy,
|
||||
|
||||
/// <summary>«Interrompi» stopped the automatic copies until <c>ExportService.AutomaticHeldUntil</c> (answer 53).</summary>
|
||||
Held,
|
||||
}
|
||||
|
||||
/// <summary>The answer of <c>ExportService.TryStartAutomaticAsync</c> and <c>TryResumeAutomaticAsync</c>.</summary>
|
||||
/// <param name="BusyWith">The long work that holds the gate, when that is why the start is <see cref="AutoExportStartOutcome.Busy"/>.</param>
|
||||
public sealed record AutoExportStart(AutoExportStartOutcome Outcome, IReadOnlyList<ExportRun> Runs, LongWorkKind? BusyWith = null);
|
||||
|
||||
/// <summary>What <c>ExportService.StartAsync</c> did: one run per destination (D6), and the plan it followed.</summary>
|
||||
public sealed record ExportStart(IReadOnlyList<ExportRun> Runs, ExportPlan Plan);
|
||||
|
||||
|
||||
@@ -41,6 +41,19 @@ public sealed class ExportPlanner(ExportEnvironment env)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
ArgumentNullException.ThrowIfNull(request.Selection, nameof(request));
|
||||
|
||||
// Tappa E5 (D5): the automatic selection is the automatic export's alone, in the destinations' own forms, never
|
||||
// «Copia di nuovo»; and an automatic run plans only the automatic selection.
|
||||
if (request.Selection is ExportSelection.Automatic != (request.Origin == ExportRunOrigin.Automatic))
|
||||
{
|
||||
throw new ArgumentException("Le copie automatiche si chiedono solo dalla copia automatica.", nameof(request));
|
||||
}
|
||||
|
||||
if (request.Selection is ExportSelection.Automatic && (request.CopyAgain || request.Formats is not null || request.Frames is not null || request.Exact is not null))
|
||||
{
|
||||
throw new ArgumentException("Una copia automatica non ricopia «Tolti» e «Superati» e usa i formati della cartella.", nameof(request));
|
||||
}
|
||||
|
||||
if (request.Frames is not null && request.Formats is not null)
|
||||
{
|
||||
throw new ArgumentException("I fotogrammi non si chiedono insieme ai formati delle copie.", nameof(request));
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -177,8 +177,11 @@ public static class ExportText
|
||||
/// <summary>«1 file copiato», «1.204 file copiati».</summary>
|
||||
public static string Count(int count, string one, string many) => count == 1 ? "1 " + one : ItalianText.Number(count) + " " + many;
|
||||
|
||||
/// <summary>The event written when a job ends: «Copia in Disco USB finita: 120 file copiati, 30 già copiati.»</summary>
|
||||
public static string CompletionMessage(ExportRunSummary summary, ExportDestination destination)
|
||||
/// <summary>
|
||||
/// The event written when a job ends: «Copia in Disco USB finita: 120 file copiati, 30 già copiati.»; for an automatic
|
||||
/// run (tappa E5) «Copia automatica in Disco USB finita: …».
|
||||
/// </summary>
|
||||
public static string CompletionMessage(ExportRunSummary summary, ExportDestination destination, bool automatic = false)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(summary);
|
||||
ArgumentNullException.ThrowIfNull(destination);
|
||||
@@ -204,9 +207,41 @@ public static class ExportText
|
||||
parts.Add(Count(Of(ExportItemState.Failed), "non riuscito", "non riusciti"));
|
||||
}
|
||||
|
||||
return $"Copia in {ExportDestinations.Label(destination)} finita: {string.Join(", ", parts)}.";
|
||||
return $"{(automatic ? "Copia automatica" : "Copia")} in {ExportDestinations.Label(destination)} finita: {string.Join(", ", parts)}.";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the end of an automatic run is worth an event (answer 54 of 4 December 2026): only when it copied or
|
||||
/// adopted something, or something did not succeed. A run that only found files already copied leaves no trace.
|
||||
/// </summary>
|
||||
public static bool AutomaticRunIsWorthAnEvent(ExportRunSummary summary)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(summary);
|
||||
return summary.ByState.GetValueOrDefault(ExportItemState.Copied)
|
||||
+ summary.ByState.GetValueOrDefault(ExportItemState.Adopted)
|
||||
+ summary.ByState.GetValueOrDefault(ExportItemState.Failed) > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Why an automatic run was cancelled (tappa E5, answer 52): its destination was switched off, retired, or lost its
|
||||
/// account meanwhile. The copies made stay.
|
||||
/// </summary>
|
||||
public const string AutomaticTurnedOff = "«Automatica» è stata spenta: i file rimasti non si copiano da soli.";
|
||||
|
||||
/// <summary>
|
||||
/// The answer to «Interrompi» of an automatic copy (answer 53): what was copied stays, and the automatic copies wait
|
||||
/// until <paramref name="until"/> («Riprendi ora» before).
|
||||
/// </summary>
|
||||
public static string AutomaticInterrupted(DateTimeOffset until, TimeZoneInfo zone) =>
|
||||
$"Copia automatica interrotta: quello che è stato copiato resta; riprende da sola alle {ItalianText.Clock(until, zone)}.";
|
||||
|
||||
/// <summary>
|
||||
/// A paused destination still without room for the next file of its paused job (tappa E5, D11): the pause stays.
|
||||
/// </summary>
|
||||
public static string NoSpaceForNext(ExportDestination destination, long free, long bytes) =>
|
||||
$"Su {ExportDestinations.Label(destination)} restano {ItalianText.Bytes(free)}: per il prossimo file ({ItalianText.Bytes(bytes)}) serve più spazio, "
|
||||
+ $"con un margine di {ItalianText.Bytes(ExportSpace.Margin)}. Libera spazio, poi riprendi.";
|
||||
|
||||
/// <summary>The event of «Controlla le copie».</summary>
|
||||
public static string CheckMessage(int checkedCopies, int removed, ExportDestination destination)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user