< Summary

Information
Class: Elsa.Scheduling.ScheduledTasks.ScheduledSpecificInstantTask
Assembly: Elsa.Scheduling
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Scheduling/ScheduledTasks/ScheduledSpecificInstantTask.cs
Line coverage
95%
Covered lines: 71
Uncovered lines: 3
Coverable lines: 74
Total lines: 126
Line coverage: 95.9%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Cancel()100%44100%
Schedule()100%2290.56%
System.IDisposable.Dispose()100%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Scheduling/ScheduledTasks/ScheduledSpecificInstantTask.cs

#LineLine coverage
 1using Elsa.Common;
 2using Elsa.Mediator.Contracts;
 3using Elsa.Scheduling.Commands;
 4using Microsoft.Extensions.DependencyInjection;
 5using Microsoft.Extensions.Logging;
 6using Timer = System.Timers.Timer;
 7
 8namespace Elsa.Scheduling.ScheduledTasks;
 9
 10/// <summary>
 11/// A task that is scheduled to execute at a specific instant.
 12/// </summary>
 13public class ScheduledSpecificInstantTask : IScheduledTask, IDisposable
 14{
 15    private readonly ITask _task;
 16    private readonly ISystemClock _systemClock;
 17    private readonly IServiceScopeFactory _scopeFactory;
 18    private readonly ILogger<ScheduledSpecificInstantTask> _logger;
 19    private readonly DateTimeOffset _startAt;
 20    private readonly CancellationTokenSource _cancellationTokenSource;
 1721    private readonly SemaphoreSlim _executionSemaphore = new(1, 1);
 22    private Timer? _timer;
 23    private bool _executing;
 24    private bool _cancellationRequested;
 25    private bool _disposed;
 26
 27    /// <summary>
 28    /// Initializes a new instance of <see cref="ScheduledSpecificInstantTask"/>.
 29    /// </summary>
 1730    public ScheduledSpecificInstantTask(ITask task, DateTimeOffset startAt, ISystemClock systemClock, IServiceScopeFacto
 31    {
 1732        _task = task;
 1733        _systemClock = systemClock;
 1734        _scopeFactory = scopeFactory;
 1735        _logger = logger;
 1736        _startAt = startAt;
 1737        _cancellationTokenSource = new();
 38
 1739        Schedule();
 1740    }
 41
 42    /// <inheritdoc />
 43    public void Cancel()
 44    {
 1345        _timer?.Dispose();
 46
 1347        if (_executing)
 48        {
 649            _cancellationRequested = true;
 650            return;
 51        }
 52
 753        _cancellationTokenSource.Cancel();
 754    }
 55
 56    private void Schedule()
 57    {
 1758        var now = _systemClock.UtcNow;
 1759        var delay = _startAt - now;
 60
 61        // Handle edge cases where delay is zero or negative (e.g., due to clock drift, fast execution, or time alignmen
 62        // Instead of silently returning, use a minimum delay to ensure the timer fires and workflow continues schedulin
 1763        if (delay <= TimeSpan.Zero)
 64        {
 365            _logger.LogWarning("Calculated delay is {Delay} which is not positive. Using minimum delay of 1ms to ensure 
 366            delay = TimeSpan.FromMilliseconds(1);
 67        }
 68
 1769        _timer = new(delay.TotalMilliseconds)
 1770        {
 1771            Enabled = true
 1772        };
 73
 1774        _timer.Elapsed += async (_, _) =>
 1775        {
 876            _timer?.Dispose();
 877            _timer = null;
 1778
 1779            // Check if disposed before proceeding
 980            if (_disposed) return;
 1781
 782            using var scope = _scopeFactory.CreateScope();
 783            var commandSender = scope.ServiceProvider.GetRequiredService<ICommandSender>();
 1784
 1785            // Check disposed again before accessing CancellationTokenSource
 786            if (_disposed) return;
 1787
 788            var cancellationToken = _cancellationTokenSource.Token;
 789            if (!cancellationToken.IsCancellationRequested)
 1790            {
 791                var acquired = false;
 1792                try
 1793                {
 794                    acquired = await _executionSemaphore.WaitAsync(0, cancellationToken);
 795                    if (!acquired) return;
 796                    _executing = true;
 797                    await commandSender.SendAsync(new RunScheduledTask(_task), cancellationToken);
 1798
 799                    if (_cancellationRequested)
 17100                    {
 6101                        _cancellationRequested = false;
 6102                        _cancellationTokenSource.Cancel();
 17103                    }
 7104                }
 0105                catch (Exception e)
 17106                {
 0107                    _logger.LogError(e, "Error executing scheduled task");
 0108                }
 17109                finally
 17110                {
 7111                    _executing = false;
 7112                    if (acquired && !_disposed)
 7113                        _executionSemaphore.Release();
 17114                }
 17115            }
 25116        };
 17117    }
 118
 119    void IDisposable.Dispose()
 120    {
 5121        _disposed = true;
 5122        _timer?.Dispose();
 5123        _cancellationTokenSource.Dispose();
 5124        _executionSemaphore.Dispose();
 5125    }
 126}