From 0c2bdedfdf1d0cdeffb3dd416acc59afaf097832 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 27 Oct 2026 19:50:32 +0100 Subject: [PATCH] fix(reorganize): a rename journal that cannot be read no longer stops the archive from opening An archive open lists the pending rename plans; one journal that was not valid JSON threw out of ArchiveService.OpenAsync, so the archive (and the server, at boot) did not open until the file was removed by hand. It is now logged and left in place; its own job fails with the reason instead of planning again over a half-applied plan. Co-Authored-By: Claude Opus 5.5 --- docs/BRIEF.md | 4 ++- docs/CHANGELOG.md | 5 +-- src/Chronos.Core/Archiving/MassRename.cs | 36 ++++++++++++++++--- .../Archiving/Jobs/MoveJobHandler.cs | 2 +- .../Archiving/ReorganizeTests.cs | 20 +++++++++++ 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/docs/BRIEF.md b/docs/BRIEF.md index c7dfd9a2..ccb383c1 100644 --- a/docs/BRIEF.md +++ b/docs/BRIEF.md @@ -484,7 +484,9 @@ step being one sequence and the sequences nested in a folder that moves with it: - `journal/rename-.json` holds the plan (its steps, templates and the sequences it was made for), the steps completed and what they did; it is written before the first step and deleted after the last. A plan whose job was paused, cancelled or failed is marked held and - waits for that job; any other is completed at the next archive open. + waits for that job; any other is completed at the next archive open. A journal that cannot be + read is logged and left where it is: the archive opens, the plan is not resumed and its job + fails with the reason. - Each step takes the leases of its sequences, then their thumbnail leases, and checks under them, before anything of it moves, that the plan still fits: the sequences where the index had them, nothing indexed since inside a folder that moves or at its new place, no sidecar of diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 1a37d0e1..f555ebd3 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -46,8 +46,9 @@ commits follow Conventional Commits (release notes are generated from them by `C Reorganize comes back under its old name; a Reorganize job cancelled and never retried leaves its held plan in `.chronos/journal` (only the file renames a step left half-way are completed at an archive open); deleting sequences is refused while a Reorganize job runs or is paused; a render of another program whose folder moves while - it runs is not stopped, and its output can land in a folder created again at the old place; a plan journal damaged - by hand (not valid JSON) stops the archive from opening until it is removed. + it runs is not stopped, and its output can land in a folder created again at the old place; a plan journal that + cannot be read is logged and left in `.chronos/journal`: the archive opens, its plan is not resumed and its job + fails with the reason. ### Changed - Import, upload and Reorganize name photos by one rule: every photo of a sequence is named with the sequence's camera diff --git a/src/Chronos.Core/Archiving/MassRename.cs b/src/Chronos.Core/Archiving/MassRename.cs index 0d85ea6c..294c61fc 100644 --- a/src/Chronos.Core/Archiving/MassRename.cs +++ b/src/Chronos.Core/Archiving/MassRename.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Security.Cryptography; using System.Text; using System.Text.Json; +using Chronos.Core.Diagnostics; using Chronos.Core.IO; using Chronos.Core.Localization; using Chronos.Core.Naming; @@ -214,25 +215,52 @@ public sealed record RenameJournal(RenamePlan Plan, IReadOnlyList Complete AtomicFile.WriteAllBytes(PathFor(archive, journal.Plan.Id), JsonSerializer.SerializeToUtf8Bytes(journal, ArchiveJsonIndented.Default.RenameJournal)); } - /// Journals left by interrupted renames, held ones included. + /// + /// Journals left by interrupted renames, held ones included. A journal that cannot be read (damaged by hand, or by a + /// disk) is logged and left where it is, so the archive still opens; its plan is not resumed, and the file renames of + /// its steps are completed by their own journals (). + /// public static IReadOnlyList ListPending(ArchivePaths archive) { if (!Directory.Exists(archive.JournalDirectory)) return []; var list = new List(); foreach (var path in Directory.EnumerateFiles(archive.JournalDirectory, "rename-*.json").Order(StringComparer.Ordinal)) { - if (JsonSerializer.Deserialize(File.ReadAllBytes(path), ArchiveJson.Default.RenameJournal) is { } journal) list.Add(journal); + try + { + if (Parse(File.ReadAllBytes(path)) is { } journal) list.Add(journal); + else Log.Warning("archive", $"The rename journal {Path.GetFileName(path)} is empty or incomplete; its plan is not resumed"); + } + catch (JsonException ex) + { + Log.Warning("archive", $"The rename journal {Path.GetFileName(path)} cannot be read; its plan is not resumed", ex); + } } return list; } - /// The journal of the plan ; null when there is none (the plan completed, or never started). + /// + /// The journal of the plan ; null when there is none (the plan completed, or never started). + /// Throws when the journal is there but cannot be read, so its job fails with the + /// reason instead of planning again over a half-applied plan. + /// public static RenameJournal? Read(ArchivePaths archive, Guid planId) { var path = PathFor(archive, planId); - return File.Exists(path) ? JsonSerializer.Deserialize(File.ReadAllBytes(path), ArchiveJson.Default.RenameJournal) : null; + if (!File.Exists(path)) return null; + try + { + return Parse(File.ReadAllBytes(path)) ?? throw new InvalidDataException($"The rename journal {Path.GetFileName(path)} is incomplete."); + } + catch (JsonException ex) + { + throw new InvalidDataException($"The rename journal {Path.GetFileName(path)} cannot be read.", ex); + } } + private static RenameJournal? Parse(byte[] bytes) => + JsonSerializer.Deserialize(bytes, ArchiveJson.Default.RenameJournal) is { Plan: not null, Completed: not null } journal ? journal : null; + public static void Delete(ArchivePaths archive, Guid planId) => File.Delete(PathFor(archive, planId)); } diff --git a/src/Chronos.Data/Archiving/Jobs/MoveJobHandler.cs b/src/Chronos.Data/Archiving/Jobs/MoveJobHandler.cs index 3b56218d..4c38ce38 100644 --- a/src/Chronos.Data/Archiving/Jobs/MoveJobHandler.cs +++ b/src/Chronos.Data/Archiving/Jobs/MoveJobHandler.cs @@ -155,7 +155,7 @@ internal sealed class MoveJobHandler(ChronosDatabase db, MassRenamer renamer, Fu Log.Info("archive", $"Reorganize {planId} held by its failed job {context.Job.Id}"); LogDone(context, journal.Plan, RenameResult.Of(journal.Done)); } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException) + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException or InvalidDataException) { Log.Warning("archive", $"Reorganize {planId} could not be held after its job failed", ex); } diff --git a/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs b/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs index f89a1a06..ab9c1676 100644 --- a/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs +++ b/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs @@ -751,4 +751,24 @@ public sealed class ReorganizeTests(TestContext context) Assert.Equal("originals/A_2.CR2", Assert.NotNull(FileRenameJournal.Recover(sequence, manifest)).Frames.Single().File); Assert.Equal(("photo", "sidecar"), (Read(sequence, "originals/A_2.CR2"), Read(sequence, "originals/A_2.xmp"))); } + + [Test] + public void ADamagedPlanJournalIsSkippedAtOpenAndFailsItsOwnJob() + { + var archive = new ArchivePaths(context.CreateTempDirectory("archive")); + var good = new RenameJournal(new RenamePlan(Guid.NewGuid(), Guid.NewGuid(), []), []); + RenameJournal.Write(archive, good); + var damaged = Guid.NewGuid(); + File.WriteAllText(RenameJournal.PathFor(archive, damaged), "{ \"plan\": "); + var empty = Guid.NewGuid(); + File.WriteAllText(RenameJournal.PathFor(archive, empty), "{}"); + + // The archive still opens: the readable journal resumes, the others stay where they are. + Assert.Equal(good.Plan.Id, RenameJournal.ListPending(archive).Single().Plan.Id); + Assert.True(File.Exists(RenameJournal.PathFor(archive, damaged))); + // The job of a damaged plan fails with the reason instead of planning again. + Assert.Throws(() => RenameJournal.Read(archive, damaged)); + Assert.Throws(() => RenameJournal.Read(archive, empty)); + Assert.Null(RenameJournal.Read(archive, Guid.NewGuid())); + } }