Files
Febe/src/Febe.Core/Abstractions/LongWorkGate.cs
T
Alby96andClaude Opus 5.5 a112c92893 E2: motore delle copie esterne (porta, destinazioni, piano, copia)
Prima metà della tappa E2 «Copie esterne» (#58), parte Core e Data:

- porta del lavoro lungo: importazione, copia e ricostruzione mai insieme;
- radice /esporta in Docker con mountinfo, controllo delle cartelle con
  il file di prova, destinazioni con «Esporta questo account» acceso se
  Alberto non ha mai scelto (decisione del 27/11/2026);
- filtro «Copie», segni delle tessere e conteggi dell'account sui soli
  file dovuti (D3);
- percorsi «Come la libreria», segmento dell'account nella cartella
  generale, 240 caratteri, storia in evidenza;
- piano di «Copia in…», un lavoro per destinazione, copia in .part con
  SHA-256 riletto dal disco, nome libero, mai sovrascrivere, adozione
  per percorso, pausa della destinazione su prova, spazio e FAT32,
  attesa fuori dalle sessioni del motore, ripresa e recupero;
- .part rimossi solo nelle cartelle degli elementi di lavori non finiti;
- «Tolto» e «Superato» (agganci in importazione, scaricamenti,
  doppioni e ricostruzione), mai ricopiati da soli;
- rapporti e testi in italiano, prove del lavoro di copia.

Interfaccia, API e composizione restano alla parte successiva.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 10:55:29 +01:00

107 lines
3.5 KiB
C#

using System.Diagnostics.CodeAnalysis;
namespace Febe.Core.Abstractions;
/// <summary>The long pieces of work that never run together (spec §4.10: «esportazione, importazione e ricostruzione non girano insieme»).</summary>
public enum LongWorkKind
{
/// <summary>An import job: analysis or copy (spec §9).</summary>
Import,
/// <summary>A job of copies to external folders (spec §4.10).</summary>
Export,
/// <summary>«Ricostruisci il database dalla libreria» (spec §8.5).</summary>
Rebuild,
}
/// <summary>
/// Lets only one of import, export and rebuild run at a time, in both directions. One instance is shared by the whole
/// process (the composition registers it once). A lease is taken when a job starts and disposed when it ends; disposing
/// it twice releases nothing more.
/// </summary>
public sealed class LongWorkGate
{
private readonly Lock _lock = new();
private Lease? _holder;
/// <summary>Who holds the gate, or null when it is free.</summary>
public LongWorkKind? Busy
{
get
{
lock (_lock)
{
return _holder?.Kind;
}
}
}
/// <summary>
/// Takes the gate for <paramref name="kind"/>. False, with <paramref name="busyWith"/> set, when another job (of any
/// kind, the same one included) holds it.
/// </summary>
public bool TryEnter(LongWorkKind kind, [NotNullWhen(true)] out IDisposable? lease, out LongWorkKind? busyWith)
{
lock (_lock)
{
if (_holder is { } holder)
{
lease = null;
busyWith = holder.Kind;
return false;
}
var taken = new Lease(this, kind);
_holder = taken;
lease = taken;
busyWith = null;
return true;
}
}
/// <summary>The Italian answer when <paramref name="wanted"/> cannot start because <paramref name="busy"/> is running.</summary>
public static string BusyMessage(LongWorkKind wanted, LongWorkKind busy) => (wanted, busy) switch
{
(LongWorkKind.Import, LongWorkKind.Import) => "C'è già un'importazione al lavoro: aspetta che finisca o interrompila.",
(LongWorkKind.Export, LongWorkKind.Export) => "C'è già una copia al lavoro: aspetta che finisca o interrompila.",
(LongWorkKind.Rebuild, LongWorkKind.Rebuild) => "La ricostruzione è già in corso.",
(_, LongWorkKind.Import) => $"C'è un'importazione al lavoro: aspetta che finisca o interrompila, poi {Verb(wanted)}.",
(_, LongWorkKind.Export) => $"C'è una copia nelle cartelle esterne al lavoro: aspetta che finisca o interrompila, poi {Verb(wanted)}.",
_ => "La ricostruzione del database è in corso: aspetta che finisca.",
};
private static string Verb(LongWorkKind kind) => kind switch
{
LongWorkKind.Import => "importa",
LongWorkKind.Export => "copia",
_ => "ricostruisci",
};
private void Release(Lease lease)
{
lock (_lock)
{
if (ReferenceEquals(_holder, lease))
{
_holder = null;
}
}
}
private sealed class Lease(LongWorkGate gate, LongWorkKind kind) : IDisposable
{
private int _disposed;
public LongWorkKind Kind { get; } = kind;
public void Dispose()
{
if (Interlocked.Exchange(ref _disposed, 1) == 0)
{
gate.Release(this);
}
}
}
}