| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Alterations.AlterationTypes; |
| | | 3 | | using Elsa.Alterations.Core.Contracts; |
| | | 4 | | using Elsa.Alterations.Core.Results; |
| | | 5 | | using Elsa.Workflows.Management; |
| | | 6 | | using Elsa.Workflows.Management.Entities; |
| | | 7 | | using Elsa.Workflows.Management.Filters; |
| | | 8 | | using Elsa.Workflows.Runtime; |
| | | 9 | | using Elsa.Workflows.Runtime.Contracts; |
| | | 10 | | using Elsa.Workflows.Runtime.Requests; |
| | | 11 | | using JetBrains.Annotations; |
| | | 12 | | |
| | | 13 | | namespace Elsa.Alterations.Endpoints.Workflows.Retry; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Retries the specified workflow instances. |
| | | 17 | | /// </summary> |
| | | 18 | | [PublicAPI] |
| | | 19 | | public class Retry : ElsaEndpoint<Request, Response> |
| | | 20 | | { |
| | | 21 | | private readonly IAlterationRunner _alterationRunner; |
| | | 22 | | private readonly IWorkflowDispatcher _workflowDispatcher; |
| | | 23 | | private readonly IWorkflowInstanceStore _workflowInstanceStore; |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | 1 | 26 | | public Retry(IAlterationRunner alterationRunner, IWorkflowDispatcher workflowDispatcher, IWorkflowInstanceStore work |
| | | 27 | | { |
| | 1 | 28 | | _alterationRunner = alterationRunner; |
| | 1 | 29 | | _workflowDispatcher = workflowDispatcher; |
| | 1 | 30 | | _workflowInstanceStore = workflowInstanceStore; |
| | 1 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public override void Configure() |
| | | 35 | | { |
| | 1 | 36 | | Routes("/alterations/workflows/retry"); |
| | 1 | 37 | | Verbs(FastEndpoints.Http.GET, FastEndpoints.Http.POST); |
| | 1 | 38 | | ConfigurePermissions("run:alterations"); |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 43 | | { |
| | 0 | 44 | | var allResults = new List<RunAlterationsResult>(); |
| | | 45 | | |
| | | 46 | | // Load each workflow instance. |
| | 0 | 47 | | var workflowInstances = (await _workflowInstanceStore.FindManyAsync(new WorkflowInstanceFilter { Ids = request.W |
| | | 48 | | |
| | 0 | 49 | | foreach (var workflowInstance in workflowInstances) |
| | | 50 | | { |
| | | 51 | | // Setup an alteration plan. |
| | 0 | 52 | | var activityIds = GetActivityIds(request, workflowInstance).ToList(); |
| | 0 | 53 | | var alterations = activityIds.Select(activityId => new ScheduleActivity { ActivityId = activityId }).Cast<IA |
| | | 54 | | |
| | | 55 | | // Run the plan. |
| | 0 | 56 | | var results = await _alterationRunner.RunAsync(request.WorkflowInstanceIds, alterations, cancellationToken); |
| | 0 | 57 | | allResults.AddRange(results); |
| | | 58 | | |
| | | 59 | | // Schedule updated workflow. |
| | 0 | 60 | | await _workflowDispatcher.DispatchAsync(new DispatchWorkflowInstanceRequest(workflowInstance.Id), cancellati |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | // Write response. |
| | 0 | 64 | | var response = new Response(allResults); |
| | 0 | 65 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | private IEnumerable<string> GetActivityIds(Request request, WorkflowInstance workflowInstance) |
| | | 69 | | { |
| | | 70 | | // If activity IDs are explicitly specified, use them. |
| | 0 | 71 | | if (request.ActivityIds?.Any() == true) |
| | 0 | 72 | | return request.ActivityIds; |
| | | 73 | | |
| | | 74 | | // Otherwise, select IDs of all faulted activities. |
| | 0 | 75 | | return workflowInstance.WorkflowState.Incidents.Select(x => x.ActivityId).Distinct().ToList(); |
| | | 76 | | } |
| | | 77 | | } |