A sequence created through POST /api/v1/sequences keeps an empty folder until its first frame. Scans no longer adopt loose photos at the archive root under its id, and a rebuild, the orphans report and fix, verify and the scan follow-ups no longer treat it as the root sequence, as missing or as a sequence to analyse. All of them use ArchiveIngest.AwaitsFolder. On the server (ArchiveServiceOptions.ImagesAfterScan), a scan that indexes sequences without thumbnails or proxies queues background Thumbs and Proxies passes for them, as an upload does. A scan that finds photos added or removed clears ThumbsBuilt and ProxiesBuilt together with AnalyzedLight. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
100 lines
4.2 KiB
C#
100 lines
4.2 KiB
C#
using System.Collections.Concurrent;
|
|
using Titano.Core.Archiving;
|
|
using Titano.Core.Imaging;
|
|
|
|
namespace Titano.Data.Archiving;
|
|
|
|
/// <summary>Settings shared by the archive services.</summary>
|
|
public sealed record ArchiveServiceOptions
|
|
{
|
|
/// <summary>Decoders for thumbnails and proxies beyond embedded previews and JPEG (WIC on Windows).</summary>
|
|
public ImageDecoder Decoder { get; init; } = new([]);
|
|
|
|
/// <summary>Name written into lease files so a waiting writer can say who holds a sequence.</summary>
|
|
public string Owner { get; init; } = "titano@" + Environment.MachineName;
|
|
|
|
/// <summary>How long a writer waits for a sequence held by another process before giving up.</summary>
|
|
public TimeSpan LeaseWait { get; init; } = TimeSpan.FromMinutes(2);
|
|
|
|
public TimeSpan LeaseDuration { get; init; } = LeaseFile.DefaultDuration;
|
|
|
|
/// <summary>
|
|
/// Scans queue thumbnails and proxies of the sequences they index without them, as an upload does (the server, whose
|
|
/// clients load both from it: BRIEF §4.3, §14.4). The desktop decodes its thumbnails on demand and makes proxies when
|
|
/// asked.
|
|
/// </summary>
|
|
public bool ImagesAfterScan { get; init; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read-modify-write of sidecars, serialised per sequence: an in-process gate for this process's writers plus a lease
|
|
/// file for writers in other processes on the same share (server and client). Every edit sees the latest sidecar,
|
|
/// with an interrupted ingest batch completed first.
|
|
/// </summary>
|
|
internal sealed class SequenceWriter(ArchiveServiceOptions options)
|
|
{
|
|
private readonly ConcurrentDictionary<Guid, SemaphoreSlim> _gates = new();
|
|
|
|
public ArchiveServiceOptions Options => options;
|
|
|
|
/// <summary>Holds the sequence exclusively until disposed.</summary>
|
|
public async Task<IAsyncDisposable> LockAsync(ArchivePaths archive, Guid sequenceId, CancellationToken cancellationToken)
|
|
{
|
|
var gate = _gates.GetOrAdd(sequenceId, _ => new SemaphoreSlim(1, 1));
|
|
await gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
var lease = await LeaseFile.AcquireAsync(archive.LocksDirectory, $"seq-{sequenceId:N}", options.Owner, options.LeaseWait,
|
|
options.LeaseDuration, cancellationToken).ConfigureAwait(false);
|
|
return new Held(gate, lease);
|
|
}
|
|
catch
|
|
{
|
|
gate.Release();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the sidecar of a locked sequence, completing an interrupted ingest batch first.
|
|
/// </summary>
|
|
/// <exception cref="InvalidDataException">The sidecar in the folder belongs to another sequence.</exception>
|
|
public static SequenceManifest ReadLocked(SequencePaths sequence, Guid sequenceId)
|
|
{
|
|
var manifest = ManifestStore.ReadSequence(sequence.ManifestPath);
|
|
if (manifest.Id != sequenceId)
|
|
throw new InvalidDataException($"{sequence.ManifestPath} belongs to sequence {manifest.Id}, not {sequenceId}.");
|
|
if (IngestJournal.Recover(sequence, manifest) is { } recovered)
|
|
{
|
|
ManifestStore.WriteSequence(sequence.ManifestPath, recovered);
|
|
manifest = recovered;
|
|
}
|
|
IngestJournal.Delete(sequence);
|
|
return manifest;
|
|
}
|
|
|
|
/// <summary>Applies <paramref name="edit"/> to the sidecar under the lock; a null result writes nothing.</summary>
|
|
public async Task<SequenceManifest> UpdateAsync(ArchivePaths archive, Guid sequenceId, string folder,
|
|
Func<SequenceManifest, SequenceManifest?> edit, CancellationToken cancellationToken)
|
|
{
|
|
var sequence = archive.Sequence(folder);
|
|
await using (await LockAsync(archive, sequenceId, cancellationToken).ConfigureAwait(false))
|
|
{
|
|
var manifest = ReadLocked(sequence, sequenceId);
|
|
if (edit(manifest) is not { } updated) return manifest;
|
|
ManifestStore.WriteSequence(sequence.ManifestPath, updated);
|
|
return updated;
|
|
}
|
|
}
|
|
|
|
private sealed class Held(SemaphoreSlim gate, LeaseFile lease) : IAsyncDisposable
|
|
{
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
lease.Dispose();
|
|
gate.Release();
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|
|
}
|