fix: logging never fails a job, one backup retention, first copy after the upgrade

Owner review 2, phase 2 review findings (scheduler core).

- Log.Write caught only IOException. Microsoft.Extensions.Logging wraps
  provider failures in an AggregateException, so a log line that could not
  be written (in tests: the process-wide sink of a stopped server whose log
  folder was gone) escaped JobEngine.RunAsync's catch before the failure was
  stored: the job stayed running and the worker faulted. This was the cause
  of the intermittent JobApiTests/ArchiveModuleTests failures. Log.Write now
  drops any non-fatal sink failure, TeeLogSink keeps a line from the other
  sinks, FileLogSink drops a line it cannot write and reopens the file at the
  next one, and LoggerSink drops what the providers throw. The scheduler's
  loop relies on the same guarantee.
- The daily backup loop (RunDailyAsync, IsDue, Interval, CheckInterval) had
  no caller since the index-backup schedule replaced it; it is removed and
  the DatabaseBackups summary describes the schedule.
- The first copy after the upgrade no longer waits a day (or for 03:00):
  SystemSchedule.LastDone seeds a new row due at once when its work was last
  done a period or more before, and both hosts pass the newest copy in the
  backups folder for index-backup.
- Back up now (Settings and Archive), the copy kept before a restore and a
  backup job that keeps fewer prune to the largest Copies kept of the backup
  schedules (DatabaseBackups.KeptAsync), not a fixed three. The four
  tooltips that promised three daily copies are reworded (en, it) and their
  stale translations deleted.
- New tests for the scheduler loop itself: a tick, Wake, a pass that threw,
  and dispose during a pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c5ad0c4c3f636129afe5dd1ce039185516da5621)
This commit is contained in:
Alby96 committed 2026-10-27 09:56:47 +01:00
1 parent 422f33d5f3
commit e384564784
59 files changed
+668 -264

No files matched your search

@@ -156,6 +156,31 @@ public sealed class FileLogSinkTests(TestContext context)
Assert.False(text.Contains("after", StringComparison.Ordinal), text);
}
/// <summary>
/// A folder removed under a sink (a server's data folder deleted, /data/logs gone) drops lines instead of throwing into
/// the code that logs; once the folder is back, the next line opens the file again.
/// </summary>
[Test]
public void ARemovedFolderDropsLinesUntilItIsBack()
{
var dir = Path.Combine(context.CreateTempDirectory(), "logs");
var time = new ManualTime(new DateTimeOffset(2026, 10, 27, 9, 0, 0, TimeSpan.Zero));
using var sink = new FileLogSink(dir, "server", new LogFileOptions { RetentionDays = 0 }, time);
Directory.Delete(dir);
// Before the first line, as a server whose temporary folder a test already deleted.
sink.Write(LogLevel.Error, "jobs", "lost", new InvalidOperationException("cause"));
Assert.False(Directory.Exists(dir), "the sink does not create the folder again by itself");
Directory.CreateDirectory(dir);
sink.Write(LogLevel.Error, "jobs", "kept", null);
sink.Write(LogLevel.Info, "jobs", "next", null);
var path = Path.Combine(dir, "server-20261027.log");
Assert.Equal(path, sink.CurrentPath);
var text = Read(path);
Assert.True(text.Contains("kept", StringComparison.Ordinal) && text.Contains("next", StringComparison.Ordinal) && !text.Contains("lost", StringComparison.Ordinal), text);
}
/// <summary>Reads a file the sink may still be writing (it shares it for reading and writing, as the log tail does).</summary>
private static string Read(string path)
{
@@ -0,0 +1,70 @@
using Chronos.Core.Diagnostics;
using Chronos.Testing;
namespace Chronos.Core.Tests.Diagnostics;
/// <summary>
/// Logging never throws into the code that logs: the job engine and the scheduler log from their error handling, and a line
/// that escaped there once left a failed job running and could stop a worker. Serial: these tests replace the process-wide sink.
/// </summary>
[Serial]
public sealed class LogTests
{
/// <summary>Throws what Microsoft.Extensions.Logging throws when a file provider's folder is gone.</summary>
private sealed class BrokenSink : ILogSink
{
public int Calls;
public void Write(LogLevel level, string category, string message, Exception? exception)
{
Interlocked.Increment(ref Calls);
throw new AggregateException("An error occurred while writing to logger(s).", new DirectoryNotFoundException("Could not find a part of the path"));
}
}
private sealed class ListSink : ILogSink
{
public List<string> Lines { get; } = [];
public void Write(LogLevel level, string category, string message, Exception? exception)
{
lock (Lines) Lines.Add($"{level} {category}: {message}");
}
}
[Test]
public void ALineASinkCannotWriteIsDroppedWithoutThrowing()
{
var sink = new BrokenSink();
var previous = Log.Sink;
var level = Log.MinimumLevel;
Log.SetSink(sink);
Log.MinimumLevel = LogLevel.Debug;
try
{
Log.Debug("test", "debug");
Log.Info("test", "info");
Log.Warning("test", "warning", new InvalidOperationException("cause"));
Log.Error("test", "error", new InvalidOperationException("cause"));
Log.Write(LogLevel.Error, "test", "write", null);
}
finally
{
Log.SetSink(previous);
Log.MinimumLevel = level;
}
Assert.Equal(5, sink.Calls);
}
[Test]
public void OneSinkOfATeeThatCannotWriteDoesNotKeepTheLineFromTheOthers()
{
var broken = new BrokenSink();
var kept = new ListSink();
var tee = new TeeLogSink(broken, kept);
tee.Write(LogLevel.Warning, "test", "first", null);
tee.Write(LogLevel.Error, "test", "second", new IOException("disk"));
Assert.Equal(2, broken.Calls);
Assert.SequenceEqual(["Warning test: first", "Error test: second"], kept.Lines);
}
}