E5: il cancello dei lavori lunghi cede il passo ai lavori di Alberto

La copia automatica prenderà il cancello con un permesso che cede
(TryEnterYielding): esclude gli altri lavori come ogni permesso, ma
importazione, ricostruzione e zip di fotogrammi lo prendono con
EnterAsync prima del proprio lucchetto e prima di registrare qualcosa.
Chi entra diventa subito titolare, la copia automatica riceve l'ordine
di fermarsi e si aspetta che lasci, in tempo reale, al più 15 secondi;
oltre, il lavoro è rifiutato con il messaggio della copia e la copia
resta titolare finché non lascia. «Cerca i video fermi» la ferma con
CancelYielding. BusyFirm decide i rifiuti anticipati, Busy resta per
chi aspetta (conversione della libreria, video fermi).

Una ricostruzione finita risveglia la copia automatica, e la sonda del
controllo delle cartelle è riservata come i file temporanei, così la
pulizia dei resti di un altro lavoro non la toglie mentre è in uso.

Risposta 49 di Alberto: la copia automatica cede sempre il passo.
Una riga in decisioni.md. ExportService resta alla tappa successiva.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-04 13:39:15 +01:00
1 parent f2568b5d88
commit cfd0ef88d7
11 files changed
+757 -58

No files matched your search

