< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Scheduling.ScheduledTask
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Scheduling/ScheduledTask.cs
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
91%
Covered lines: 22
Uncovered lines: 2
Coverable lines: 24
Total lines: 109
Line coverage: 91.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 01:25:22 Line coverage: 100% (13/13) Total lines: 62 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a10/13/2025 - 16:52:37 Line coverage: 91.6% (22/24) Total lines: 109 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%11100%
get_Work()100%11100%
get_Cron()100%11100%
get_Interval()100%11100%
get_RunImmediately()100%11100%
get_TokenSource()100%11100%
get_LastRunAt()100%210%
set_LastRunAt(...)100%210%
get_NextRunAt()100%11100%
set_NextRunAt(...)100%11100%
GetTimestamps()100%11100%
SetTimestamps(...)100%11100%
get_Runner()100%11100%
get_AnchorAt()100%11100%
get_RunIteration()100%11100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Scheduling/ScheduledTask.cs

#LineLine coverage
 1using Cronos;
 2
 3namespace 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>
 2515internal sealed record ScheduledTask(
 4516    string Name,
 2117    Func<CancellationToken, Task> Work,
 1918    CronExpression? Cron,
 7019    TimeSpan? Interval,
 2420    bool RunImmediately,
 4821    CancellationTokenSource TokenSource
 2522)
 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
 2529    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    {
 043        get { lock (_timestampLock) { return _lastRunAt; } }
 044        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    {
 8153        get { lock (_timestampLock) { return _nextRunAt; } }
 10054        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()
 3962    {
 63        lock (_timestampLock)
 64        {
 3965            return (_lastRunAt, _nextRunAt);
 66        }
 3967    }
 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)
 2175    {
 76        lock (_timestampLock)
 77        {
 2178            _lastRunAt = lastRunAt;
 2179            _nextRunAt = nextRunAt;
 2180        }
 2181    }
 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>
 4893    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>
 4898    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>
 77103    public int RunIteration { get; set; }
 104
 105    /// <summary>
 106    /// True when the scheduling loop has exited.
 107    /// </summary>
 108    public volatile bool IsCompleted;
 109}