| | | 1 | | using Cronos; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Scheduling; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a scheduled task with its configuration and state. |
| | | 7 | | /// This record is used to encapsulate the details of a scheduled task, |
| | | 8 | | /// </summary> |
| | | 9 | | /// <param name="Name">The name of the task.</param> |
| | | 10 | | /// <param name="Work">The work to be performed by the task.</param> |
| | | 11 | | /// <param name="Cron">The cron expression for the task.</param> |
| | | 12 | | /// <param name="Interval">The interval for the task.</param> |
| | | 13 | | /// <param name="RunImmediately">Whether to run the task immediately.</param> |
| | | 14 | | /// <param name="TokenSource">The cancellation token source for the task.</param> |
| | 25 | 15 | | internal sealed record ScheduledTask( |
| | 45 | 16 | | string Name, |
| | 21 | 17 | | Func<CancellationToken, Task> Work, |
| | 19 | 18 | | CronExpression? Cron, |
| | 70 | 19 | | TimeSpan? Interval, |
| | 24 | 20 | | bool RunImmediately, |
| | 48 | 21 | | CancellationTokenSource TokenSource |
| | 25 | 22 | | ) |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Lock object for synchronizing timestamp updates to prevent reading inconsistent state |
| | | 26 | | /// where LastRunAt is newer than NextRunAt. |
| | | 27 | | /// </summary> |
| | | 28 | | #if NET9_0_OR_GREATER |
| | 25 | 29 | | private readonly Lock _timestampLock = new(); |
| | | 30 | | #else |
| | | 31 | | private readonly object _timestampLock = new(); |
| | | 32 | | #endif |
| | | 33 | | |
| | | 34 | | private DateTimeOffset? _lastRunAt; |
| | | 35 | | private DateTimeOffset _nextRunAt; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// The last time this task was run, or null if it has not run yet. |
| | | 39 | | /// This is used to determine if the task has run at least once. |
| | | 40 | | /// </summary> |
| | | 41 | | public DateTimeOffset? LastRunAt |
| | | 42 | | { |
| | 0 | 43 | | get { lock (_timestampLock) { return _lastRunAt; } } |
| | 0 | 44 | | set { lock (_timestampLock) { _lastRunAt = value; } } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// The next time this task is scheduled to run, based on the cron expression or interval. |
| | | 49 | | /// If the task is not scheduled, this will be DateTimeOffset.MinValue. |
| | | 50 | | /// </summary> |
| | | 51 | | public DateTimeOffset NextRunAt |
| | | 52 | | { |
| | 81 | 53 | | get { lock (_timestampLock) { return _nextRunAt; } } |
| | 100 | 54 | | set { lock (_timestampLock) { _nextRunAt = value; } } |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets both timestamps atomically to prevent reading inconsistent state. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <returns>A tuple containing LastRunAt and NextRunAt.</returns> |
| | | 61 | | public (DateTimeOffset? LastRunAt, DateTimeOffset NextRunAt) GetTimestamps() |
| | 39 | 62 | | { |
| | | 63 | | lock (_timestampLock) |
| | | 64 | | { |
| | 39 | 65 | | return (_lastRunAt, _nextRunAt); |
| | | 66 | | } |
| | 39 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Sets both timestamps atomically to ensure consistent state. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="lastRunAt">The last run timestamp.</param> |
| | | 73 | | /// <param name="nextRunAt">The next run timestamp.</param> |
| | | 74 | | public void SetTimestamps(DateTimeOffset lastRunAt, DateTimeOffset nextRunAt) |
| | 21 | 75 | | { |
| | | 76 | | lock (_timestampLock) |
| | | 77 | | { |
| | 21 | 78 | | _lastRunAt = lastRunAt; |
| | 21 | 79 | | _nextRunAt = nextRunAt; |
| | 21 | 80 | | } |
| | 21 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Indicates whether the task is currently suspended. |
| | | 85 | | /// A suspended task will not run until resumed. |
| | | 86 | | /// </summary> |
| | | 87 | | public volatile bool IsSuspended; |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// The background runner task handling the scheduling loop. Used to allow |
| | | 91 | | /// graceful cancellation (tests assert no further executions after Cancel()). |
| | | 92 | | /// </summary> |
| | 48 | 93 | | public Task? Runner { get; set; } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Fixed anchor timestamp captured at schedule time for interval jobs to enable fixed-rate scheduling. |
| | | 97 | | /// </summary> |
| | 48 | 98 | | public DateTimeOffset AnchorAt { get; init; } = DateTimeOffset.UtcNow; |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Number of successful executions completed (for interval jobs) to compute deterministic next slot. |
| | | 102 | | /// </summary> |
| | 77 | 103 | | public int RunIteration { get; set; } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// True when the scheduling loop has exited. |
| | | 107 | | /// </summary> |
| | | 108 | | public volatile bool IsCompleted; |
| | | 109 | | } |