+75 -30
View File
@@ -114,9 +114,17 @@ public sealed class ImportService : IAsyncDisposable
{
EnsureIdle();
var source = Sources.Inspect(sourcePath);
var run = await Analyzer.CreateRunAsync(source, options, ct).ConfigureAwait(false);
StartJob(run.Id, ImportPhase.Analyzing, (progress, token) => Analyzer.AnalyzeAsync(run.Id, progress, token));
return run;
var lease = await EnterGateAsync(ct).ConfigureAwait(false);
try
{
var run = await Analyzer.CreateRunAsync(source, options, ct).ConfigureAwait(false);
StartJob(run.Id, ImportPhase.Analyzing, (progress, token) => Analyzer.AnalyzeAsync(run.Id, progress, token), ref lease);
return run;
}
finally
{
lease?.Dispose(); // not handed to a job: the start failed
}
}
/// <summary>Continues an interrupted analysis or copy in the background.</summary>
@@ -124,19 +132,30 @@ public sealed class ImportService : IAsyncDisposable
{
var run = await _env.Storage.Imports.GetRunAsync(runId, ct).ConfigureAwait(false)
?? throw new KeyNotFoundException($"Importazione {runId} non trovata.");
switch (run.State)
if (run.State is not (ImportRunState.Analyzing or ImportRunState.AnalysisInterrupted or ImportRunState.Copying or ImportRunState.CopyInterrupted))
{
case ImportRunState.Analyzing or ImportRunState.AnalysisInterrupted:
StartJob(runId, ImportPhase.Analyzing, (progress, token) => Analyzer.AnalyzeAsync(runId, progress, token));
break;
case ImportRunState.Copying or ImportRunState.CopyInterrupted:
StartJob(runId, ImportPhase.Copying, (progress, token) => Copier.CopyAsync(runId, progress, token));
break;
default:
throw new InvalidOperationException("Questa importazione non è interrotta: non c'è niente da riprendere.");
throw new InvalidOperationException("Questa importazione non è interrotta: non c'è niente da riprendere.");
}
return run;
EnsureIdle();
var lease = await EnterGateAsync(ct).ConfigureAwait(false);
try
{
if (run.State is ImportRunState.Analyzing or ImportRunState.AnalysisInterrupted)
{
StartJob(runId, ImportPhase.Analyzing, (progress, token) => Analyzer.AnalyzeAsync(runId, progress, token), ref lease);
}
else
{
StartJob(runId, ImportPhase.Copying, (progress, token) => Copier.CopyAsync(runId, progress, token), ref lease);
}
return run;
}
finally
{
lease?.Dispose();
}
}
/// <summary>
@@ -146,16 +165,24 @@ public sealed class ImportService : IAsyncDisposable
public async Task<ImportRun> StartCopyAsync(long runId, ImportMode? mode = null, CancellationToken ct = default)
{
EnsureIdle();
var run = mode is { } m
? await Review.SetModeAsync(runId, m, ct).ConfigureAwait(false)
: await _env.Storage.Imports.GetRunAsync(runId, ct).ConfigureAwait(false) ?? throw new KeyNotFoundException($"Importazione {runId} non trovata.");
if (run.State is not (ImportRunState.Reviewing or ImportRunState.CopyInterrupted or ImportRunState.Completed))
var lease = await EnterGateAsync(ct).ConfigureAwait(false);
try
{
throw new InvalidOperationException("L'analisi non è finita: riprendila prima di importare.");
}
var run = mode is { } m
? await Review.SetModeAsync(runId, m, ct).ConfigureAwait(false)
: await _env.Storage.Imports.GetRunAsync(runId, ct).ConfigureAwait(false) ?? throw new KeyNotFoundException($"Importazione {runId} non trovata.");
if (run.State is not (ImportRunState.Reviewing or ImportRunState.CopyInterrupted or ImportRunState.Completed))
{
throw new InvalidOperationException("L'analisi non è finita: riprendila prima di importare.");
}
StartJob(runId, ImportPhase.Copying, (progress, token) => Copier.CopyAsync(runId, progress, token));
return run;
StartJob(runId, ImportPhase.Copying, (progress, token) => Copier.CopyAsync(runId, progress, token), ref lease);
return run;
}
finally
{
lease?.Dispose();
}
}
/// <summary>«Interrompi»: stops the running job of this import and waits until its state is saved.</summary>
@@ -278,6 +305,10 @@ public sealed class ImportService : IAsyncDisposable
}
}
/// <summary>
/// The quick refusals, before anything is read or recorded: an import job runs, or a long work that does not give way
/// holds the gate (an automatic copy does give way: <see cref="EnterGateAsync"/> displaces it).
/// </summary>
private void EnsureIdle()
{
if (ActiveRunId is not null)
@@ -285,14 +316,32 @@ public sealed class ImportService : IAsyncDisposable
throw new InvalidOperationException("C'è già un'importazione al lavoro: aspetta che finisca o interrompila.");
}
// Checked up front too, so a refused start records nothing; StartJob takes the gate for real.
if (_env.Gate?.Busy is { } busy)
if (_env.Gate?.BusyFirm is { } busy)
{
throw new InvalidOperationException(LongWorkGate.BusyMessage(LongWorkKind.Import, busy));
}
}
private void StartJob(long runId, ImportPhase phase, Func<IProgress<ImportProgress>, CancellationToken, Task> work)
/// <summary>
/// Takes the long-work gate before anything is recorded and outside <see cref="_gate"/> (tappa E5, D6): a yielding
/// automatic copy is stopped and awaited (at most <see cref="LongWorkGate.YieldWait"/>, real time), and meanwhile a
/// second start is refused at once. Null without a gate (tests).
/// </summary>
private async Task<IDisposable?> EnterGateAsync(CancellationToken ct)
{
if (_env.Gate is not { } gate)
{
return null;
}
var entry = await gate.EnterAsync(LongWorkKind.Import, ct).ConfigureAwait(false);
return entry.BusyWith is { } busy
? throw new InvalidOperationException(LongWorkGate.BusyMessage(LongWorkKind.Import, busy))
: entry.Lease;
}
/// <summary>Starts the job; on success it owns <paramref name="held"/> (set to null here) and releases it when it ends.</summary>
private void StartJob(long runId, ImportPhase phase, Func<IProgress<ImportProgress>, CancellationToken, Task> work, ref IDisposable? held)
{
lock (_gate)
{
@@ -301,12 +350,8 @@ public sealed class ImportService : IAsyncDisposable
throw new InvalidOperationException("C'è già un'importazione al lavoro: aspetta che finisca o interrompila.");
}
IDisposable? lease = null;
if (_env.Gate is { } gate && !gate.TryEnter(LongWorkKind.Import, out lease, out var busy))
{
throw new InvalidOperationException(LongWorkGate.BusyMessage(LongWorkKind.Import, busy!.Value));
}
var lease = held;
held = null;
_job?.Cancellation.Dispose();
var cancellation = new CancellationTokenSource();
var reporter = new Reporter(this);