| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Scheduling.Commands; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using Timer = System.Timers.Timer; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Scheduling.ScheduledTasks; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// A task that is scheduled to execute at a specific instant. |
| | | 12 | | /// </summary> |
| | | 13 | | public 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; |
| | 21 | 21 | | 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> |
| | 21 | 30 | | public ScheduledSpecificInstantTask(ITask task, DateTimeOffset startAt, ISystemClock systemClock, IServiceScopeFacto |
| | | 31 | | { |
| | 21 | 32 | | _task = task; |
| | 21 | 33 | | _systemClock = systemClock; |
| | 21 | 34 | | _scopeFactory = scopeFactory; |
| | 21 | 35 | | _logger = logger; |
| | 21 | 36 | | _startAt = startAt; |
| | 21 | 37 | | _cancellationTokenSource = new(); |
| | | 38 | | |
| | 21 | 39 | | Schedule(); |
| | 21 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public void Cancel() |
| | | 44 | | { |
| | 17 | 45 | | _timer?.Dispose(); |
| | | 46 | | |
| | 17 | 47 | | if (_executing) |
| | | 48 | | { |
| | 10 | 49 | | _cancellationRequested = true; |
| | 10 | 50 | | return; |
| | | 51 | | } |
| | | 52 | | |
| | 7 | 53 | | _cancellationTokenSource.Cancel(); |
| | 7 | 54 | | } |
| | | 55 | | |
| | | 56 | | private void Schedule() |
| | | 57 | | { |
| | 21 | 58 | | var now = _systemClock.UtcNow; |
| | 21 | 59 | | 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 |
| | 21 | 63 | | if (delay <= TimeSpan.Zero) |
| | | 64 | | { |
| | 6 | 65 | | _logger.LogWarning("Calculated delay is {Delay} which is not positive. Using minimum delay of 1ms to ensure |
| | 6 | 66 | | delay = TimeSpan.FromMilliseconds(1); |
| | | 67 | | } |
| | | 68 | | |
| | 21 | 69 | | _timer = new(delay.TotalMilliseconds) |
| | 21 | 70 | | { |
| | 21 | 71 | | Enabled = true |
| | 21 | 72 | | }; |
| | | 73 | | |
| | 21 | 74 | | _timer.Elapsed += async (_, _) => |
| | 21 | 75 | | { |
| | 10 | 76 | | _timer?.Dispose(); |
| | 10 | 77 | | _timer = null; |
| | 21 | 78 | | |
| | 21 | 79 | | // Check if disposed before proceeding |
| | 10 | 80 | | if (_disposed) return; |
| | 21 | 81 | | |
| | 10 | 82 | | using var scope = _scopeFactory.CreateScope(); |
| | 10 | 83 | | var commandSender = scope.ServiceProvider.GetRequiredService<ICommandSender>(); |
| | 21 | 84 | | |
| | 21 | 85 | | // Check disposed again before accessing CancellationTokenSource |
| | 10 | 86 | | if (_disposed) return; |
| | 21 | 87 | | |
| | 10 | 88 | | var cancellationToken = _cancellationTokenSource.Token; |
| | 10 | 89 | | if (!cancellationToken.IsCancellationRequested) |
| | 21 | 90 | | { |
| | 10 | 91 | | var acquired = false; |
| | 21 | 92 | | try |
| | 21 | 93 | | { |
| | 10 | 94 | | acquired = await _executionSemaphore.WaitAsync(0, cancellationToken); |
| | 10 | 95 | | if (!acquired) return; |
| | 10 | 96 | | _executing = true; |
| | 10 | 97 | | await commandSender.SendAsync(new RunScheduledTask(_task), cancellationToken); |
| | 21 | 98 | | |
| | 10 | 99 | | if (_cancellationRequested) |
| | 21 | 100 | | { |
| | 10 | 101 | | _cancellationRequested = false; |
| | 10 | 102 | | _cancellationTokenSource.Cancel(); |
| | 21 | 103 | | } |
| | 10 | 104 | | } |
| | 0 | 105 | | catch (Exception e) |
| | 21 | 106 | | { |
| | 0 | 107 | | _logger.LogError(e, "Error executing scheduled task"); |
| | 0 | 108 | | } |
| | 21 | 109 | | finally |
| | 21 | 110 | | { |
| | 10 | 111 | | _executing = false; |
| | 10 | 112 | | if (acquired && !_disposed) |
| | 10 | 113 | | _executionSemaphore.Release(); |
| | 21 | 114 | | } |
| | 21 | 115 | | } |
| | 31 | 116 | | }; |
| | 21 | 117 | | } |
| | | 118 | | |
| | | 119 | | void IDisposable.Dispose() |
| | | 120 | | { |
| | 5 | 121 | | _disposed = true; |
| | 5 | 122 | | _timer?.Dispose(); |
| | 5 | 123 | | _cancellationTokenSource.Dispose(); |
| | 5 | 124 | | _executionSemaphore.Dispose(); |
| | 5 | 125 | | } |
| | | 126 | | } |