| | | 1 | | using Elsa.Alterations.AlterationTypes; |
| | | 2 | | using Elsa.Alterations.Core.Abstractions; |
| | | 3 | | using Elsa.Alterations.Core.Contexts; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Alterations.AlterationHandlers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Cancels an activity. |
| | | 12 | | /// </summary> |
| | | 13 | | [UsedImplicitly] |
| | | 14 | | public class CancelActivityHandler : AlterationHandlerBase<CancelActivity> |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | protected override ValueTask HandleAsync(AlterationContext context, CancelActivity alteration) |
| | | 18 | | { |
| | 0 | 19 | | if (alteration.ActivityInstanceId == null && alteration.ActivityId == null) |
| | | 20 | | { |
| | 0 | 21 | | context.Fail("Either ActivityInstanceId or ActivityId must be specified"); |
| | 0 | 22 | | return ValueTask.CompletedTask; |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | var activityExecutionContexts = GetActivityExecutionContexts(context, alteration).ToList(); |
| | | 26 | | |
| | 0 | 27 | | if (!activityExecutionContexts.Any()) |
| | | 28 | | { |
| | 0 | 29 | | context.Fail( |
| | 0 | 30 | | alteration.ActivityInstanceId != null |
| | 0 | 31 | | ? $"Activity execution context with ID {alteration.ActivityInstanceId} not found" |
| | 0 | 32 | | : $"Activity execution contexts for activity with ID {alteration.ActivityId} not found"); |
| | | 33 | | |
| | 0 | 34 | | return ValueTask.CompletedTask; |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | context.Succeed(() => CancelAsync(activityExecutionContexts)); |
| | 0 | 38 | | return ValueTask.CompletedTask; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private async Task CancelAsync(IEnumerable<ActivityExecutionContext> activityExecutionContexts) |
| | | 42 | | { |
| | 0 | 43 | | foreach (var activityExecutionContext in activityExecutionContexts) |
| | 0 | 44 | | await CancelAsync(activityExecutionContext); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task CancelAsync(ActivityExecutionContext activityExecutionContext) |
| | | 48 | | { |
| | 0 | 49 | | await activityExecutionContext.CancelActivityAsync(); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static IEnumerable<ActivityExecutionContext> GetActivityExecutionContexts(AlterationContext context, CancelA |
| | | 53 | | { |
| | 0 | 54 | | var workflowExecutionContext = context.WorkflowExecutionContext; |
| | | 55 | | |
| | 0 | 56 | | return alteration.ActivityInstanceId != null |
| | 0 | 57 | | ? workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Id == alteration.ActivityInstanceId) |
| | 0 | 58 | | : alteration.ActivityId != null |
| | 0 | 59 | | ? workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Activity.Id == alteration.ActivityId) |
| | 0 | 60 | | : Enumerable.Empty<ActivityExecutionContext>(); |
| | | 61 | | } |
| | | 62 | | } |