Files
Chronos/src/Chronos.Data/Housekeeping/HousekeepingJobHandlers.cs
T
Alby96 e384564784 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)
2026-10-27 09:56:47 +01:00

81 lines
4.4 KiB
C#

using Chronos.Core.Diagnostics;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Archiving.Jobs;
using Chronos.Data.Jobs;
using Chronos.Data.Sqlite;
namespace Chronos.Data.Housekeeping;
/// <summary>
/// <see cref="JobKind.Backup"/> (owner review 2, R10): a <c>VACUUM INTO</c> copy of the SQLite index into
/// <paramref name="directory"/> (by default the <c>backups</c> folder next to the index, <see cref="DatabaseBackups.DefaultDirectory"/>),
/// keeping the newest <see cref="BackupParameters.Keep"/>, or more when a backup schedule keeps more (one folder, one
/// retention: <see cref="DatabaseBackups.KeptAsync"/>). A PostgreSQL index, or one without a file, cannot be
/// copied this way (<see cref="DatabaseBackups.Unavailable"/>): the job fails with that reason, and schedules do not queue it.
/// </summary>
internal sealed class BackupJobHandler(ChronosDatabase db, string? directory) : IJobHandler
{
/// <summary>Most copies a schedule may keep.</summary>
public const int MaxKeep = 100;
public JobKind Kind => JobKind.Backup;
public async Task RunAsync(JobContext context)
{
if (db.Database is not SqliteDatabase { Path: { } path })
throw new NotSupportedException(DatabaseBackups.Unavailable(db) == Scheduling.ScheduleUnavailable.Postgres
? "Backups of a PostgreSQL database are made with the server's own tools."
: "An index without a file cannot be backed up.");
var own = Math.Clamp(ArchiveJobJson.Read(context.Job, ArchiveJobJson.Default.BackupParameters, new BackupParameters(DatabaseBackups.Keep)).Keep, 1, MaxKeep);
var keep = int.Max(own, await DatabaseBackups.ScheduledKeepAsync(db, context.Cancellation).ConfigureAwait(false));
await context.ReportAsync(0, 1).ConfigureAwait(false);
var copy = await db.BackupAsync(directory ?? DatabaseBackups.DefaultDirectory(path), keep, context.Cancellation).ConfigureAwait(false);
context.Log(Loc.L("Archive.Log.BackupDone", copy, CleanupSteps.Megabytes(new FileInfo(copy).Length), keep));
await context.ReportAsync(1, 1).ConfigureAwait(false);
}
}
/// <summary>
/// <see cref="JobKind.Cleanup"/> (owner review 2, R10): runs the named steps of <see cref="CleanupParameters"/> in order,
/// each with the job's age limit, and logs one line per step. A step that fails is logged and the others still run; the job
/// then fails naming it. Steps are quick and safe to repeat, so a resumed job runs them all again (no checkpoint). An
/// unknown name (a step of another host, or of a newer build) is logged and skipped.
/// </summary>
internal sealed class CleanupJobHandler(ChronosDatabase db, IReadOnlyList<ICleanupStep> steps, TimeProvider time) : IJobHandler
{
public JobKind Kind => JobKind.Cleanup;
public async Task RunAsync(JobContext context)
{
var parameters = ArchiveJobJson.Read(context.Job, ArchiveJobJson.Default.CleanupParameters, new CleanupParameters([], 0));
var archive = context.Job.TargetIds.Count > 0 ? await db.Archives.GetAsync(context.Job.TargetIds[0], context.Cancellation).ConfigureAwait(false) : null;
var cleanup = new CleanupContext(db, archive, context, time.GetUtcNow());
var names = parameters.Steps;
var failed = new List<string>();
for (var i = 0; i < names.Count; i++)
{
context.Cancellation.ThrowIfCancellationRequested();
await context.ReportAsync(i, names.Count).ConfigureAwait(false);
var step = steps.FirstOrDefault(s => string.Equals(s.Name, names[i], StringComparison.Ordinal));
if (step is null)
{
context.Log(Loc.L("Archive.Log.StepUnknown", names[i]));
continue;
}
try
{
context.Log(await step.RunAsync(cleanup, parameters).ConfigureAwait(false));
}
catch (Exception ex) when (!(ex is OperationCanceledException && context.Cancellation.IsCancellationRequested))
{
Log.Warning("jobs", $"Clean-up step {step.Name} of job {context.Job.Id} failed", ex);
context.Log(Loc.L("Archive.Log.StepFailed", step.Name, ex.Message));
failed.Add(step.Name);
}
}
await context.ReportAsync(names.Count, names.Count).ConfigureAwait(false);
if (failed.Count > 0) throw new InvalidOperationException($"Clean-up steps failed: {string.Join(", ", failed)}.");
}
}