| | 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 | | /// The last time this task was run, or null if it has not run yet. |
| | 26 | | /// This is used to determine if the task has run at least once. |
| | 27 | | /// </summary> |
| 60 | 28 | | public DateTimeOffset? LastRunAt { get; set; } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// The next time this task is scheduled to run, based on the cron expression or interval. |
| | 32 | | /// If the task is not scheduled, this will be DateTimeOffset.MinValue. |
| | 33 | | /// </summary> |
| 119 | 34 | | public DateTimeOffset NextRunAt { get; set; } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Indicates whether the task is currently suspended. |
| | 38 | | /// A suspended task will not run until resumed. |
| | 39 | | /// </summary> |
| | 40 | | public volatile bool IsSuspended; |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// The background runner task handling the scheduling loop. Used to allow |
| | 44 | | /// graceful cancellation (tests assert no further executions after Cancel()). |
| | 45 | | /// </summary> |
| 48 | 46 | | public Task? Runner { get; set; } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Fixed anchor timestamp captured at schedule time for interval jobs to enable fixed-rate scheduling. |
| | 50 | | /// </summary> |
| 48 | 51 | | public DateTimeOffset AnchorAt { get; init; } = DateTimeOffset.UtcNow; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Number of successful executions completed (for interval jobs) to compute deterministic next slot. |
| | 55 | | /// </summary> |
| 77 | 56 | | public int RunIteration { get; set; } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// True when the scheduling loop has exited. |
| | 60 | | /// </summary> |
| | 61 | | public volatile bool IsCompleted; |
| | 62 | | } |