| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Scheduling.Commands; |
| | | 4 | | using Elsa.Scheduling.Options; |
| | | 5 | | using Elsa.Scheduling.Services; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using Timer = System.Timers.Timer; |
| | | 9 | | using OptionsFactory = Microsoft.Extensions.Options.Options; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Scheduling.ScheduledTasks; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// A task that is scheduled to execute at a specific instant. |
| | | 15 | | /// </summary> |
| | | 16 | | public class ScheduledSpecificInstantTask : IScheduledTask, IDisposable |
| | | 17 | | { |
| | 0 | 18 | | private static readonly PastDueScheduleStaggerer DefaultPastDueScheduleStaggerer = new(OptionsFactory.Create(new Sch |
| | | 19 | | private readonly ITask _task; |
| | | 20 | | private readonly ISystemClock _systemClock; |
| | | 21 | | private readonly IServiceScopeFactory _scopeFactory; |
| | | 22 | | private readonly ILogger<ScheduledSpecificInstantTask> _logger; |
| | | 23 | | private readonly PastDueScheduleStaggerer _pastDueScheduleStaggerer; |
| | | 24 | | private readonly DateTimeOffset _startAt; |
| | | 25 | | private readonly CancellationTokenSource _cancellationTokenSource; |
| | 20 | 26 | | private readonly SemaphoreSlim _executionSemaphore = new(1, 1); |
| | | 27 | | private Timer? _timer; |
| | | 28 | | private bool _executing; |
| | | 29 | | private bool _cancellationRequested; |
| | | 30 | | private bool _disposed; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of <see cref="ScheduledSpecificInstantTask"/>. |
| | | 34 | | /// </summary> |
| | | 35 | | public ScheduledSpecificInstantTask( |
| | | 36 | | ITask task, |
| | | 37 | | DateTimeOffset startAt, |
| | | 38 | | ISystemClock systemClock, |
| | | 39 | | IServiceScopeFactory scopeFactory, |
| | | 40 | | ILogger<ScheduledSpecificInstantTask> logger) |
| | 0 | 41 | | : this(task, startAt, systemClock, scopeFactory, logger, DefaultPastDueScheduleStaggerer) |
| | | 42 | | { |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Initializes a new instance of <see cref="ScheduledSpecificInstantTask"/>. |
| | | 47 | | /// </summary> |
| | | 48 | | [ActivatorUtilitiesConstructor] |
| | 20 | 49 | | public ScheduledSpecificInstantTask( |
| | 20 | 50 | | ITask task, |
| | 20 | 51 | | DateTimeOffset startAt, |
| | 20 | 52 | | ISystemClock systemClock, |
| | 20 | 53 | | IServiceScopeFactory scopeFactory, |
| | 20 | 54 | | ILogger<ScheduledSpecificInstantTask> logger, |
| | 20 | 55 | | PastDueScheduleStaggerer pastDueScheduleStaggerer) |
| | | 56 | | { |
| | 20 | 57 | | _task = task; |
| | 20 | 58 | | _systemClock = systemClock; |
| | 20 | 59 | | _scopeFactory = scopeFactory; |
| | 20 | 60 | | _logger = logger; |
| | 20 | 61 | | _pastDueScheduleStaggerer = pastDueScheduleStaggerer; |
| | 20 | 62 | | _startAt = startAt; |
| | 20 | 63 | | _cancellationTokenSource = new(); |
| | | 64 | | |
| | 20 | 65 | | Schedule(); |
| | 20 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public void Cancel() |
| | | 70 | | { |
| | 13 | 71 | | _timer?.Dispose(); |
| | | 72 | | |
| | 13 | 73 | | if (_executing) |
| | | 74 | | { |
| | 6 | 75 | | _cancellationRequested = true; |
| | 6 | 76 | | return; |
| | | 77 | | } |
| | | 78 | | |
| | 7 | 79 | | _cancellationTokenSource.Cancel(); |
| | 7 | 80 | | } |
| | | 81 | | |
| | | 82 | | private void Schedule() |
| | | 83 | | { |
| | 20 | 84 | | var now = _systemClock.UtcNow; |
| | 20 | 85 | | var delay = _startAt - now; |
| | 20 | 86 | | var adjustedDelay = _pastDueScheduleStaggerer.GetDelay(delay); |
| | | 87 | | |
| | 20 | 88 | | if (delay <= TimeSpan.Zero) |
| | | 89 | | { |
| | 6 | 90 | | _logger.LogDebug("Calculated delay is {Delay} which is not positive. Using catch-up delay of {CatchUpDelay}" |
| | | 91 | | } |
| | | 92 | | |
| | 20 | 93 | | _timer = new(adjustedDelay.TotalMilliseconds) |
| | 20 | 94 | | { |
| | 20 | 95 | | Enabled = true |
| | 20 | 96 | | }; |
| | | 97 | | |
| | 20 | 98 | | _timer.Elapsed += async (_, _) => |
| | 20 | 99 | | { |
| | 6 | 100 | | _timer?.Dispose(); |
| | 6 | 101 | | _timer = null; |
| | 20 | 102 | | |
| | 20 | 103 | | // Check if disposed before proceeding |
| | 6 | 104 | | if (_disposed) return; |
| | 20 | 105 | | |
| | 6 | 106 | | using var scope = _scopeFactory.CreateScope(); |
| | 6 | 107 | | var commandSender = scope.ServiceProvider.GetRequiredService<ICommandSender>(); |
| | 20 | 108 | | |
| | 20 | 109 | | // Check disposed again before accessing CancellationTokenSource |
| | 6 | 110 | | if (_disposed) return; |
| | 20 | 111 | | |
| | 6 | 112 | | var cancellationToken = _cancellationTokenSource.Token; |
| | 6 | 113 | | if (!cancellationToken.IsCancellationRequested) |
| | 20 | 114 | | { |
| | 6 | 115 | | var acquired = false; |
| | 20 | 116 | | try |
| | 20 | 117 | | { |
| | 6 | 118 | | acquired = await _executionSemaphore.WaitAsync(0, cancellationToken); |
| | 6 | 119 | | if (!acquired) return; |
| | 6 | 120 | | _executing = true; |
| | 6 | 121 | | await commandSender.SendAsync(new RunScheduledTask(_task), cancellationToken); |
| | 20 | 122 | | |
| | 6 | 123 | | if (_cancellationRequested) |
| | 20 | 124 | | { |
| | 6 | 125 | | _cancellationRequested = false; |
| | 6 | 126 | | _cancellationTokenSource.Cancel(); |
| | 20 | 127 | | } |
| | 6 | 128 | | } |
| | 0 | 129 | | catch (Exception e) |
| | 20 | 130 | | { |
| | 0 | 131 | | _logger.LogError(e, "Error executing scheduled task"); |
| | 0 | 132 | | } |
| | 20 | 133 | | finally |
| | 20 | 134 | | { |
| | 6 | 135 | | _executing = false; |
| | 6 | 136 | | if (acquired && !_disposed) |
| | 6 | 137 | | _executionSemaphore.Release(); |
| | 20 | 138 | | } |
| | 20 | 139 | | } |
| | 26 | 140 | | }; |
| | 20 | 141 | | } |
| | | 142 | | |
| | | 143 | | void IDisposable.Dispose() |
| | | 144 | | { |
| | 8 | 145 | | _disposed = true; |
| | 8 | 146 | | _timer?.Dispose(); |
| | 8 | 147 | | _cancellationTokenSource.Dispose(); |
| | 8 | 148 | | _executionSemaphore.Dispose(); |
| | 8 | 149 | | } |
| | | 150 | | } |