Files
Chronos/src/Chronos.Server/Api/ReorganizePlanCache.cs
T
Alby96 9ae773aa5a fix(server): keep Reorganize dry runs for the confirm, test the protocol's key paths
An all:true dry run reads every sidecar and photo folder of the archive inside
the request, and the confirm planned the whole archive again before answering
202. The server now keeps each dry run's plan for five minutes (eight plans at
most) by archive, selection, templates and a stamp of the archive's index rows:
the same dry run again and its confirmation take the kept plan, and any change
the index records (an upload, an edit, a move, a scan, a verify) gives another
stamp and a new plan. A change made on disk alone is not in the stamp; the move
job still plans again and refuses a plan that no longer matches.

Server tests now cover an all:true apply around a sequence already in place, a
real plan_changed after an upload, a move job paused between two sequences and
resumed, archive_offline, job_unavailable, more than 1,000 ids and the kept
plans themselves; a Data test covers the index stamp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 71bb53fbf8eb7d0327ea692d046634b12f8d37e9)
2026-10-27 16:30:00 +01:00

70 lines
3.2 KiB
C#

using Chronos.Core.Archiving;
using Chronos.Data.Repositories;
namespace Chronos.Server.Api;
/// <summary>
/// Reorganize's dry runs kept a short while (owner review 2, R7): the same dry run asked again, and the confirmation that
/// follows it, take the plan made then instead of reading every sidecar and photo folder of the selection again (with
/// <c>all</c>, of the whole archive), while the archive's index is as it was then
/// (<see cref="SequenceRepository.GetStampAsync"/>) and for at most <see cref="Lifetime"/>. A change made on disk alone,
/// which the index does not have, is not seen by a kept plan: the <c>move</c> job plans again and refuses a plan that no
/// longer matches the archive, so nothing changes that the dry run did not show.
/// </summary>
public sealed class ReorganizePlanCache(TimeProvider time)
{
/// <summary>How long a plan is kept, from when its planning started.</summary>
public static readonly TimeSpan Lifetime = TimeSpan.FromMinutes(5);
/// <summary>Plans kept at most (a plan of every sequence holds one step per sequence); the oldest goes first.</summary>
public const int Capacity = 8;
private readonly Lock _gate = new();
private readonly Dictionary<Key, (RenamePlan Plan, DateTimeOffset Started)> _plans = [];
/// <summary>
/// What a plan is kept by: the archive, the stamp of its index, the sequences (all of them, or the ids in the order
/// given, which decides the folder that gets " (2)") and the templates.
/// </summary>
public readonly record struct Key(Guid ArchiveId, string Stamp, string Sequences, RenameTemplates Templates)
{
/// <param name="sequenceIds">The sequences of the request; null for every sequence of the archive.</param>
public static Key Of(Guid archiveId, string stamp, IReadOnlyList<Guid>? sequenceIds, RenameTemplates templates) =>
new(archiveId, stamp, sequenceIds is null ? "*" : string.Join(',', sequenceIds.Select(id => id.ToString("N"))), templates);
}
/// <summary>
/// The plan kept for <paramref name="key"/>, or the one <paramref name="plan"/> makes now, then kept; a refusal
/// (<paramref name="plan"/> throws) is not kept.
/// </summary>
public async Task<RenamePlan> GetOrPlanAsync(Key key, Func<Task<RenamePlan>> plan)
{
ArgumentNullException.ThrowIfNull(plan);
DateTimeOffset started;
lock (_gate)
{
started = time.GetUtcNow();
Expire(started);
if (_plans.TryGetValue(key, out var kept)) return kept.Plan;
}
var made = await plan().ConfigureAwait(false);
lock (_gate)
{
if (!_plans.ContainsKey(key) && _plans.Count >= Capacity) _plans.Remove(_plans.MinBy(p => p.Value.Started).Key);
_plans[key] = (made, started);
}
return made;
}
/// <summary>Forgets the plan kept for <paramref name="key"/>: its job is queued, and what the job changes gives the index another stamp.</summary>
public void Forget(Key key)
{
lock (_gate) _plans.Remove(key);
}
private void Expire(DateTimeOffset now)
{
foreach (var key in _plans.Where(p => now - p.Value.Started >= Lifetime).Select(p => p.Key).ToList()) _plans.Remove(key);
}
}