feat(data): scheduler engine, schedules table and housekeeping jobs
Owner review 2, R10: every timer that queues work becomes a visible schedule, so the server and the desktop need one shared engine to run them. - Migration 0007: schedules table (system rows keyed by system_key) and jobs.schedule_id; Job.ScheduleId, JobDto.ScheduleId, JobRepository HasPendingAsync / ListPendingTargetsAsync / ListByScheduleAsync. - ScheduleRepository: seeding that keeps user edits, reset, compare-and-set claim of a run (one winner across processes), outcomes. - Scheduling: in-house recurrence (every/daily/weekly/monthly, DST gap moves forward, ambiguous time runs once), ScheduleActions resolving scan, verify, thumbnails, proxies, analysis, maintenance, backup, cleanup, duplicates and orphans into archive jobs, IScheduleHost, and the Scheduler loop (catch-up, clock going back, run now, CRUD as API v1 DTOs). - New job kinds Backup (16) and Cleanup (17) with handlers, shared clean-up steps (job history, audit log, trash report that never deletes) and host steps through ArchiveServiceOptions; read-only Duplicates and Orphans report handlers, so the server can run those kinds too. - JobEngine no longer purges history at start: the housekeeping schedule's job-history step does; JobEngine.CanRun and an EnqueueAsync with scheduleId. - Protocol DTOs for schedules; job kind names in en and it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit eb0edd2483a493db7c6945d5b494461cabb2a4ad)
This commit is contained in:
1 parent
0e84392f4f
commit
e5508e7e69
51 files changed
+3219
-30
No files matched your search
@@ -0,0 +1,52 @@
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Protocol;
|
||||
|
||||
namespace Chronos.Data.Scheduling;
|
||||
|
||||
/// <summary>
|
||||
/// What a host (the server, the desktop) tells its <see cref="Scheduler"/> (owner review 2, R10): its time zone, the archive
|
||||
/// schedules work on, the system schedules standing for its timers, the clean-up steps its jobs run, and the actions it
|
||||
/// cannot run. Called from the scheduler's loop, off any UI thread; members must be thread-safe.
|
||||
/// </summary>
|
||||
public interface IScheduleHost
|
||||
{
|
||||
/// <summary>The zone of daily, weekly and monthly times: the PC's on the desktop, the container's (TZ) on the server.</summary>
|
||||
TimeZoneInfo TimeZone { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The archive schedules targeting <c>current</c> work on (the server's archive, the desktop's current one), with its
|
||||
/// settings as fresh as the host keeps them; null when there is none yet or it cannot be reached. The scheduler probes
|
||||
/// whether it is online itself.
|
||||
/// </summary>
|
||||
ValueTask<Archive?> GetArchiveAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// The system schedules: every timer that queues work on this host, as a visible schedule. Seeded at start by key and
|
||||
/// never overwritten (the user's edits stay); <see cref="Scheduler.ResetAsync"/> puts one back to these values.
|
||||
/// </summary>
|
||||
IReadOnlyList<SystemSchedule> SystemSchedules { get; }
|
||||
|
||||
/// <summary>The names of the clean-up steps this host's <see cref="JobKind.Cleanup"/> jobs run (normally those of <c>ArchiveService.CleanupSteps</c>).</summary>
|
||||
IReadOnlyList<string> CleanupSteps { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Why this host does not run <paramref name="action"/> (a short code, e.g. <c>not-here</c>), or null when it may. Asked
|
||||
/// before the scheduler's own checks (<see cref="ScheduleUnavailable"/>), for the action list and at every run.
|
||||
/// </summary>
|
||||
string? Unavailable(string action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A host's system schedule: <paramref name="SystemKey"/> identifies it across starts (<c>archive-scan</c>,
|
||||
/// <c>index-backup</c>, <c>housekeeping</c>…); the rest is what it is seeded with and reset to. <paramref name="Name"/> is
|
||||
/// stored as given; clients show system schedules by their key in the user's language.
|
||||
/// </summary>
|
||||
public sealed record SystemSchedule(
|
||||
string SystemKey,
|
||||
string Name,
|
||||
string Action,
|
||||
ScheduleRecurrence Recurrence,
|
||||
ScheduleParametersDto? Parameters = null,
|
||||
bool Enabled = true,
|
||||
bool CatchUp = true,
|
||||
JobPriority Priority = JobPriority.Background);
|
||||
@@ -0,0 +1,291 @@
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Archiving.Jobs;
|
||||
using Chronos.Data.Housekeeping;
|
||||
using Chronos.Data.Processing;
|
||||
using Chronos.Data.Repositories;
|
||||
using Chronos.Protocol;
|
||||
|
||||
namespace Chronos.Data.Scheduling;
|
||||
|
||||
/// <summary>Whether a host runs an action, and why not (<see cref="ScheduleUnavailable"/> or the host's own code).</summary>
|
||||
public sealed record ScheduleActionInfo(string Name, bool Available, string? Reason);
|
||||
|
||||
/// <summary>
|
||||
/// What a schedule's run comes to: the jobs to queue (<see cref="ScheduleOutcome.Queued"/>), or none and why
|
||||
/// (<see cref="ScheduleOutcome.SkippedPending"/>, <see cref="ScheduleOutcome.SkippedOffline"/>,
|
||||
/// <see cref="ScheduleOutcome.SkippedNothing"/>, <see cref="ScheduleOutcome.Unavailable"/>).
|
||||
/// </summary>
|
||||
public sealed record ScheduleResolution(IReadOnlyList<ArchiveJobRequest> Requests, string Outcome)
|
||||
{
|
||||
public static ScheduleResolution Skip(string outcome) => new([], outcome);
|
||||
|
||||
public static ScheduleResolution Queue(IReadOnlyList<ArchiveJobRequest> requests) => new(requests, ScheduleOutcome.Queued);
|
||||
}
|
||||
|
||||
/// <summary>What resolving an action needs: the index, the host, which job kinds the host's engine runs, and the time of the run.</summary>
|
||||
public sealed record ScheduleActionContext(ChronosDatabase Database, IScheduleHost Host, Func<JobKind, bool> CanRun, DateTimeOffset Now);
|
||||
|
||||
/// <summary>
|
||||
/// The actions a schedule can take (owner review 2, R10), shared by the server and the desktop: their names, parameters
|
||||
/// (<see cref="ScheduleParametersDto"/>, normalised to the fields each reads), availability, and the archive jobs a run
|
||||
/// queues for the host's current archive. Rebuild and render are not schedulable. Every action that works on an archive
|
||||
/// skips a run while the archive is missing or offline (or read-only, for one that writes), and while a job doing the same
|
||||
/// work on it is pending; a run that finds nothing to do queues nothing.
|
||||
/// </summary>
|
||||
public static class ScheduleActions
|
||||
{
|
||||
/// <summary>Incremental scan of the archive (<c>force</c>: a full one; <c>deep</c>: deep analysis of what it indexes).</summary>
|
||||
public const string Scan = "scan";
|
||||
|
||||
/// <summary>Integrity check of the originals, the least recently verified first (<c>olderThanDays</c>, <c>percent</c>).</summary>
|
||||
public const string Verify = "verify";
|
||||
|
||||
/// <summary>Thumbnails of the sequences without them (<c>force</c>: of every sequence).</summary>
|
||||
public const string Thumbnails = "thumbnails";
|
||||
|
||||
/// <summary>Proxies of the sequences without them, when the archive makes proxies (<c>force</c>: of every sequence).</summary>
|
||||
public const string Proxies = "proxies";
|
||||
|
||||
/// <summary>Light analysis of the sequences without a current one (<c>deep</c>: deep analysis of those without one).</summary>
|
||||
public const string Analysis = "analysis";
|
||||
|
||||
/// <summary>The archive's maintenance job (<c>dryRun</c>: report only).</summary>
|
||||
public const string Maintenance = "maintenance";
|
||||
|
||||
/// <summary>A copy of the SQLite index (<c>keep</c>: copies kept). Not on PostgreSQL.</summary>
|
||||
public const string Backup = "backup";
|
||||
|
||||
/// <summary>Clean-up steps (<c>steps</c>, <c>olderThanDays</c>): job history, audit log, trash report, the host's own.</summary>
|
||||
public const string Cleanup = "cleanup";
|
||||
|
||||
/// <summary>Report of the duplicate photos into the job log.</summary>
|
||||
public const string Duplicates = "duplicates";
|
||||
|
||||
/// <summary>Report of where the index and the disk disagree into the job log.</summary>
|
||||
public const string Orphans = "orphans";
|
||||
|
||||
/// <summary>Most days a schedule's age limits accept (ten years).</summary>
|
||||
public const int MaxDays = 3650;
|
||||
|
||||
/// <summary>Sequences per job a run queues: a larger set becomes several jobs.</summary>
|
||||
public const int SequencesPerJob = 500;
|
||||
|
||||
/// <summary>Every action, in the order clients list them.</summary>
|
||||
public static IReadOnlyList<string> All { get; } = [Scan, Verify, Thumbnails, Proxies, Analysis, Maintenance, Backup, Cleanup, Duplicates, Orphans];
|
||||
|
||||
public static bool IsKnown(string? action) => action is not null && All.Contains(action, StringComparer.Ordinal);
|
||||
|
||||
/// <summary>The job kinds <paramref name="action"/> queues (analysis: light, or deep with <c>deep</c>); empty for an unknown one.</summary>
|
||||
public static IReadOnlyList<JobKind> KindsOf(string action) => action switch
|
||||
{
|
||||
Scan => [JobKind.Scan],
|
||||
Verify => [JobKind.Verify],
|
||||
Thumbnails => [JobKind.Thumbs],
|
||||
Proxies => [JobKind.Proxies],
|
||||
Analysis => [JobKind.LightAnalyse, JobKind.DeepAnalyse],
|
||||
Maintenance => [JobKind.Maintenance],
|
||||
Backup => [JobKind.Backup],
|
||||
Cleanup => [JobKind.Cleanup],
|
||||
Duplicates => [JobKind.Duplicates],
|
||||
Orphans => [JobKind.Orphans],
|
||||
_ => [],
|
||||
};
|
||||
|
||||
/// <summary>The parameters a new schedule of <paramref name="action"/> starts with.</summary>
|
||||
public static ScheduleParametersDto Defaults(string action) => action switch
|
||||
{
|
||||
Scan => new(Force: false, Deep: false),
|
||||
Verify => new(OlderThanDays: 30, Percent: 100),
|
||||
Thumbnails or Proxies => new(Force: false),
|
||||
Analysis => new(Deep: false),
|
||||
Maintenance => new(DryRun: false),
|
||||
Backup => new(Keep: DatabaseBackups.Keep),
|
||||
Cleanup => new(Steps: [CleanupSteps.JobHistory], OlderThanDays: CleanupSteps.DefaultHistoryDays),
|
||||
_ => new(),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// <paramref name="parameters"/> reduced to the fields <paramref name="action"/> reads, each missing one taken from
|
||||
/// <see cref="Defaults"/>; clean-up steps keep their order without repeats.
|
||||
/// </summary>
|
||||
public static ScheduleParametersDto Normalize(string action, ScheduleParametersDto? parameters)
|
||||
{
|
||||
var p = parameters ?? new ScheduleParametersDto();
|
||||
var d = Defaults(action);
|
||||
return action switch
|
||||
{
|
||||
Scan => new(Force: p.Force ?? d.Force, Deep: p.Deep ?? d.Deep),
|
||||
Verify => new(OlderThanDays: p.OlderThanDays ?? d.OlderThanDays, Percent: p.Percent ?? d.Percent),
|
||||
Thumbnails or Proxies => new(Force: p.Force ?? d.Force),
|
||||
Analysis => new(Deep: p.Deep ?? d.Deep),
|
||||
Maintenance => new(DryRun: p.DryRun ?? d.DryRun),
|
||||
Backup => new(Keep: p.Keep ?? d.Keep),
|
||||
Cleanup => new(Steps: p.Steps is { } steps ? [.. steps.Distinct(StringComparer.Ordinal)] : d.Steps, OlderThanDays: p.OlderThanDays ?? d.OlderThanDays),
|
||||
_ => new(),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The first invalid field of a schedule's action and parameters, by its API name (<c>action</c>,
|
||||
/// <c>parameters.percent</c>, <c>parameters.steps</c>…); null when they are valid. <paramref name="steps"/> are the
|
||||
/// clean-up steps the host runs.
|
||||
/// </summary>
|
||||
public static string? Problem(string action, ScheduleParametersDto? parameters, IReadOnlyCollection<string> steps)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(steps);
|
||||
if (!IsKnown(action)) return "action";
|
||||
var p = Normalize(action, parameters);
|
||||
if (p.OlderThanDays is < 0 or > MaxDays) return "parameters.olderThanDays";
|
||||
if (p.Percent is < 1 or > 100) return "parameters.percent";
|
||||
if (p.Keep is < 1 or > BackupJobHandler.MaxKeep) return "parameters.keep";
|
||||
if (action == Cleanup && (p.Steps is not { Count: > 0 } named || named.Any(s => !steps.Contains(s)))) return "parameters.steps";
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Every action with whether this host runs it: not when the host says so (<see cref="IScheduleHost.Unavailable"/>), its
|
||||
/// engine has no handler for the kind (<see cref="ScheduleUnavailable.NoHandler"/>), the index cannot be copied (backup:
|
||||
/// <see cref="ScheduleUnavailable.Postgres"/>, <see cref="ScheduleUnavailable.NoIndexFile"/>), or the current archive makes
|
||||
/// no proxies (<see cref="ScheduleUnavailable.ProxiesDisabled"/>). A missing or offline archive leaves actions available:
|
||||
/// their runs are skipped until it is back.
|
||||
/// </summary>
|
||||
public static async Task<IReadOnlyList<ScheduleActionInfo>> AvailabilityAsync(ScheduleActionContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
var archive = await context.Host.GetArchiveAsync(cancellationToken).ConfigureAwait(false);
|
||||
return [.. All.Select(a => Unavailable(context, a, null, archive) is { } reason ? new ScheduleActionInfo(a, false, reason) : new ScheduleActionInfo(a, true, null))];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What a run of <paramref name="action"/> with <paramref name="parameters"/> queues now for the host's current archive,
|
||||
/// or why it queues nothing (see the type's remarks). Read-only: nothing is queued here.
|
||||
/// </summary>
|
||||
public static async Task<ScheduleResolution> ResolveAsync(ScheduleActionContext context, string action, ScheduleParametersDto? parameters,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
if (!IsKnown(action)) return ScheduleResolution.Skip(ScheduleOutcome.Unavailable);
|
||||
var p = Normalize(action, parameters);
|
||||
var db = context.Database;
|
||||
var archive = await context.Host.GetArchiveAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (Unavailable(context, action, p, archive) is not null) return ScheduleResolution.Skip(ScheduleOutcome.Unavailable);
|
||||
switch (action)
|
||||
{
|
||||
case Backup:
|
||||
return ScheduleResolution.Queue([ArchiveJobs.Backup(p.Keep!.Value)]);
|
||||
case Cleanup:
|
||||
return ScheduleResolution.Queue([ArchiveJobs.Cleanup(archive, p.Steps!, p.OlderThanDays!.Value)]);
|
||||
}
|
||||
|
||||
if (archive is null) return ScheduleResolution.Skip(ScheduleOutcome.SkippedOffline);
|
||||
var availability = ArchiveService.GetAvailability(archive);
|
||||
var writes = action is not (Verify or Duplicates or Orphans) && !(action == Maintenance && p.DryRun == true);
|
||||
if (availability == ArchiveAvailability.Offline || (writes && availability != ArchiveAvailability.Online))
|
||||
return ScheduleResolution.Skip(ScheduleOutcome.SkippedOffline);
|
||||
|
||||
switch (action)
|
||||
{
|
||||
case Scan:
|
||||
return await PendingAsync(db, [JobKind.Scan, JobKind.Rebuild], archive, cancellationToken).ConfigureAwait(false)
|
||||
?? ScheduleResolution.Queue([ArchiveJobs.Scan(archive, full: p.Force == true, deepAnalysis: p.Deep == true)]);
|
||||
case Verify:
|
||||
return await PendingAsync(db, [JobKind.Verify], archive, cancellationToken).ConfigureAwait(false)
|
||||
?? await VerifyAsync(context, archive, p, cancellationToken).ConfigureAwait(false);
|
||||
case Thumbnails:
|
||||
case Proxies:
|
||||
{
|
||||
var kind = action == Thumbnails ? JobKind.Thumbs : JobKind.Proxies;
|
||||
var built = action == Thumbnails ? SequenceStatus.ThumbsBuilt : SequenceStatus.ProxiesBuilt;
|
||||
var ids = await SequencesAsync(db, archive.Id, p.Force == true ? SequenceStatus.None : built, cancellationToken).ConfigureAwait(false);
|
||||
return await ChunkedAsync(db, kind, ids, chunk => action == Thumbnails
|
||||
? ArchiveJobs.Thumbnails(chunk, archive.Name, p.Force == true)
|
||||
: ArchiveJobs.Proxies(chunk, archive.Name, p.Force == true), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
case Analysis:
|
||||
{
|
||||
var deep = p.Deep == true;
|
||||
var ids = deep
|
||||
? await SequencesAsync(db, archive.Id, SequenceStatus.AnalyzedDeep, cancellationToken).ConfigureAwait(false)
|
||||
: await AnalysisFollowUp.UnanalysedAsync(db, archive.Id, cancellationToken).ConfigureAwait(false);
|
||||
return await ChunkedAsync(db, deep ? JobKind.DeepAnalyse : JobKind.LightAnalyse, ids, chunk => deep
|
||||
? ProcessingJobs.DeepAnalysis(chunk, archive.Name)
|
||||
: ProcessingJobs.LightAnalysis(chunk, archive.Name), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
case Maintenance:
|
||||
return await PendingAsync(db, [JobKind.Maintenance], archive, cancellationToken).ConfigureAwait(false)
|
||||
?? ScheduleResolution.Queue([ArchiveJobs.Maintenance(archive, p.DryRun == true)]);
|
||||
case Duplicates:
|
||||
return await PendingAsync(db, [JobKind.Duplicates], archive, cancellationToken).ConfigureAwait(false)
|
||||
?? ScheduleResolution.Queue([ArchiveJobs.Duplicates(archive)]);
|
||||
default:
|
||||
return await PendingAsync(db, [JobKind.Orphans], archive, cancellationToken).ConfigureAwait(false)
|
||||
?? ScheduleResolution.Queue([ArchiveJobs.Orphans(archive)]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Why <paramref name="action"/> cannot run here; null when it can. With <paramref name="parameters"/>, the kind it would queue must run.</summary>
|
||||
private static string? Unavailable(ScheduleActionContext context, string action, ScheduleParametersDto? parameters, Archive? archive)
|
||||
{
|
||||
if (context.Host.Unavailable(action) is { } reason) return reason;
|
||||
var kinds = KindsOf(action);
|
||||
var kind = action == Analysis && parameters?.Deep == true ? JobKind.DeepAnalyse : kinds[0];
|
||||
if (!context.CanRun(kind)) return ScheduleUnavailable.NoHandler;
|
||||
if (action == Backup && DatabaseBackups.Unavailable(context.Database) is { } backup) return backup;
|
||||
if (action == Proxies && archive is { ProxyLongEdge: ProxyPolicy.Disabled }) return ScheduleUnavailable.ProxiesDisabled;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary><see cref="ScheduleOutcome.SkippedPending"/> while a job of <paramref name="kinds"/> on the archive is queued, running or paused.</summary>
|
||||
private static async Task<ScheduleResolution?> PendingAsync(ChronosDatabase db, IReadOnlyCollection<JobKind> kinds, Archive archive,
|
||||
CancellationToken cancellationToken) =>
|
||||
await db.Jobs.HasPendingAsync(kinds, [archive.Id], cancellationToken).ConfigureAwait(false) ? ScheduleResolution.Skip(ScheduleOutcome.SkippedPending) : null;
|
||||
|
||||
/// <summary>
|
||||
/// The least recently verified sequences: those not verified for <c>olderThanDays</c> (0: any), at most
|
||||
/// <c>percent</c> of the archive's sequences (rounded up). All of them at 100 % and 0 days, as one archive-wide job.
|
||||
/// </summary>
|
||||
private static async Task<ScheduleResolution> VerifyAsync(ScheduleActionContext context, Archive archive, ScheduleParametersDto p, CancellationToken cancellationToken)
|
||||
{
|
||||
var db = context.Database;
|
||||
var total = await db.Sequences.CountAsync(new SequenceQuery { ArchiveId = archive.Id }, cancellationToken).ConfigureAwait(false);
|
||||
if (total == 0) return ScheduleResolution.Skip(ScheduleOutcome.SkippedNothing);
|
||||
var days = p.OlderThanDays ?? 0;
|
||||
var percent = p.Percent ?? 100;
|
||||
if (days == 0 && percent >= 100) return ScheduleResolution.Queue([ArchiveJobs.Verify(archive)]);
|
||||
var limit = (int)Math.Ceiling(total * percent / 100.0);
|
||||
var ids = await db.Sequences.ListVerifyCandidatesAsync(archive.Id, days > 0 ? context.Now - TimeSpan.FromDays(days) : null, limit, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
return ids.Count == 0 ? ScheduleResolution.Skip(ScheduleOutcome.SkippedNothing) : ScheduleResolution.Queue([ArchiveJobs.Verify(archive, ids)]);
|
||||
}
|
||||
|
||||
/// <summary>The sequences of an archive with frames and without <paramref name="without"/> (none: every one), oldest capture first.</summary>
|
||||
private static async Task<List<Guid>> SequencesAsync(ChronosDatabase db, Guid archiveId, SequenceStatus without, CancellationToken cancellationToken)
|
||||
{
|
||||
var ids = new List<Guid>();
|
||||
var query = new SequenceQuery { ArchiveId = archiveId, WithoutStatus = without, Sort = SequenceSort.Oldest, Limit = 1000 };
|
||||
do
|
||||
{
|
||||
var page = await db.Sequences.ListAsync(query, cancellationToken).ConfigureAwait(false);
|
||||
ids.AddRange(page.Items.Where(s => s.FrameCount > 0).Select(s => s.Id));
|
||||
query = query with { Cursor = page.NextCursor };
|
||||
}
|
||||
while (query.Cursor is not null);
|
||||
return ids;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The jobs for <paramref name="ids"/> less those a pending job of <paramref name="kind"/> already covers, at most
|
||||
/// <see cref="SequencesPerJob"/> per job; <see cref="ScheduleOutcome.SkippedNothing"/> when none is left.
|
||||
/// </summary>
|
||||
private static async Task<ScheduleResolution> ChunkedAsync(ChronosDatabase db, JobKind kind, IReadOnlyList<Guid> ids,
|
||||
Func<IReadOnlyList<Guid>, ArchiveJobRequest> request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (ids.Count == 0) return ScheduleResolution.Skip(ScheduleOutcome.SkippedNothing);
|
||||
var pending = await db.Jobs.ListPendingTargetsAsync(kind, cancellationToken).ConfigureAwait(false);
|
||||
var left = ids.Where(id => !pending.Contains(id)).Distinct().ToList();
|
||||
if (left.Count == 0) return ScheduleResolution.Skip(pending.Count > 0 ? ScheduleOutcome.SkippedPending : ScheduleOutcome.SkippedNothing);
|
||||
return ScheduleResolution.Queue([.. left.Chunk(SequencesPerJob).Select(chunk => request(chunk))]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Protocol;
|
||||
|
||||
namespace Chronos.Data.Scheduling;
|
||||
|
||||
/// <summary>
|
||||
/// Schedules between the model and API v1 (<see cref="ScheduleDto"/>, <see cref="RecurrenceDto"/>), and the stored form of
|
||||
/// their parameters (<see cref="ScheduleParametersDto"/> as camelCase JSON, normalised to the fields the action reads).
|
||||
/// </summary>
|
||||
public static class ScheduleMapping
|
||||
{
|
||||
/// <summary>Wire names of <see cref="RecurrenceKind"/>: <c>every</c>, <c>daily</c>, <c>weekly</c>, <c>monthly</c>.</summary>
|
||||
public static string Kind(RecurrenceKind kind) => kind.ToString().ToLowerInvariant();
|
||||
|
||||
/// <summary>Wire names of <see cref="RecurrenceUnit"/>: <c>minutes</c>, <c>hours</c>, <c>days</c>.</summary>
|
||||
public static string Unit(RecurrenceUnit unit) => unit.ToString().ToLowerInvariant();
|
||||
|
||||
public static ScheduleDto ToDto(Schedule schedule)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schedule);
|
||||
var recurrence = ScheduleRecurrence.TryParse(schedule.Recurrence, out var parsed) ? ToDto(parsed) : new RecurrenceDto(schedule.Recurrence);
|
||||
return new ScheduleDto(schedule.Id, schedule.SystemKey, schedule.Name, schedule.Action, schedule.Target, Parameters(schedule), recurrence,
|
||||
schedule.CatchUp, JobWire.Priority(schedule.Priority), schedule.Enabled, schedule.NextRun, schedule.LastRun, schedule.LastJobId,
|
||||
schedule.LastOutcome, schedule.CreatedBy, schedule.Created, schedule.Updated);
|
||||
}
|
||||
|
||||
public static RecurrenceDto ToDto(ScheduleRecurrence recurrence)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(recurrence);
|
||||
var time = recurrence.Time.ToString("HH:mm", CultureInfo.InvariantCulture);
|
||||
return recurrence.Kind switch
|
||||
{
|
||||
RecurrenceKind.Every => new RecurrenceDto(Kind(recurrence.Kind), Every: recurrence.Every, Unit: Unit(recurrence.Unit)),
|
||||
RecurrenceKind.Daily => new RecurrenceDto(Kind(recurrence.Kind), Time: time),
|
||||
RecurrenceKind.Weekly => new RecurrenceDto(Kind(recurrence.Kind), Time: time, Days: [.. recurrence.Days.Select(ScheduleRecurrence.DayName)]),
|
||||
_ => new RecurrenceDto(Kind(recurrence.Kind), Time: time, DayOfMonth: recurrence.DayOfMonth),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The recurrence <paramref name="dto"/> describes; null when it is missing or invalid (out of range, unknown names).</summary>
|
||||
public static ScheduleRecurrence? FromDto(RecurrenceDto? dto)
|
||||
{
|
||||
if (dto is null) return null;
|
||||
var text = dto.Kind?.ToLowerInvariant() switch
|
||||
{
|
||||
"every" when dto.Every is { } every && Enum.TryParse<RecurrenceUnit>(dto.Unit, ignoreCase: true, out var unit) && Enum.IsDefined(unit)
|
||||
=> string.Create(CultureInfo.InvariantCulture, $"every:{every}{Unit(unit)[0]}"),
|
||||
"daily" => "daily:" + dto.Time,
|
||||
"weekly" when dto.Days is { Count: > 0 } days => $"weekly:{string.Join(',', days)}@{dto.Time}",
|
||||
"monthly" when dto.DayOfMonth is { } day => string.Create(CultureInfo.InvariantCulture, $"monthly:{day}@{dto.Time}"),
|
||||
_ => null,
|
||||
};
|
||||
return ScheduleRecurrence.TryParse(text, out var recurrence) ? recurrence : null;
|
||||
}
|
||||
|
||||
public static ScheduleActionDto ToDto(ScheduleActionInfo info)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(info);
|
||||
return new ScheduleActionDto(info.Name, info.Available, info.Reason);
|
||||
}
|
||||
|
||||
/// <summary>The schedule's parameters, normalised for its action (stored JSON that cannot be read counts as none).</summary>
|
||||
public static ScheduleParametersDto Parameters(Schedule schedule)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schedule);
|
||||
ScheduleParametersDto? stored = null;
|
||||
if (schedule.ParametersJson is { Length: > 0 } json)
|
||||
{
|
||||
try
|
||||
{
|
||||
stored = JsonSerializer.Deserialize(json, ProtocolJson.Default.ScheduleParametersDto);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
}
|
||||
}
|
||||
return ScheduleActions.Normalize(schedule.Action, stored);
|
||||
}
|
||||
|
||||
/// <summary>The stored form of <paramref name="parameters"/> for <paramref name="action"/>; null for an action without parameters.</summary>
|
||||
public static string? ParametersJson(string action, ScheduleParametersDto? parameters)
|
||||
{
|
||||
var normalized = ScheduleActions.Normalize(action, parameters);
|
||||
return normalized == new ScheduleParametersDto() ? null : JsonSerializer.Serialize(normalized, ProtocolJson.Default.ScheduleParametersDto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
namespace Chronos.Data.Scheduling;
|
||||
|
||||
/// <summary>
|
||||
/// What a schedule's run did (<see cref="Core.Model.Schedule.LastOutcome"/>), as stored and sent over the API. Clients
|
||||
/// show them in their own words; <see cref="Failed(string)"/> carries the reason as text.
|
||||
/// </summary>
|
||||
public static class ScheduleOutcome
|
||||
{
|
||||
/// <summary>The action's jobs were queued (the schedule keeps the first one's id).</summary>
|
||||
public const string Queued = "queued";
|
||||
|
||||
/// <summary>Not queued: the schedule's previous job, or a job doing the same work, is still queued, running or paused.</summary>
|
||||
public const string SkippedPending = "skipped-pending";
|
||||
|
||||
/// <summary>Not queued: the archive the action works on is missing, offline, or read-only for an action that writes.</summary>
|
||||
public const string SkippedOffline = "skipped-offline";
|
||||
|
||||
/// <summary>Not queued: nothing to do (every sequence verified recently, every thumbnail built…).</summary>
|
||||
public const string SkippedNothing = "skipped-nothing";
|
||||
|
||||
/// <summary>Not queued: this host cannot run the action (<see cref="ScheduleUnavailable"/> says why).</summary>
|
||||
public const string Unavailable = "unavailable";
|
||||
|
||||
/// <summary>Prefix of <see cref="Failed(string)"/>.</summary>
|
||||
public const string FailedPrefix = "failed:";
|
||||
|
||||
/// <summary>Longest reason kept after <see cref="FailedPrefix"/>.</summary>
|
||||
public const int MaxReasonLength = 200;
|
||||
|
||||
/// <summary><c>failed:<reason></c>: resolving or queueing the action threw; the reason is the error's message, shortened.</summary>
|
||||
public static string Failed(string reason)
|
||||
{
|
||||
var text = string.IsNullOrWhiteSpace(reason) ? "error" : reason.ReplaceLineEndings(" ").Trim();
|
||||
return FailedPrefix + (text.Length > MaxReasonLength ? text[..MaxReasonLength] : text);
|
||||
}
|
||||
|
||||
/// <summary>True for a <c>failed:</c> outcome, with its reason.</summary>
|
||||
public static bool IsFailed(string? outcome, out string reason)
|
||||
{
|
||||
var failed = outcome?.StartsWith(FailedPrefix, StringComparison.Ordinal) == true;
|
||||
reason = failed ? outcome![FailedPrefix.Length..] : string.Empty;
|
||||
return failed;
|
||||
}
|
||||
|
||||
/// <summary>The outcomes without a reason, for clients that name each one.</summary>
|
||||
public static IReadOnlyList<string> Plain { get; } = [Queued, SkippedPending, SkippedOffline, SkippedNothing, Unavailable];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Why an action cannot run on a host (<see cref="ScheduleActionInfo.Reason"/>, <c>ScheduleActionDto.reason</c>). A host's
|
||||
/// own hook (<see cref="IScheduleHost.Unavailable"/>) may answer with codes of its own; clients show unknown codes as
|
||||
/// "not available here".
|
||||
/// </summary>
|
||||
public static class ScheduleUnavailable
|
||||
{
|
||||
/// <summary>This host's job engine has no handler for the action's job kind.</summary>
|
||||
public const string NoHandler = "no-handler";
|
||||
|
||||
/// <summary>Index backups: the index is on PostgreSQL, which its own tools back up.</summary>
|
||||
public const string Postgres = "postgres";
|
||||
|
||||
/// <summary>Index backups: the SQLite index has no file (in memory).</summary>
|
||||
public const string NoIndexFile = "no-index-file";
|
||||
|
||||
/// <summary>Proxies: the archive's proxy policy makes none.</summary>
|
||||
public const string ProxiesDisabled = "proxies-disabled";
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace Chronos.Data.Scheduling;
|
||||
|
||||
/// <summary>The shape of a <see cref="ScheduleRecurrence"/>.</summary>
|
||||
public enum RecurrenceKind
|
||||
{
|
||||
/// <summary>A fixed interval: every n minutes, hours or days.</summary>
|
||||
Every,
|
||||
|
||||
/// <summary>Every day at a wall-clock time.</summary>
|
||||
Daily,
|
||||
|
||||
/// <summary>On some days of the week at a wall-clock time.</summary>
|
||||
Weekly,
|
||||
|
||||
/// <summary>On one day of the month (1–28) at a wall-clock time.</summary>
|
||||
Monthly,
|
||||
}
|
||||
|
||||
/// <summary>Unit of an <see cref="RecurrenceKind.Every"/> interval.</summary>
|
||||
public enum RecurrenceUnit
|
||||
{
|
||||
Minutes,
|
||||
Hours,
|
||||
Days,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When a schedule fires (owner review 2, R10), in an in-house text form instead of cron: <c>every:<n>m|h|d</c>
|
||||
/// (at least 5 minutes), <c>daily:HH:MM</c>, <c>weekly:mon,tue,…@HH:MM</c> and <c>monthly:<1–28>@HH:MM</c>.
|
||||
/// Intervals are fixed durations anchored to the previous due time, so they never drift and the occurrences missed meanwhile
|
||||
/// collapse into one; days of an interval are 24 hours, whatever the clocks do. Daily, weekly and monthly times are wall-clock
|
||||
/// times of the host's zone: a time a spring-forward gap skips moves forward by the gap (02:30 becomes 03:30), and a time the
|
||||
/// clocks go back over happens once, at its first occurrence. Instances compare by value.
|
||||
/// </summary>
|
||||
public sealed record ScheduleRecurrence
|
||||
{
|
||||
/// <summary>The shortest interval: a schedule never fires more than every 5 minutes.</summary>
|
||||
public const int MinMinutes = 5;
|
||||
|
||||
public const int MaxMinutes = 24 * 60;
|
||||
public const int MaxHours = 7 * 24;
|
||||
public const int MaxDays = 366;
|
||||
|
||||
/// <summary>The last day a monthly schedule can name: every month has it.</summary>
|
||||
public const int MaxDayOfMonth = 28;
|
||||
|
||||
/// <summary>Wire names of the days, Monday first, as the text form and the API write them.</summary>
|
||||
public static IReadOnlyList<string> DayNames { get; } = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
||||
|
||||
private static readonly DayOfWeek[] WeekOrder =
|
||||
[DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday];
|
||||
|
||||
/// <summary>Bit <c>1 << (int)DayOfWeek</c> per weekly day; a field, so equality compares the days.</summary>
|
||||
private readonly int _days;
|
||||
|
||||
private ScheduleRecurrence(RecurrenceKind kind, int every, RecurrenceUnit unit, TimeOnly time, int days, int dayOfMonth)
|
||||
{
|
||||
Kind = kind;
|
||||
Every = every;
|
||||
Unit = unit;
|
||||
Time = time;
|
||||
_days = days;
|
||||
DayOfMonth = dayOfMonth;
|
||||
}
|
||||
|
||||
public RecurrenceKind Kind { get; }
|
||||
|
||||
/// <summary>The interval's count of <see cref="Unit"/>; 0 for the other kinds.</summary>
|
||||
public int Every { get; }
|
||||
|
||||
/// <summary>The interval's unit (<see cref="RecurrenceKind.Every"/> only).</summary>
|
||||
public RecurrenceUnit Unit { get; }
|
||||
|
||||
/// <summary>Wall-clock time of a daily, weekly or monthly schedule, to the minute; midnight for an interval.</summary>
|
||||
public TimeOnly Time { get; }
|
||||
|
||||
/// <summary>The days of a weekly schedule, Monday first; empty for the other kinds.</summary>
|
||||
public IReadOnlyList<DayOfWeek> Days => [.. WeekOrder.Where(d => (_days & Bit(d)) != 0)];
|
||||
|
||||
/// <summary>Day of the month of a monthly schedule (1–28); 0 for the other kinds.</summary>
|
||||
public int DayOfMonth { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The longest time between two occurrences: the interval, a day, a week, or 31 days for a monthly schedule. A next run
|
||||
/// further away than two of them means the clock went back, and the next run is computed again.
|
||||
/// </summary>
|
||||
public TimeSpan Period => Kind switch
|
||||
{
|
||||
RecurrenceKind.Every => Unit switch
|
||||
{
|
||||
RecurrenceUnit.Minutes => TimeSpan.FromMinutes(Every),
|
||||
RecurrenceUnit.Hours => TimeSpan.FromHours(Every),
|
||||
_ => TimeSpan.FromDays(Every),
|
||||
},
|
||||
RecurrenceKind.Daily => TimeSpan.FromDays(1),
|
||||
RecurrenceKind.Weekly => TimeSpan.FromDays(7),
|
||||
_ => TimeSpan.FromDays(31),
|
||||
};
|
||||
|
||||
/// <summary>Every <paramref name="every"/> <paramref name="unit"/> (at least <see cref="MinMinutes"/> minutes).</summary>
|
||||
/// <exception cref="ArgumentOutOfRangeException">The interval is shorter than 5 minutes or longer than its unit allows.</exception>
|
||||
public static ScheduleRecurrence Interval(int every, RecurrenceUnit unit)
|
||||
{
|
||||
var (min, max) = unit switch
|
||||
{
|
||||
RecurrenceUnit.Minutes => (MinMinutes, MaxMinutes),
|
||||
RecurrenceUnit.Hours => (1, MaxHours),
|
||||
RecurrenceUnit.Days => (1, MaxDays),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(unit), unit, "Unknown unit."),
|
||||
};
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(every, min);
|
||||
ArgumentOutOfRangeException.ThrowIfGreaterThan(every, max);
|
||||
return new ScheduleRecurrence(RecurrenceKind.Every, every, unit, default, 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>Every day at <paramref name="time"/> (seconds are dropped).</summary>
|
||||
public static ScheduleRecurrence DailyAt(TimeOnly time) => new(RecurrenceKind.Daily, 0, default, Minute(time), 0, 0);
|
||||
|
||||
/// <summary>On <paramref name="days"/> at <paramref name="time"/>.</summary>
|
||||
/// <exception cref="ArgumentException">No day given.</exception>
|
||||
public static ScheduleRecurrence WeeklyAt(IEnumerable<DayOfWeek> days, TimeOnly time)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(days);
|
||||
var mask = 0;
|
||||
foreach (var day in days)
|
||||
{
|
||||
if (!Enum.IsDefined(day)) throw new ArgumentOutOfRangeException(nameof(days), day, "Unknown day.");
|
||||
mask |= Bit(day);
|
||||
}
|
||||
if (mask == 0) throw new ArgumentException("A weekly schedule needs at least one day.", nameof(days));
|
||||
return new ScheduleRecurrence(RecurrenceKind.Weekly, 0, default, Minute(time), mask, 0);
|
||||
}
|
||||
|
||||
/// <summary>On day <paramref name="dayOfMonth"/> (1–28) of every month at <paramref name="time"/>.</summary>
|
||||
public static ScheduleRecurrence MonthlyAt(int dayOfMonth, TimeOnly time)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(dayOfMonth, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfGreaterThan(dayOfMonth, MaxDayOfMonth);
|
||||
return new ScheduleRecurrence(RecurrenceKind.Monthly, 0, default, Minute(time), 0, dayOfMonth);
|
||||
}
|
||||
|
||||
/// <summary>The recurrence written as <paramref name="text"/> (any case, spaces around parts allowed).</summary>
|
||||
/// <exception cref="FormatException">The text is not a valid recurrence.</exception>
|
||||
public static ScheduleRecurrence Parse(string text) =>
|
||||
TryParse(text, out var recurrence) ? recurrence : throw new FormatException($"'{text}' is not a schedule recurrence.");
|
||||
|
||||
/// <summary>Parses the text form; false for anything else, values out of range included.</summary>
|
||||
public static bool TryParse([NotNullWhen(true)] string? text, [NotNullWhen(true)] out ScheduleRecurrence? recurrence)
|
||||
{
|
||||
recurrence = null;
|
||||
if (string.IsNullOrWhiteSpace(text)) return false;
|
||||
var colon = text.IndexOf(':', StringComparison.Ordinal);
|
||||
if (colon <= 0) return false;
|
||||
var kind = text[..colon].Trim().ToLowerInvariant();
|
||||
var rest = text[(colon + 1)..].Trim();
|
||||
try
|
||||
{
|
||||
recurrence = kind switch
|
||||
{
|
||||
"every" => ParseInterval(rest),
|
||||
"daily" => TryTime(rest, out var time) ? DailyAt(time) : null,
|
||||
"weekly" => ParseWeekly(rest),
|
||||
"monthly" => ParseMonthly(rest),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
// A value out of range (every:4m, monthly:29@…).
|
||||
recurrence = null;
|
||||
}
|
||||
return recurrence is not null;
|
||||
}
|
||||
|
||||
/// <summary>The canonical text form: <c>every:10m</c>, <c>daily:03:00</c>, <c>weekly:mon,thu@02:30</c>, <c>monthly:1@04:00</c>.</summary>
|
||||
public string Format()
|
||||
{
|
||||
var time = Time.ToString("HH:mm", CultureInfo.InvariantCulture);
|
||||
return Kind switch
|
||||
{
|
||||
RecurrenceKind.Every => string.Create(CultureInfo.InvariantCulture,
|
||||
$"every:{Every}{Unit switch { RecurrenceUnit.Minutes => 'm', RecurrenceUnit.Hours => 'h', _ => 'd' }}"),
|
||||
RecurrenceKind.Daily => "daily:" + time,
|
||||
RecurrenceKind.Weekly => new StringBuilder("weekly:").AppendJoin(',', Days.Select(DayName)).Append('@').Append(time).ToString(),
|
||||
_ => string.Create(CultureInfo.InvariantCulture, $"monthly:{DayOfMonth}@{time}"),
|
||||
};
|
||||
}
|
||||
|
||||
public override string ToString() => Format();
|
||||
|
||||
/// <summary>The wire name of <paramref name="day"/>: <c>mon</c> … <c>sun</c>.</summary>
|
||||
public static string DayName(DayOfWeek day) => DayNames[Array.IndexOf(WeekOrder, day)];
|
||||
|
||||
/// <summary>The day named <paramref name="name"/> (<see cref="DayNames"/>, any case); false for anything else.</summary>
|
||||
public static bool TryParseDay(string? name, out DayOfWeek day)
|
||||
{
|
||||
var index = IndexOfDay(name);
|
||||
day = index < 0 ? default : WeekOrder[index];
|
||||
return index >= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The first occurrence strictly after <paramref name="afterUtc"/>, in UTC and to the minute. An interval is laid on the
|
||||
/// grid of <paramref name="anchorUtc"/> (the previous due time: no drift, missed occurrences collapse into the next one),
|
||||
/// or starts one interval after <paramref name="afterUtc"/>'s minute without an anchor. Daily, weekly and monthly times
|
||||
/// are read in <paramref name="zone"/> (see the type's remarks for clock changes); they need no anchor.
|
||||
/// </summary>
|
||||
public DateTimeOffset Next(DateTimeOffset afterUtc, TimeZoneInfo zone, DateTimeOffset? anchorUtc = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(zone);
|
||||
var after = afterUtc.ToUniversalTime();
|
||||
if (Kind == RecurrenceKind.Every)
|
||||
{
|
||||
var period = Period.Ticks;
|
||||
if (anchorUtc is not { } anchor) return Truncate(after) + Period;
|
||||
var behind = after.UtcTicks - anchor.UtcTicks;
|
||||
var steps = behind / period;
|
||||
if (behind % period != 0 && behind < 0) steps--;
|
||||
return new DateTimeOffset(anchor.UtcTicks + (steps + 1) * period, TimeSpan.Zero);
|
||||
}
|
||||
var local = DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(after, zone).DateTime);
|
||||
// From the day before: a time a gap moves forward can land after a later midnight.
|
||||
for (var i = -1; i <= 400; i++)
|
||||
{
|
||||
var day = local.AddDays(i);
|
||||
if (!Matches(day)) continue;
|
||||
var at = ToUtc(day.ToDateTime(Time), zone);
|
||||
if (at > after) return at;
|
||||
}
|
||||
throw new InvalidOperationException($"No occurrence of {Format()} within a year.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The UTC instant of the wall-clock time <paramref name="local"/> in <paramref name="zone"/>: a time inside a
|
||||
/// spring-forward gap moves forward by the gap, an ambiguous one takes its first occurrence (still on the larger offset).
|
||||
/// </summary>
|
||||
public static DateTimeOffset ToUtc(DateTime local, TimeZoneInfo zone)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(zone);
|
||||
local = DateTime.SpecifyKind(local, DateTimeKind.Unspecified);
|
||||
TimeSpan offset;
|
||||
if (zone.IsInvalidTime(local)) offset = zone.GetUtcOffset(local.AddHours(-12));
|
||||
else if (zone.IsAmbiguousTime(local)) offset = zone.GetAmbiguousTimeOffsets(local).Max();
|
||||
else offset = zone.GetUtcOffset(local);
|
||||
return new DateTimeOffset(local.Ticks - offset.Ticks, TimeSpan.Zero);
|
||||
}
|
||||
|
||||
/// <summary><c>10m</c>, <c>6h</c>, <c>7d</c>; null for anything else.</summary>
|
||||
private static ScheduleRecurrence? ParseInterval(string text)
|
||||
{
|
||||
if (text.Length < 2) return null;
|
||||
RecurrenceUnit? unit = char.ToLowerInvariant(text[^1]) switch
|
||||
{
|
||||
'm' => RecurrenceUnit.Minutes,
|
||||
'h' => RecurrenceUnit.Hours,
|
||||
'd' => RecurrenceUnit.Days,
|
||||
_ => null,
|
||||
};
|
||||
return unit is { } u && TryNumber(text[..^1], out var every) ? Interval(every, u) : null;
|
||||
}
|
||||
|
||||
/// <summary><c>mon,thu@02:30</c>; null for anything else.</summary>
|
||||
private static ScheduleRecurrence? ParseWeekly(string text)
|
||||
{
|
||||
var at = text.LastIndexOf('@');
|
||||
if (at <= 0 || !TryTime(text[(at + 1)..], out var time)) return null;
|
||||
var days = new List<DayOfWeek>();
|
||||
foreach (var name in text[..at].Split(','))
|
||||
{
|
||||
if (!TryParseDay(name.Trim(), out var day)) return null;
|
||||
days.Add(day);
|
||||
}
|
||||
return WeeklyAt(days, time);
|
||||
}
|
||||
|
||||
/// <summary><c>1@04:00</c>; null for anything else.</summary>
|
||||
private static ScheduleRecurrence? ParseMonthly(string text)
|
||||
{
|
||||
var at = text.LastIndexOf('@');
|
||||
return at > 0 && TryNumber(text[..at], out var day) && TryTime(text[(at + 1)..], out var time) ? MonthlyAt(day, time) : null;
|
||||
}
|
||||
|
||||
private bool Matches(DateOnly day) => Kind switch
|
||||
{
|
||||
RecurrenceKind.Daily => true,
|
||||
RecurrenceKind.Weekly => (_days & Bit(day.DayOfWeek)) != 0,
|
||||
_ => day.Day == DayOfMonth,
|
||||
};
|
||||
|
||||
private static int Bit(DayOfWeek day) => 1 << (int)day;
|
||||
|
||||
private static TimeOnly Minute(TimeOnly time) => new(time.Hour, time.Minute);
|
||||
|
||||
private static DateTimeOffset Truncate(DateTimeOffset utc) => new(utc.UtcTicks - utc.UtcTicks % TimeSpan.TicksPerMinute, TimeSpan.Zero);
|
||||
|
||||
private static int IndexOfDay(string? name)
|
||||
{
|
||||
for (var i = 0; i < DayNames.Count; i++)
|
||||
{
|
||||
if (string.Equals(DayNames[i], name, StringComparison.OrdinalIgnoreCase)) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static bool TryNumber(string text, out int value) =>
|
||||
int.TryParse(text.Trim(), NumberStyles.None, CultureInfo.InvariantCulture, out value);
|
||||
|
||||
private static bool TryTime(string text, out TimeOnly time)
|
||||
{
|
||||
time = default;
|
||||
var parts = text.Trim().Split(':');
|
||||
if (parts is not [var h, var m] || h.Length is < 1 or > 2 || m.Length != 2
|
||||
|| !TryNumber(h, out var hour) || !TryNumber(m, out var minute) || hour > 23 || minute > 59)
|
||||
return false;
|
||||
time = new TimeOnly(hour, minute);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,429 @@
|
||||
using System.Threading.Channels;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Protocol;
|
||||
|
||||
namespace Chronos.Data.Scheduling;
|
||||
|
||||
/// <summary>How a <see cref="Scheduler"/> keeps time.</summary>
|
||||
public sealed record SchedulerOptions
|
||||
{
|
||||
/// <summary>Clock of the runs (tests pass their own); its timers pace the loop.</summary>
|
||||
public TimeProvider Time { get; init; } = TimeProvider.System;
|
||||
|
||||
/// <summary>How often the loop looks for due schedules (and for schedules other processes changed).</summary>
|
||||
public TimeSpan Tick { get; init; } = TimeSpan.FromSeconds(30);
|
||||
|
||||
/// <summary>
|
||||
/// How late a run may be and still count as on time. A schedule further behind (the host was off, the PC slept) missed
|
||||
/// its run: with catch-up it fires once, without it moves to its next occurrence without running.
|
||||
/// </summary>
|
||||
public TimeSpan CatchUpGrace { get; init; } = TimeSpan.FromMinutes(5);
|
||||
}
|
||||
|
||||
/// <summary>What a schedule's run did: the outcome (<see cref="ScheduleOutcome"/>) and the first job it queued.</summary>
|
||||
internal sealed record ScheduleFire(Guid ScheduleId, string Outcome, Guid? JobId);
|
||||
|
||||
/// <summary>A schedule created or edited, or the first invalid field by its API name (<c>name</c>, <c>recurrence</c>, <c>parameters.steps</c>…).</summary>
|
||||
public sealed record ScheduleEditResult(ScheduleDto? Schedule, string? Problem)
|
||||
{
|
||||
/// <summary>True when the schedule to edit does not exist.</summary>
|
||||
public bool NotFound => Schedule is null && Problem is null;
|
||||
}
|
||||
|
||||
/// <summary>What <see cref="Scheduler.DeleteAsync"/> did.</summary>
|
||||
public enum ScheduleDeleteOutcome
|
||||
{
|
||||
Deleted,
|
||||
NotFound,
|
||||
|
||||
/// <summary>A system schedule: disable it instead.</summary>
|
||||
System,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the schedules of one host (owner review 2, R10), in the style of the <see cref="JobEngine"/> it queues jobs on.
|
||||
/// <see cref="StartAsync"/> seeds the host's system schedules (edits kept), gives enabled schedules without a next run one,
|
||||
/// moves past the runs that schedules without catch-up missed, fires those with catch-up once, then looks every
|
||||
/// <see cref="SchedulerOptions.Tick"/> (or at <see cref="Wake"/>). A due run is claimed by moving its next run with a
|
||||
/// compare-and-set, so several processes on one database fire it once; the claimed run then queues the action's jobs (at the
|
||||
/// schedule's priority, with its id) unless the schedule's previous job is still pending, and records the outcome. A crash
|
||||
/// between the claim and the queueing loses that one run (it is logged as claimed first). A next run more than two periods
|
||||
/// ahead means the clock went back: it is computed again. Edits go through this class so the next run follows them.
|
||||
/// </summary>
|
||||
public sealed class Scheduler : IAsyncDisposable
|
||||
{
|
||||
/// <summary>Longest schedule name.</summary>
|
||||
public const int MaxNameLength = 100;
|
||||
|
||||
/// <summary>The only target so far: the host's archive.</summary>
|
||||
public const string CurrentTarget = "current";
|
||||
|
||||
private readonly ChronosDatabase _db;
|
||||
private readonly JobEngine _engine;
|
||||
private readonly Channel<bool> _wake = Channel.CreateBounded<bool>(new BoundedChannelOptions(1) { FullMode = BoundedChannelFullMode.DropWrite });
|
||||
private readonly CancellationTokenSource _shutdown = new();
|
||||
private readonly Lock _gate = new();
|
||||
private IScheduleHost? _host;
|
||||
private Task _loop = Task.CompletedTask;
|
||||
|
||||
public Scheduler(ChronosDatabase db, JobEngine engine, SchedulerOptions? options = null)
|
||||
{
|
||||
_db = db ?? throw new ArgumentNullException(nameof(db));
|
||||
_engine = engine ?? throw new ArgumentNullException(nameof(engine));
|
||||
Options = options ?? new SchedulerOptions();
|
||||
}
|
||||
|
||||
public SchedulerOptions Options { get; }
|
||||
|
||||
/// <summary>The host given to <see cref="StartAsync"/>; null before.</summary>
|
||||
public IScheduleHost? Host => Volatile.Read(ref _host);
|
||||
|
||||
/// <summary>A schedule was created, edited, reset or ran (also by the loop). Raised on the caller's or the loop's thread.</summary>
|
||||
public event Action<ScheduleDto>? ScheduleChanged;
|
||||
|
||||
/// <summary>A schedule was deleted.</summary>
|
||||
public event Action<Guid>? ScheduleDeleted;
|
||||
|
||||
/// <summary>
|
||||
/// Seeds <paramref name="host"/>'s system schedules, settles the runs missed while the host was off (see the type's
|
||||
/// remarks) and starts the loop. The first pass runs before this returns.
|
||||
/// </summary>
|
||||
public async Task StartAsync(IScheduleHost host, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(host);
|
||||
if (Host is not null) throw new InvalidOperationException("The scheduler has started already.");
|
||||
var added = await _db.Schedules.EnsureSystemAsync(host.SystemSchedules, Now(), cancellationToken).ConfigureAwait(false);
|
||||
if (added > 0) Log.Info("schedules", $"Added {added} system schedule(s)");
|
||||
if (Interlocked.CompareExchange(ref _host, host, null) is not null) throw new InvalidOperationException("The scheduler has started already.");
|
||||
try
|
||||
{
|
||||
await RunDueAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
Log.Warning("schedules", "The schedules could not be run at start; the loop tries again", ex);
|
||||
}
|
||||
var shutdown = _shutdown.Token;
|
||||
lock (_gate) _loop = Task.Run(() => LoopAsync(shutdown), CancellationToken.None);
|
||||
}
|
||||
|
||||
/// <summary>Makes the loop look for due schedules now (another process changed them, the archive came back).</summary>
|
||||
public void Wake() => _wake.Writer.TryWrite(true);
|
||||
|
||||
/// <summary>
|
||||
/// One pass of the loop: corrects next runs (missing, clock gone back, missed without catch-up), then claims and fires
|
||||
/// every due schedule. Returns how many fired here.
|
||||
/// </summary>
|
||||
public async Task<int> RunDueAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var host = RequireHost();
|
||||
var now = Now();
|
||||
foreach (var schedule in await _db.Schedules.ListAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
if (schedule.Enabled) await CorrectAsync(host, schedule, now, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
var fired = 0;
|
||||
foreach (var schedule in await _db.Schedules.ListDueAsync(now, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
DateTimeOffset? next = ScheduleRecurrence.TryParse(schedule.Recurrence, out var recurrence)
|
||||
? recurrence.Next(now, host.TimeZone, schedule.NextRun)
|
||||
: null;
|
||||
// The claim: one process wins the run; the others see the moved next run and leave it.
|
||||
if (!await _db.Schedules.TryAdvanceAsync(schedule.Id, schedule.NextRun, next, cancellationToken).ConfigureAwait(false)) continue;
|
||||
if (next is null)
|
||||
{
|
||||
Log.Warning("schedules", $"Schedule {schedule.Id} has an invalid recurrence '{schedule.Recurrence}'; it stops until edited");
|
||||
await RecordAsync(schedule.Id, null, ScheduleOutcome.Failed("recurrence"), cancellationToken).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
Log.Debug("schedules", $"{schedule.Name} ({schedule.Action}) due {schedule.NextRun:O} claimed; next {next:O}");
|
||||
await FireAsync(host, schedule, schedule.CreatedBy, manual: false, cancellationToken).ConfigureAwait(false);
|
||||
fired++;
|
||||
}
|
||||
return fired;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the schedule now, as its next run would (also a disabled one), without moving its next run; null when it does
|
||||
/// not exist. <paramref name="userId"/> is stamped on the jobs it queues.
|
||||
/// </summary>
|
||||
public async Task<ScheduleRunDto?> RunNowAsync(Guid id, Guid? userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var host = RequireHost();
|
||||
if (await _db.Schedules.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { } schedule) return null;
|
||||
var fire = await FireAsync(host, schedule, userId ?? schedule.CreatedBy, manual: true, cancellationToken).ConfigureAwait(false);
|
||||
var dto = await GetAsync(id, cancellationToken).ConfigureAwait(false) ?? ScheduleMapping.ToDto(schedule);
|
||||
return new ScheduleRunDto(dto, fire.Outcome, fire.JobId);
|
||||
}
|
||||
|
||||
/// <summary>The schedules (system ones first), the actions with their availability here, the host's time zone and clean-up steps.</summary>
|
||||
public async Task<SchedulesDto> ListAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var host = RequireHost();
|
||||
var items = await _db.Schedules.ListAsync(cancellationToken).ConfigureAwait(false);
|
||||
var actions = await ScheduleActions.AvailabilityAsync(Context(host), cancellationToken).ConfigureAwait(false);
|
||||
return new SchedulesDto([.. items.Select(ScheduleMapping.ToDto)], [.. actions.Select(ScheduleMapping.ToDto)], host.TimeZone.Id, host.CleanupSteps);
|
||||
}
|
||||
|
||||
public async Task<ScheduleDto?> GetAsync(Guid id, CancellationToken cancellationToken = default) =>
|
||||
await _db.Schedules.GetAsync(id, cancellationToken).ConfigureAwait(false) is { } schedule ? ScheduleMapping.ToDto(schedule) : null;
|
||||
|
||||
/// <summary>Creates a schedule (enabled ones get their next run at once); the result names the first invalid field instead when there is one.</summary>
|
||||
public async Task<ScheduleEditResult> CreateAsync(CreateScheduleRequest request, Guid? createdBy, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
var host = RequireHost();
|
||||
if (Problem(host, request.Name, request.Action, request.Target, request.Recurrence, request.Parameters, request.Priority) is { } problem)
|
||||
return new ScheduleEditResult(null, problem);
|
||||
var recurrence = ScheduleMapping.FromDto(request.Recurrence)!;
|
||||
var now = Now();
|
||||
var schedule = new Schedule
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Name = request.Name.Trim(),
|
||||
Action = request.Action,
|
||||
Target = CurrentTarget,
|
||||
ParametersJson = ScheduleMapping.ParametersJson(request.Action, request.Parameters),
|
||||
Recurrence = recurrence.Format(),
|
||||
CatchUp = request.CatchUp,
|
||||
Priority = Priority(request.Priority, JobPriority.Background),
|
||||
Enabled = request.Enabled,
|
||||
NextRun = request.Enabled ? recurrence.Next(now, host.TimeZone) : null,
|
||||
CreatedBy = createdBy,
|
||||
Created = now,
|
||||
Updated = now,
|
||||
};
|
||||
await _db.Schedules.InsertAsync(schedule, cancellationToken).ConfigureAwait(false);
|
||||
Log.Info("schedules", $"Schedule {schedule.Name} ({schedule.Action}, {schedule.Recurrence}) created");
|
||||
return new ScheduleEditResult(Changed(schedule), null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the fields <paramref name="request"/> gives. The next run is computed again when the recurrence changes or
|
||||
/// the schedule is enabled; a disabled schedule has none. A system schedule keeps its name and action (asking to change
|
||||
/// them is a problem of field <c>name</c> or <c>action</c>).
|
||||
/// </summary>
|
||||
public async Task<ScheduleEditResult> UpdateAsync(Guid id, UpdateScheduleRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
var host = RequireHost();
|
||||
if (await _db.Schedules.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { } current) return new ScheduleEditResult(null, null);
|
||||
if (current.IsSystem && request.Name is { } name && name.Trim() != current.Name) return new ScheduleEditResult(null, "name");
|
||||
if (current.IsSystem && request.Action is { } changed && changed != current.Action) return new ScheduleEditResult(null, "action");
|
||||
var action = request.Action ?? current.Action;
|
||||
// Another action starts from its own defaults; the same one keeps the stored values the request leaves out.
|
||||
var parameters = request.Parameters ?? (action == current.Action ? ScheduleMapping.Parameters(current) : null);
|
||||
var recurrenceDto = request.Recurrence ?? (ScheduleRecurrence.TryParse(current.Recurrence, out var stored) ? ScheduleMapping.ToDto(stored) : null);
|
||||
if (Problem(host, request.Name ?? current.Name, action, null, recurrenceDto, parameters, request.Priority) is { } problem)
|
||||
return new ScheduleEditResult(null, problem);
|
||||
var recurrence = ScheduleMapping.FromDto(recurrenceDto)!;
|
||||
var now = Now();
|
||||
var enabled = request.Enabled ?? current.Enabled;
|
||||
var reschedule = !enabled || !current.Enabled || current.NextRun is null || recurrence.Format() != current.Recurrence;
|
||||
var schedule = current with
|
||||
{
|
||||
Name = (request.Name ?? current.Name).Trim(),
|
||||
Action = action,
|
||||
ParametersJson = ScheduleMapping.ParametersJson(action, parameters),
|
||||
Recurrence = recurrence.Format(),
|
||||
CatchUp = request.CatchUp ?? current.CatchUp,
|
||||
Priority = Priority(request.Priority, current.Priority),
|
||||
Enabled = enabled,
|
||||
NextRun = !enabled ? null : reschedule ? recurrence.Next(now, host.TimeZone) : current.NextRun,
|
||||
Updated = now,
|
||||
};
|
||||
if (!await _db.Schedules.UpdateAsync(schedule, reschedule, cancellationToken).ConfigureAwait(false)) return new ScheduleEditResult(null, null);
|
||||
Log.Info("schedules", $"Schedule {schedule.Name} ({schedule.Action}, {schedule.Recurrence}, {(enabled ? "enabled" : "disabled")}) changed");
|
||||
var saved = await _db.Schedules.GetAsync(id, cancellationToken).ConfigureAwait(false) ?? schedule;
|
||||
return new ScheduleEditResult(Changed(saved), null);
|
||||
}
|
||||
|
||||
/// <summary>Deletes a schedule a user created; a system schedule is refused (<see cref="ScheduleDeleteOutcome.System"/>).</summary>
|
||||
public async Task<ScheduleDeleteOutcome> DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (await _db.Schedules.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { } schedule) return ScheduleDeleteOutcome.NotFound;
|
||||
if (schedule.IsSystem) return ScheduleDeleteOutcome.System;
|
||||
if (!await _db.Schedules.DeleteAsync(id, cancellationToken).ConfigureAwait(false)) return ScheduleDeleteOutcome.NotFound;
|
||||
Log.Info("schedules", $"Schedule {schedule.Name} ({schedule.Action}) deleted");
|
||||
ScheduleDeleted?.Invoke(id);
|
||||
return ScheduleDeleteOutcome.Deleted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts a system schedule back to the host's defaults (<see cref="IScheduleHost.SystemSchedules"/>) with a new next
|
||||
/// run. A schedule a user created, or one the host no longer defines, is a problem of field <c>systemKey</c>.
|
||||
/// </summary>
|
||||
public async Task<ScheduleEditResult> ResetAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var host = RequireHost();
|
||||
if (await _db.Schedules.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { } current) return new ScheduleEditResult(null, null);
|
||||
if (current.SystemKey is not { } key || host.SystemSchedules.FirstOrDefault(s => s.SystemKey == key) is not { } defaults)
|
||||
return new ScheduleEditResult(null, "systemKey");
|
||||
var now = Now();
|
||||
await _db.Schedules.ResetSystemAsync(key, defaults, now, cancellationToken).ConfigureAwait(false);
|
||||
if (defaults.Enabled) await _db.Schedules.TryAdvanceAsync(id, null, defaults.Recurrence.Next(now, host.TimeZone), cancellationToken).ConfigureAwait(false);
|
||||
Log.Info("schedules", $"System schedule {key} reset");
|
||||
var saved = await _db.Schedules.GetAsync(id, cancellationToken).ConfigureAwait(false);
|
||||
return saved is null ? new ScheduleEditResult(null, null) : new ScheduleEditResult(Changed(saved), null);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
Task loop;
|
||||
lock (_gate) loop = _loop;
|
||||
await _shutdown.CancelAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await loop.ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
_shutdown.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fills in a missing next run, computes it again after the clock went back, and moves a schedule without catch-up past
|
||||
/// a run it missed. Compare-and-set like a claim, so concurrent passes agree.
|
||||
/// </summary>
|
||||
private async Task CorrectAsync(IScheduleHost host, Schedule schedule, DateTimeOffset now, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!ScheduleRecurrence.TryParse(schedule.Recurrence, out var recurrence)) return;
|
||||
DateTimeOffset next;
|
||||
string why;
|
||||
if (schedule.NextRun is not { } due)
|
||||
{
|
||||
next = recurrence.Next(now, host.TimeZone);
|
||||
why = "first run";
|
||||
}
|
||||
else if (due - now > recurrence.Period * 2)
|
||||
{
|
||||
next = recurrence.Next(now, host.TimeZone);
|
||||
why = "the clock went back";
|
||||
}
|
||||
else if (!schedule.CatchUp && now - due > Options.CatchUpGrace)
|
||||
{
|
||||
next = recurrence.Next(now, host.TimeZone, due);
|
||||
why = $"missed the run of {due:O} (no catch-up)";
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!await _db.Schedules.TryAdvanceAsync(schedule.Id, schedule.NextRun, next, cancellationToken).ConfigureAwait(false)) return;
|
||||
Log.Info("schedules", $"{schedule.Name} ({schedule.Action}): next run {next:O} ({why})");
|
||||
Changed(schedule with { NextRun = next });
|
||||
}
|
||||
|
||||
/// <summary>Queues the schedule's action unless its previous job is pending, records the outcome and logs one line.</summary>
|
||||
private async Task<ScheduleFire> FireAsync(IScheduleHost host, Schedule schedule, Guid? createdBy, bool manual, CancellationToken cancellationToken)
|
||||
{
|
||||
string outcome;
|
||||
Guid? jobId = null;
|
||||
try
|
||||
{
|
||||
if (schedule.LastJobId is { } last
|
||||
&& await _db.Jobs.GetAsync(last, cancellationToken).ConfigureAwait(false) is { State: JobState.Queued or JobState.Running or JobState.Paused })
|
||||
{
|
||||
outcome = ScheduleOutcome.SkippedPending;
|
||||
}
|
||||
else
|
||||
{
|
||||
var resolution = await ScheduleActions.ResolveAsync(Context(host), schedule.Action, ScheduleMapping.Parameters(schedule), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
outcome = resolution.Outcome;
|
||||
foreach (var request in resolution.Requests)
|
||||
{
|
||||
var job = await _engine.EnqueueAsync(request.Kind, request.Targets, request.Label, request.ParametersJson, schedule.Priority, createdBy,
|
||||
schedule.Id, cancellationToken).ConfigureAwait(false);
|
||||
jobId ??= job.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning("schedules", $"{schedule.Name} ({schedule.Action}) could not run", ex);
|
||||
outcome = ScheduleOutcome.Failed(ex.Message);
|
||||
}
|
||||
await RecordAsync(schedule.Id, jobId, outcome, CancellationToken.None).ConfigureAwait(false);
|
||||
Log.Info("schedules", $"{schedule.Name} ({schedule.Action}{(manual ? ", run now" : string.Empty)}): {outcome}{(jobId is { } id ? $", job {id}" : string.Empty)}");
|
||||
return new ScheduleFire(schedule.Id, outcome, jobId);
|
||||
}
|
||||
|
||||
private async Task RecordAsync(Guid id, Guid? jobId, string outcome, CancellationToken cancellationToken)
|
||||
{
|
||||
await _db.Schedules.RecordOutcomeAsync(id, jobId, outcome, Now(), cancellationToken).ConfigureAwait(false);
|
||||
if (await _db.Schedules.GetAsync(id, cancellationToken).ConfigureAwait(false) is { } saved) Changed(saved);
|
||||
}
|
||||
|
||||
private async Task LoopAsync(CancellationToken shutdown)
|
||||
{
|
||||
while (!shutdown.IsCancellationRequested)
|
||||
{
|
||||
await WaitAsync(shutdown).ConfigureAwait(false);
|
||||
if (shutdown.IsCancellationRequested) return;
|
||||
try
|
||||
{
|
||||
await RunDueAsync(shutdown).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (shutdown.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Nothing a pass throws ends the loop (a database briefly away, a host hook that failed).
|
||||
Log.Warning("schedules", "The schedules could not be run; the next tick tries again", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Waits for <see cref="Wake"/>, the tick or shutdown, whichever comes first.</summary>
|
||||
private async Task WaitAsync(CancellationToken shutdown)
|
||||
{
|
||||
using var tick = new CancellationTokenSource(Options.Tick, Options.Time);
|
||||
using var either = CancellationTokenSource.CreateLinkedTokenSource(shutdown, tick.Token);
|
||||
try
|
||||
{
|
||||
await _wake.Reader.ReadAsync(either.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Tick elapsed or shutdown.
|
||||
}
|
||||
}
|
||||
|
||||
private ScheduleDto Changed(Schedule schedule)
|
||||
{
|
||||
var dto = ScheduleMapping.ToDto(schedule);
|
||||
ScheduleChanged?.Invoke(dto);
|
||||
return dto;
|
||||
}
|
||||
|
||||
private ScheduleActionContext Context(IScheduleHost host) => new(_db, host, _engine.CanRun, Now());
|
||||
|
||||
private DateTimeOffset Now() => Options.Time.GetUtcNow();
|
||||
|
||||
private IScheduleHost RequireHost() => Host ?? throw new InvalidOperationException("Start the scheduler with its host first.");
|
||||
|
||||
private static JobPriority Priority(string? wire, JobPriority fallback) => wire is not null && JobWire.TryParsePriority(wire, out var priority) ? priority : fallback;
|
||||
|
||||
/// <summary>The first invalid field of a schedule as given, by its API name; null when it is valid.</summary>
|
||||
private static string? Problem(IScheduleHost host, string? name, string? action, string? target, RecurrenceDto? recurrence,
|
||||
ScheduleParametersDto? parameters, string? priority)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name) || name.Trim().Length > MaxNameLength) return "name";
|
||||
if (action is null) return "action";
|
||||
if (ScheduleActions.Problem(action, parameters, host.CleanupSteps) is { } invalid) return invalid;
|
||||
if (target is not null && target != CurrentTarget) return "target";
|
||||
if (ScheduleMapping.FromDto(recurrence) is null) return "recurrence";
|
||||
if (priority is not null && !JobWire.TryParsePriority(priority, out _)) return "priority";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user