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 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-10-27 19:50:32 +01:00
1 parent 074d09839a
commit 0c2bdedfdf
5 files changed
+59 -8

No files matched your search

+32 -4
View File
@@ -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<Guid> Complete
AtomicFile.WriteAllBytes(PathFor(archive, journal.Plan.Id), JsonSerializer.SerializeToUtf8Bytes(journal, ArchiveJsonIndented.Default.RenameJournal));
}
/// <summary>Journals left by interrupted renames, held ones included.</summary>
/// <summary>
/// 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 (<see cref="FileRenameJournal"/>).
/// </summary>
public static IReadOnlyList<RenameJournal> ListPending(ArchivePaths archive)
{
if (!Directory.Exists(archive.JournalDirectory)) return [];
var list = new List<RenameJournal>();
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;
}
/// <summary>The journal of the plan <paramref name="planId"/>; null when there is none (the plan completed, or never started).</summary>
/// <summary>
/// The journal of the plan <paramref name="planId"/>; null when there is none (the plan completed, or never started).
/// Throws <see cref="InvalidDataException"/> 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.
/// </summary>
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));
}
@@ -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);
}