| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Common.RecurringTasks; |
| | | 3 | | using Elsa.Workflows.Management; |
| | | 4 | | using Elsa.Workflows.Management.Filters; |
| | | 5 | | using Elsa.Workflows.Runtime.Options; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using Microsoft.Extensions.Options; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Runtime.Tasks; |
| | | 11 | | |
| | | 12 | | [SingleNodeTask] |
| | | 13 | | [UsedImplicitly] |
| | 5 | 14 | | public class RestartInterruptedWorkflowsTask( |
| | 5 | 15 | | IWorkflowInstanceStore workflowInstanceStore, |
| | 5 | 16 | | IWorkflowRestarter workflowRestarter, |
| | 5 | 17 | | IOptions<RuntimeOptions> options, |
| | 5 | 18 | | ISystemClock systemClock, |
| | 5 | 19 | | ILogger<RestartInterruptedWorkflowsTask> logger) : RecurringTask |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public override async Task ExecuteAsync(CancellationToken cancellationToken) |
| | | 23 | | { |
| | 2 | 24 | | var workflowInstanceFilter = CreateWorkflowInstanceFilter(); |
| | 2 | 25 | | var batchSize = options.Value.RestartInterruptedWorkflowsBatchSize; |
| | 2 | 26 | | var workflowInstances = workflowInstanceStore.EnumerateSummariesAsync(workflowInstanceFilter, batchSize, cancell |
| | | 27 | | |
| | 2 | 28 | | logger.LogInformation("Restarting interrupted workflows."); |
| | 4 | 29 | | await foreach (var workflowInstance in workflowInstances) |
| | | 30 | | { |
| | 0 | 31 | | await workflowRestarter.RestartWorkflowAsync(workflowInstance.Id, cancellationToken: cancellationToken); |
| | | 32 | | } |
| | 2 | 33 | | logger.LogInformation("Finished restarting interrupted workflows."); |
| | 2 | 34 | | } |
| | | 35 | | |
| | | 36 | | private WorkflowInstanceFilter CreateWorkflowInstanceFilter() |
| | | 37 | | { |
| | 2 | 38 | | var livenessThreshold = options.Value.InactivityThreshold; |
| | 2 | 39 | | var now = systemClock.UtcNow; |
| | 2 | 40 | | var cutoffTimestamp = now - livenessThreshold; |
| | 2 | 41 | | return new() |
| | 2 | 42 | | { |
| | 2 | 43 | | IsExecuting = true, |
| | 2 | 44 | | BeforeLastUpdated = cutoffTimestamp |
| | 2 | 45 | | }; |
| | | 46 | | } |
| | | 47 | | } |