E5: il giro non si ripete a vuoto e le copie automatiche cedono davvero
Il passaggio che segue la ripresa di un giro non rifà il lavoro di recupero (prova delle cartelle in pausa, ripresa dei lavori fermati da un errore): un lavoro che si ferma di nuovo aspetta il giro dopo invece di essere ripreso all'infinito senza dormire. Il giro misura lo spazio per il lavoro automatico che riprenderebbe, non per una pausa di Alberto, così una pausa per spazio non va e viene con un evento nuovo ogni volta. «Annulla» tiene lontane le partenze automatiche finché il lavoro è annullato; «Esporta questo account» spento ferma subito la copia automatica; «Cerca i video fermi» chiede di fermarsi a una copia automatica partita dopo di lei; un file che manca sul disco non crea più un lavoro automatico a ogni passaggio. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
93f7978d9e
commit
309f4d0402
12 files changed
+436
-32
No files matched your search
@@ -236,12 +236,21 @@ public sealed class AutoExportWorker
|
||||
/// (those stopped with an error only at the round); else a job over every automatic destination not paused.
|
||||
/// </summary>
|
||||
/// <param name="round">A recovery round: «Non riuscito» copies are tried again too (answer 50).</param>
|
||||
public async Task<AutoExportPass> RunOnceAsync(bool round, CancellationToken ct = default)
|
||||
public Task<AutoExportPass> RunOnceAsync(bool round, CancellationToken ct = default) => RunOnceAsync(round, recover: round, ct);
|
||||
|
||||
/// <summary>
|
||||
/// One pass. <paramref name="recover"/> does the round's recovery work (orphan runs cancelled, paused destinations
|
||||
/// tried again, runs stopped by an error resumed); <paramref name="round"/> only chooses the round's selection, which
|
||||
/// tries «Non riuscito» again. The pass that follows a round's resume looks for files as the round would, but never
|
||||
/// redoes its recovery: a run that fails or pauses again waits for the next real round instead of being resumed at
|
||||
/// once, again and again (engineering B4).
|
||||
/// </summary>
|
||||
internal async Task<AutoExportPass> RunOnceAsync(bool round, bool recover, CancellationToken ct = default)
|
||||
{
|
||||
await _pass.WaitAsync(ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
return await PassAsync(round, ct).ConfigureAwait(false);
|
||||
return await PassAsync(round, recover, ct).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -250,11 +259,11 @@ public sealed class AutoExportWorker
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<AutoExportPass> PassAsync(bool round, CancellationToken ct)
|
||||
private async Task<AutoExportPass> PassAsync(bool round, bool recover, CancellationToken ct)
|
||||
{
|
||||
var service = await ServiceAsync(ct).ConfigureAwait(false);
|
||||
var exports = _env.Storage.Exports;
|
||||
if (round)
|
||||
if (recover)
|
||||
{
|
||||
// Ghost runs: of a destination retired, switched off, or of an account removed (engineering A10).
|
||||
await service.CancelOrphanAutomaticRunsAsync(ct).ConfigureAwait(false);
|
||||
@@ -281,7 +290,7 @@ public sealed class AutoExportWorker
|
||||
}
|
||||
|
||||
var destinations = await exports.ListDestinationsAsync(includeRetired: false, ct).ConfigureAwait(false);
|
||||
if (round && destinations.Any(d => d.PausedAt is not null))
|
||||
if (recover && destinations.Any(d => d.PausedAt is not null))
|
||||
{
|
||||
foreach (var paused in destinations.Where(d => d.PausedAt is not null))
|
||||
{
|
||||
@@ -289,7 +298,8 @@ public sealed class AutoExportWorker
|
||||
try
|
||||
{
|
||||
// Answers again: the pause goes (no event); still not: it stays, with no new event (D10, D11).
|
||||
await service.ProbeDestinationAsync(paused.Id, ct).ConfigureAwait(false);
|
||||
// Room is measured for the automatic runs it would resume, not for a paused job of Alberto's.
|
||||
await service.ProbeDestinationAsync(paused.Id, forAutomatic: true, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is KeyNotFoundException or InvalidOperationException)
|
||||
{
|
||||
@@ -305,7 +315,7 @@ public sealed class AutoExportWorker
|
||||
.Where(r => r.Origin == ExportRunOrigin.Automatic)
|
||||
.ToList();
|
||||
var resumable = unfinished
|
||||
.Where(r => r.Kind == ExportRunKind.Copy && usable.ContainsKey(r.DestinationId) && (round || r.LastError is null))
|
||||
.Where(r => r.Kind == ExportRunKind.Copy && usable.ContainsKey(r.DestinationId) && (recover || r.LastError is null))
|
||||
.Select(r => r.Id)
|
||||
.ToList();
|
||||
if (resumable.Count > 0)
|
||||
@@ -439,7 +449,7 @@ public sealed class AutoExportWorker
|
||||
var pass = AutoExportPass.Idle;
|
||||
try
|
||||
{
|
||||
pass = await RunOnceAsync(isRound || followUpRound, ct).ConfigureAwait(false);
|
||||
pass = await RunOnceAsync(isRound || followUpRound, recover: isRound, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||||
{
|
||||
|
||||
@@ -165,6 +165,9 @@ public sealed partial class ExportService : IAsyncDisposable
|
||||
/// <summary>Tests only: awaited after each run an automatic plan records, to act in the middle of a plan.</summary>
|
||||
internal Func<CancellationToken, Task>? AutomaticRunRecorded { get; set; }
|
||||
|
||||
/// <summary>Tests only: awaited by «Annulla» after it looked at the running job, before it writes «annullato».</summary>
|
||||
internal Func<CancellationToken, Task>? DiscardChecked { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// After a restart: a job that was running becomes «interrotto» (paused and interrupted ones stay), and the
|
||||
/// <c>.part</c> files of unfinished jobs are removed in the background, only in their items' folders. Never resumes
|
||||
@@ -271,6 +274,7 @@ public sealed partial class ExportService : IAsyncDisposable
|
||||
}
|
||||
|
||||
var frameItems = 0;
|
||||
var accounts = new Dictionary<long, Account?>();
|
||||
try
|
||||
{
|
||||
await foreach (var file in Planner.EnumerateAsync(request, effective, settings, ct, lengthOf).ConfigureAwait(false))
|
||||
@@ -288,6 +292,13 @@ public sealed partial class ExportService : IAsyncDisposable
|
||||
continue;
|
||||
}
|
||||
|
||||
// A library file the database has but the disk has not cannot be written: recorded, it would end
|
||||
// «Mancante» in a new automatic run at every pass while it stays missing (answer 54: only files to write).
|
||||
if (scopes is not null && !await SourceIsThereAsync(file.Candidate, accounts, ct).ConfigureAwait(false))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!runs.TryGetValue(destination, out var run))
|
||||
{
|
||||
run = await exports.CreateRunAsync(
|
||||
@@ -407,6 +418,19 @@ public sealed partial class ExportService : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Whether the library file of a planned copy is on disk now (its account looked up once per plan).</summary>
|
||||
private async Task<bool> SourceIsThereAsync(ExportCandidate candidate, Dictionary<long, Account?> accounts, CancellationToken ct)
|
||||
{
|
||||
if (!accounts.TryGetValue(candidate.AccountId, out var account))
|
||||
{
|
||||
account = await _env.Storage.Accounts.GetAsync(candidate.AccountId, ct).ConfigureAwait(false);
|
||||
accounts[candidate.AccountId] = account;
|
||||
}
|
||||
|
||||
var media = account is null ? null : await _env.Storage.Media.GetAsync(candidate.MediaId, ct).ConfigureAwait(false);
|
||||
return media is not null && _env.ResolveSourcePath(account!, media) is not null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The length of a video the library does not know, from its headers (only the library file, never the
|
||||
/// destination); null when it cannot be read now (the job then gives it one instant, and the copy tells).
|
||||
@@ -598,26 +622,49 @@ public sealed partial class ExportService : IAsyncDisposable
|
||||
|
||||
/// <summary>
|
||||
/// «Annulla»: an unfinished job becomes «annullato» (the running job is interrupted first when it holds it). Our
|
||||
/// <c>.part</c> files in its folders go; no other file is touched, and the copies made stay.
|
||||
/// <c>.part</c> files in its folders go; no other file is touched, and the copies made stay. Automatic starts stay
|
||||
/// away for the whole operation, so the worker cannot resume the run between the check and the «annullato».
|
||||
/// </summary>
|
||||
public async Task DiscardAsync(long runId, CancellationToken ct = default)
|
||||
{
|
||||
if (ActiveRunIds.Contains(runId))
|
||||
lock (_gate)
|
||||
{
|
||||
await StopAsync(automaticOnly: false, destinationId: null, ct).ConfigureAwait(false); // not «Interrompi»: nothing is held
|
||||
_changeRequests++;
|
||||
}
|
||||
|
||||
var run = await _env.Storage.Exports.GetRunAsync(runId, ct).ConfigureAwait(false) ?? throw new KeyNotFoundException($"Lavoro di copia {runId} non trovato.");
|
||||
if (run.State is ExportRunState.Completed or ExportRunState.Cancelled)
|
||||
try
|
||||
{
|
||||
return;
|
||||
}
|
||||
// An automatic resume checks the change requests under the lock where it sets the job: one that passed that
|
||||
// check before this point is the running job now, and it is stopped here.
|
||||
if (ActiveRunIds.Contains(runId))
|
||||
{
|
||||
await StopAsync(automaticOnly: false, destinationId: null, ct).ConfigureAwait(false); // not «Interrompi»: nothing is held
|
||||
}
|
||||
|
||||
await _env.Storage.Exports.UpdateRunAsync(run with { State = ExportRunState.Cancelled, CompletedAt = _env.Clock.Now }, CancellationToken.None).ConfigureAwait(false);
|
||||
await ExportRunSettings.ClearAsync(_env.Storage.Settings, runId, CancellationToken.None).ConfigureAwait(false);
|
||||
if (await _env.Storage.Exports.GetDestinationAsync(run.DestinationId, ct).ConfigureAwait(false) is { } destination)
|
||||
if (DiscardChecked is { } checkedHook)
|
||||
{
|
||||
await checkedHook(ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var run = await _env.Storage.Exports.GetRunAsync(runId, ct).ConfigureAwait(false) ?? throw new KeyNotFoundException($"Lavoro di copia {runId} non trovato.");
|
||||
if (run.State is ExportRunState.Completed or ExportRunState.Cancelled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await _env.Storage.Exports.UpdateRunAsync(run with { State = ExportRunState.Cancelled, CompletedAt = _env.Clock.Now }, CancellationToken.None).ConfigureAwait(false);
|
||||
await ExportRunSettings.ClearAsync(_env.Storage.Settings, runId, CancellationToken.None).ConfigureAwait(false);
|
||||
if (await _env.Storage.Exports.GetDestinationAsync(run.DestinationId, ct).ConfigureAwait(false) is { } destination)
|
||||
{
|
||||
await ExportLeftovers.CleanAsync(_env, run, destination, ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
await ExportLeftovers.CleanAsync(_env, run, destination, ct).ConfigureAwait(false);
|
||||
lock (_gate)
|
||||
{
|
||||
_changeRequests--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -694,7 +741,16 @@ public sealed partial class ExportService : IAsyncDisposable
|
||||
/// </summary>
|
||||
/// <exception cref="KeyNotFoundException">No such destination.</exception>
|
||||
/// <exception cref="InvalidOperationException">The destination was retired.</exception>
|
||||
public async Task<string?> ProbeDestinationAsync(long destinationId, CancellationToken ct = default)
|
||||
public Task<string?> ProbeDestinationAsync(long destinationId, CancellationToken ct = default) =>
|
||||
ProbeDestinationAsync(destinationId, forAutomatic: false, ct);
|
||||
|
||||
/// <summary>
|
||||
/// The probe as <see cref="ProbeDestinationAsync(long, CancellationToken)"/>; with <paramref name="forAutomatic"/>
|
||||
/// (the recovery round) the room is measured for the paused automatic jobs the round would resume
|
||||
/// (<see cref="IExportStore.GetAutomaticPendingBytesAsync"/>), so a small paused job of Alberto's does not clear a
|
||||
/// pause that the automatic job would hit again at once, with a new event each time.
|
||||
/// </summary>
|
||||
public async Task<string?> ProbeDestinationAsync(long destinationId, bool forAutomatic, CancellationToken ct = default)
|
||||
{
|
||||
var exports = _env.Storage.Exports;
|
||||
var destination = await exports.GetDestinationAsync(destinationId, ct).ConfigureAwait(false)
|
||||
@@ -706,7 +762,7 @@ public sealed partial class ExportService : IAsyncDisposable
|
||||
|
||||
var problem = await Copier.CheckDestinationAsync(destination, ct).ConfigureAwait(false);
|
||||
if (problem is null
|
||||
&& await exports.GetFirstPendingBytesAsync(destinationId, ct).ConfigureAwait(false) is { } bytes
|
||||
&& await (forAutomatic ? exports.GetAutomaticPendingBytesAsync(destinationId, ct) : exports.GetFirstPendingBytesAsync(destinationId, ct)).ConfigureAwait(false) is { } bytes
|
||||
&& await FreeBytesAsync(destination, ct).ConfigureAwait(false) is { } free
|
||||
&& free < bytes + ExportSpace.Margin)
|
||||
{
|
||||
@@ -1317,6 +1373,31 @@ public sealed partial class ExportService : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// «Esporta questo account» (spec §4.10). Turned off, it is serialised with the automatic copies as a change of
|
||||
/// destination: an automatic start being planned and the automatic job are stopped (their runs «interrotto», no
|
||||
/// error), and the next pass resumes them with the switch read again, so the account's remaining files are skipped
|
||||
/// (as answer 52 for «Automatica»). Manual runs are left as they are. Turned on, it only signals the automatic export.
|
||||
/// </summary>
|
||||
/// <exception cref="KeyNotFoundException">No such account.</exception>
|
||||
public async Task SetAccountExportEnabledAsync(long accountId, bool enabled, CancellationToken ct = default)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
await Destinations.SetAccountEnabledAsync(accountId, enabled, ct).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
await ChangeDestinationAsync(
|
||||
null,
|
||||
async token =>
|
||||
{
|
||||
await Destinations.SetAccountEnabledAsync(accountId, enabled, token).ConfigureAwait(false);
|
||||
return true;
|
||||
},
|
||||
ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The unfinished automatic runs whose destination is retired, gone, or no longer automatic (switched off, its
|
||||
/// account removed) become «annullato» with the reason (D7, engineering A10): nothing would ever resume them. The
|
||||
|
||||
@@ -679,7 +679,11 @@ public sealed class StillVideoService : IDisposable
|
||||
stop.ThrowIfCancellationRequested();
|
||||
}
|
||||
|
||||
/// <summary>Why the job may not analyse now, but for the viewer: an engine session or a long work.</summary>
|
||||
/// <summary>
|
||||
/// Why the job may not analyse now, but for the viewer: an engine session or a long work. An automatic copy that took
|
||||
/// the gate after the search started is asked to stop at every look (§16 answer 49: the search comes first), not
|
||||
/// only at the start, so the search never waits behind a whole automatic job.
|
||||
/// </summary>
|
||||
private string? BackgroundReason()
|
||||
{
|
||||
if (_env.Session.InSession)
|
||||
@@ -687,7 +691,17 @@ public sealed class StillVideoService : IDisposable
|
||||
return StillVideoText.InSession;
|
||||
}
|
||||
|
||||
return _env.Gate?.Busy is not null ? StillVideoText.LongWork : null;
|
||||
if (_env.Gate is not { } gate)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (gate.HeldByYielding)
|
||||
{
|
||||
gate.CancelYielding();
|
||||
}
|
||||
|
||||
return gate.Busy is not null ? StillVideoText.LongWork : null;
|
||||
}
|
||||
|
||||
/// <summary>Counts <paramref name="item"/> (with its outcome, or as left out) and takes it off the front of the queue.</summary>
|
||||
|
||||
@@ -291,6 +291,14 @@ public interface IExportStore
|
||||
/// </summary>
|
||||
Task<long?> GetFirstPendingBytesAsync(long destinationId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// The room the recovery round of the automatic export needs before it clears a pause (tappa E5, D11): the largest
|
||||
/// of the library files of the first items still «In attesa» of the destination's paused automatic copy jobs, the
|
||||
/// ones the round resumes; without such a job, as <see cref="GetFirstPendingBytesAsync"/>. A paused job of Alberto's
|
||||
/// never clears the pause for an automatic one: it waits for «Riprendi» or «Riprova» (answer 48).
|
||||
/// </summary>
|
||||
Task<long?> GetAutomaticPendingBytesAsync(long destinationId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Jobs for the Dashboard: newest first, at most <paramref name="limit"/>.</summary>
|
||||
Task<IReadOnlyList<ExportRun>> ListRecentRunsAsync(int limit, CancellationToken ct = default);
|
||||
|
||||
|
||||
Reference in new issue
Block a user