using Chronos.Core.Archiving;
using Chronos.Core.Model;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Scheduling;
using Chronos.Protocol;
using Chronos.Server.Configuration;
using Chronos.Server.Storage;
namespace Chronos.Server.Jobs;
/// An age limit an environment variable fixes for a system schedule: the setting it comes from, the variable and the days.
public sealed record LockedAge(string Setting, string Variable, int Days);
///
/// What the server tells its (owner review 2, R10): the container's time zone (TZ), the archive it
/// serves with the settings another writer put into archive.json (refreshed at every run, as the former polling did), the
/// server's own clean-up steps, and its system schedules, which stand for every timer it ran out of sight before: the
/// archive scan every 10 minutes (no catch-up), the uploads sweep every 6 hours, the nightly copy of an SQLite index,
/// the job history and the audit log retentions. They are seeded from once (the user's edits
/// stay) and reset to the options as they are then.
///
public sealed class ServerScheduleHost : IScheduleHost
{
public const string ArchiveScan = "archive-scan";
public const string UploadCleanup = "upload-cleanup";
public const string IndexBackup = "index-backup";
public const string Housekeeping = "housekeeping";
public const string AuditCleanup = "audit-cleanup";
/// Minutes between two archive scans (the desktop's rhythm, as the polling had).
public const int ScanMinutes = 10;
///
/// Hours between two sweeps of abandoned uploads: rare enough to keep the job history readable (four clean-up jobs a
/// day); an idle upload still expires after , at most this much later.
///
public const int UploadSweepHours = 6;
private readonly ChronosDatabase _db;
private readonly ServerOptions _options;
private readonly ServerArchive _archive;
private readonly ArchiveService? _archives;
private readonly ILogger _logger;
public ServerScheduleHost(ChronosDatabase db, ServerOptions options, ServerArchive archive, IServiceProvider services, ILogger logger)
{
ArgumentNullException.ThrowIfNull(services);
_db = db;
_options = options;
_archive = archive;
_logger = logger;
// Without the archive module (tests with a fake ingest) nothing refreshes archive.json and no clean-up step exists.
_archives = services.GetService();
CleanupSteps = _archives is null ? [] : [.. _archives.CleanupSteps.Select(s => s.Name)];
TimeZone = ResolveTimeZone(Environment.GetEnvironmentVariable("TZ"), logger);
}
public TimeZoneInfo TimeZone { get; }
public IReadOnlyList CleanupSteps { get; }
/// Built from the options as they are now, so a reset follows a changed .
public IReadOnlyList SystemSchedules
{
get
{
var nightly = (Backup: new TimeOnly(3, 0), History: new TimeOnly(4, 0), Audit: new TimeOnly(4, 30));
var list = new List
{
// A missed scan is not caught up: the next one comes within minutes and SchedulerHost scans once at start.
new(ArchiveScan, "Archive scan", ScheduleActions.Scan, ScheduleRecurrence.Interval(ScanMinutes, RecurrenceUnit.Minutes), CatchUp: false),
new(UploadCleanup, "Upload clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.Interval(UploadSweepHours, RecurrenceUnit.Hours),
new ScheduleParametersDto(OlderThanDays: UploadMaintenance.FinishedRetentionDays, Steps: [UploadCleanupStep.StepName])),
};
// PostgreSQL is backed up with its own tools (BRIEF ยง6.2): no schedule that could never run.
if (DatabaseBackups.Unavailable(_db) is null)
{
list.Add(new(IndexBackup, "Index backup", ScheduleActions.Backup, ScheduleRecurrence.DailyAt(nightly.Backup),
new ScheduleParametersDto(Keep: DatabaseBackups.Keep)));
}
list.Add(new(Housekeeping, "Housekeeping", ScheduleActions.Cleanup, ScheduleRecurrence.DailyAt(nightly.History),
new ScheduleParametersDto(OlderThanDays: _options.JobHistoryDays, Steps: [Chronos.Data.Housekeeping.CleanupSteps.JobHistory])));
var audit = _options.AuditRetentionDays;
list.Add(new(AuditCleanup, "Audit log clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.DailyAt(nightly.Audit),
new ScheduleParametersDto(OlderThanDays: audit, Steps: [Chronos.Data.Housekeeping.CleanupSteps.Audit]), Enabled: audit > 0));
return list;
}
}
///
/// The served archive, after taking the settings another writer sharing it (a desktop) put into its archive.json, as
/// the former polling did before each scan; while it is offline, or the file cannot be read, as the index has it.
///
public async ValueTask GetArchiveAsync(CancellationToken cancellationToken)
{
var current = _archive.Current;
if (_archives is null || ArchiveService.GetAvailability(current) == ArchiveAvailability.Offline) return current;
try
{
return await _archive.RefreshAsync(_archives, _db, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
_logger.LogWarning(ex, "The settings in the archive's archive.json could not be read; schedules use those of the index");
return _archive.Current;
}
}
/// The server runs every action it has a handler for; the scheduler finds the rest out itself.
public string? Unavailable(string action) => null;
/// The server has no such setting: a scan schedule's own deep parameter decides, off when unset.
public bool DeepAnalysisAfterScan => false;
///
/// The age limit of the system schedule that an environment variable fixes
/// (CHRONOS_AUDIT_RETENTION_DAYS for ); null when the schedule's own value applies.
///
public LockedAge? LockedAgeOf(string? systemKey) =>
systemKey == AuditCleanup && _options.Locked.Contains(nameof(ServerOptions.AuditRetentionDays))
? new LockedAge(nameof(ServerOptions.AuditRetentionDays), ServerOptions.AuditRetentionVariable, _options.AuditRetentionDays)
: null;
///
/// The zone of schedule times: the process's local zone (TZ in a container), by its IANA id where the platform maps it
/// (the admin panel formats times with it); UTC, with a warning, when TZ names a zone this system does not know (no
/// tzdata in the image).
///
public static TimeZoneInfo ResolveTimeZone(string? tz, ILogger logger)
{
ArgumentNullException.ThrowIfNull(logger);
var name = tz?.Trim().TrimStart(':');
// TZ may also be a path to a zone file, which the runtime reads itself.
if (!string.IsNullOrEmpty(name) && !Path.IsPathRooted(name) && !OperatingSystem.IsWindows() && !TimeZoneInfo.TryFindSystemTimeZoneById(name, out _))
{
logger.LogWarning("TZ is {Zone}, a time zone this system does not know (is tzdata installed?): schedules run in UTC", name);
return TimeZoneInfo.Utc;
}
var local = TimeZoneInfo.Local;
if (!local.HasIanaId && TimeZoneInfo.TryConvertWindowsIdToIanaId(local.Id, out var iana) && TimeZoneInfo.TryFindSystemTimeZoneById(iana, out var zone))
return zone;
return local;
}
}