| | | 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 Elsa.Workflows.Signals; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Alterations.AlterationHandlers; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Schedules an activity for execution. |
| | | 13 | | /// </summary> |
| | | 14 | | [UsedImplicitly] |
| | | 15 | | public class ScheduleActivityHandler : AlterationHandlerBase<ScheduleActivity> |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | protected override async ValueTask HandleAsync(AlterationContext context, ScheduleActivity alteration) |
| | | 19 | | { |
| | 0 | 20 | | if (alteration.ActivityInstanceId == null && alteration.ActivityId == null) |
| | | 21 | | { |
| | 0 | 22 | | context.Fail("Either ActivityInstanceId or ActivityId must be specified"); |
| | 0 | 23 | | return; |
| | | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | var workflowExecutionContext = context.WorkflowExecutionContext; |
| | 0 | 27 | | var existingActivityExecutionContext = GetActivityExecutionContext(context, alteration); |
| | | 28 | | |
| | 0 | 29 | | if (existingActivityExecutionContext != null) |
| | | 30 | | { |
| | | 31 | | // If the activity is in a faulted state, reset it to Running. |
| | 0 | 32 | | if (existingActivityExecutionContext.Status == ActivityStatus.Faulted) |
| | 0 | 33 | | existingActivityExecutionContext.TransitionTo(ActivityStatus.Running); |
| | | 34 | | |
| | | 35 | | // Schedule the activity execution context. |
| | 0 | 36 | | var parentContext = existingActivityExecutionContext.ParentActivityExecutionContext; |
| | 0 | 37 | | await parentContext!.SendSignalAsync(new ScheduleChildActivity(existingActivityExecutionContext)); |
| | 0 | 38 | | context.Succeed(); |
| | 0 | 39 | | return; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | // Schedule a new activity instance. |
| | 0 | 43 | | var activityId = alteration.ActivityId; |
| | | 44 | | |
| | 0 | 45 | | if (activityId == null) |
| | | 46 | | { |
| | 0 | 47 | | context.Fail("No existing activity execution context was found and no activity ID was specified"); |
| | 0 | 48 | | return; |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | var activityNode = workflowExecutionContext.FindNodeByActivityId(activityId); |
| | | 52 | | |
| | 0 | 53 | | if (activityNode == null) |
| | | 54 | | { |
| | 0 | 55 | | context.Fail($"Activity with ID {activityId} not found"); |
| | 0 | 56 | | return; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // Find the parent activity execution context within which to schedule the activity. |
| | 0 | 60 | | var parentActivityContexts = workflowExecutionContext.ActivityExecutionContexts.Reverse().ToList(); |
| | | 61 | | |
| | 0 | 62 | | var parentExecutionContext = |
| | 0 | 63 | | (from ancestorNode in activityNode.Ancestors() |
| | 0 | 64 | | from parentActivityContext in parentActivityContexts |
| | 0 | 65 | | where parentActivityContext.Activity.Id == ancestorNode.Activity.Id |
| | 0 | 66 | | select parentActivityContext).FirstOrDefault(); |
| | | 67 | | |
| | 0 | 68 | | if (parentExecutionContext == null) |
| | | 69 | | { |
| | 0 | 70 | | context.Fail($"Could not find parent activity execution context for activity with ID {activityId}"); |
| | 0 | 71 | | return; |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | await parentExecutionContext.SendSignalAsync(new ScheduleChildActivity(activityNode.Activity)); |
| | 0 | 75 | | context.Succeed(); |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static ActivityExecutionContext? GetActivityExecutionContext(AlterationContext context, ScheduleActivity alt |
| | | 79 | | { |
| | 0 | 80 | | if (alteration.ActivityInstanceId != null) |
| | 0 | 81 | | return context.WorkflowExecutionContext.ActivityExecutionContexts.FirstOrDefault(x => x.Id == alteration.Act |
| | | 82 | | |
| | 0 | 83 | | return null; |
| | | 84 | | } |
| | | 85 | | } |