E3: conversione della libreria in sottofondo con righe sempre vere
La libreria può ora tenere accanto a ogni originale una copia nel formato scelto, preparata in sottofondo senza mai sostituire o cancellare l'originale. - LongWorkGate ha un permesso di sottofondo che il lavoro in primo piano (importazione, copie, ricostruzione) interrompe; il salvataggio in corso si aspetta al più CommitWait. - La coda e il lavoratore: nuovi file accodati all'arrivo, quelli già in libreria solo su richiesta, pausa, nuovo tentativo, attesa durante una sessione del motore o con poco spazio, nomi liberi e adozione di un file identico già presente. - Le righe di media_conversion restano vere in ogni via: eliminazione (Ricorda e Consenti il ritorno, con RemoveWithConversionsAsync per il vincolo RESTRICT), perdente del ripasso dei doppioni, file cambiato (superate e riaccodate), ricostruzione (conta e riconosce i convertiti, accoda solo Pending), importazione (una copia convertita o un fotogramma non rientra come file nuovo). - FebeApp avvia il lavoratore con InBackground; la demo mai. _febe.jsonl non cambia. La prova RecordsCannotBeDeletedFromUnderTheirCopies usa ora una riga Converted: RemoveAsync toglie da sé le righe senza file, e il vincolo si verifica sia in SQL sia attraverso il magazzino. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b746aff45d
commit
724c5bda6f
25 files changed
+2621
-38
No files matched your search
@@ -0,0 +1,148 @@
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Domain;
|
||||
using Febe.Core.Export;
|
||||
using Febe.Core.Storage;
|
||||
|
||||
namespace Febe.Core.Library.Conversion;
|
||||
|
||||
/// <summary>What «Anche i N già in libreria» would queue: files and the bytes of their originals («circa», D17).</summary>
|
||||
public sealed record LibraryConversionPreview(int Files, long Bytes);
|
||||
|
||||
/// <summary>
|
||||
/// The queue of the conversion for the library (tappa E3, D15, WP2.2): what enters it and when. A new file enters after
|
||||
/// its line of <c>_febe.jsonl</c> (<see cref="LibraryIngest.AppendJournalAsync"/>); the files already in the library
|
||||
/// only when Alberto answers «Anche i N già in libreria» to the question asked when the conversion is turned on (Q29);
|
||||
/// a format turned off or changed empties its queue, the converted files stay where they are.
|
||||
/// </summary>
|
||||
public sealed class LibraryConversionQueue(IFebeStorage storage, IClock clock)
|
||||
{
|
||||
/// <summary>«In pausa» (<c>1</c>): the worker converts nothing until it is resumed.</summary>
|
||||
public const string PausedKey = "esportazione.libreria-sospesa";
|
||||
|
||||
private static readonly Lock SignalGate = new();
|
||||
private static TaskCompletionSource _signal = NewSignal();
|
||||
|
||||
/// <summary>The files «Anche i N già in libreria» would queue for the library formats of <paramref name="settings"/>.</summary>
|
||||
public async Task<LibraryConversionPreview> PreviewExistingAsync(ExportSettings settings, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings);
|
||||
var (files, bytes) = await storage.Conversions.CountExistingAsync(
|
||||
LibraryConversionRules.FormatFor(MediaKind.Photo, settings), LibraryConversionRules.FormatFor(MediaKind.Video, settings), ct).ConfigureAwait(false);
|
||||
return new LibraryConversionPreview(files, bytes);
|
||||
}
|
||||
|
||||
/// <summary>«Anche i N già in libreria»: queues the present files that convert, in one statement per kind. Returns how many.</summary>
|
||||
public async Task<int> EnqueueExistingAsync(ExportSettings settings, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings);
|
||||
var added = await storage.Conversions.EnqueueExistingAsync(
|
||||
LibraryConversionRules.FormatFor(MediaKind.Photo, settings), LibraryConversionRules.FormatFor(MediaKind.Video, settings), clock.Now, ct).ConfigureAwait(false);
|
||||
if (added > 0)
|
||||
{
|
||||
Signal();
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The settings changed: a library format turned off or changed loses its queue (its converted files and rows stay).
|
||||
/// Turning one on queues nothing here: the interface asks, then calls <see cref="EnqueueExistingAsync"/> only for
|
||||
/// «Anche i N già in libreria».
|
||||
/// </summary>
|
||||
public async Task OnSettingsChangedAsync(ExportSettings before, ExportSettings after, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(before);
|
||||
ArgumentNullException.ThrowIfNull(after);
|
||||
foreach (var kind in new[] { MediaKind.Photo, MediaKind.Video })
|
||||
{
|
||||
var old = LibraryConversionRules.FormatFor(kind, before);
|
||||
if (old is { } format && old != LibraryConversionRules.FormatFor(kind, after))
|
||||
{
|
||||
await storage.Conversions.DeletePendingAsync(format, ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>«Riprova»: the failed files go back to the queue, except those that never convert (<c>AsIs*</c>). Returns how many.</summary>
|
||||
public async Task<int> RetryFailedAsync(CancellationToken ct = default)
|
||||
{
|
||||
var reset = await storage.Conversions.ResetFailedAsync(clock.Now, ct).ConfigureAwait(false);
|
||||
if (reset > 0)
|
||||
{
|
||||
Signal();
|
||||
}
|
||||
|
||||
return reset;
|
||||
}
|
||||
|
||||
/// <summary>«In pausa» or not.</summary>
|
||||
public static async Task<bool> IsPausedAsync(ISettingsStore settings, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings);
|
||||
return await settings.GetAsync(PausedKey, ct).ConfigureAwait(false) == "1";
|
||||
}
|
||||
|
||||
/// <summary>«Sospendi» / «Riprendi» the conversion for the library.</summary>
|
||||
public async Task SetPausedAsync(bool paused, CancellationToken ct = default)
|
||||
{
|
||||
await storage.Settings.SetAsync(PausedKey, paused ? "1" : "0", ct).ConfigureAwait(false);
|
||||
if (!paused)
|
||||
{
|
||||
Signal();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues the present files of an entry by the library formats set now, after its journal line (the hook of
|
||||
/// <see cref="LibraryIngest"/>). Nothing when both formats are «Originale». Returns how many rows were added.
|
||||
/// </summary>
|
||||
public static async Task<int> EnqueueEntryAsync(IFebeStorage storage, IReadOnlyList<Media> media, DateTimeOffset at, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(storage);
|
||||
ArgumentNullException.ThrowIfNull(media);
|
||||
var settings = await ExportSettings.LoadAsync(storage.Settings, ct).ConfigureAwait(false);
|
||||
if (!settings.ConvertsLibrary)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var requests = LibraryConversionRules.RequestsFor(media, settings);
|
||||
if (requests.Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var added = await storage.Conversions.EnqueueAsync(requests, at, ct).ConfigureAwait(false);
|
||||
if (added > 0)
|
||||
{
|
||||
Signal();
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
/// <summary>Wakes the worker: something entered the queue (or the pause ended).</summary>
|
||||
public static void Signal()
|
||||
{
|
||||
TaskCompletionSource fired;
|
||||
lock (SignalGate)
|
||||
{
|
||||
fired = _signal;
|
||||
_signal = NewSignal();
|
||||
}
|
||||
|
||||
fired.TrySetResult();
|
||||
}
|
||||
|
||||
/// <summary>A task that completes at the next <see cref="Signal"/>.</summary>
|
||||
internal static Task NextSignal()
|
||||
{
|
||||
lock (SignalGate)
|
||||
{
|
||||
return _signal.Task;
|
||||
}
|
||||
}
|
||||
|
||||
private static TaskCompletionSource NewSignal() => new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
}
|
||||
Reference in new issue
Block a user