| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Workflows.Management; |
| | | 3 | | using Elsa.Workflows.Runtime.Middleware.Activities; |
| | | 4 | | using Elsa.Workflows.Runtime.Options; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Runtime; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// An activity invoker that invokes activities detached from the workflow. This is useful for invoking activities from |
| | | 11 | | /// </summary> |
| | 3 | 12 | | public class BackgroundActivityInvoker( |
| | 3 | 13 | | IBookmarkQueue bookmarkQueue, |
| | 3 | 14 | | IWorkflowInstanceManager workflowInstanceManager, |
| | 3 | 15 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 3 | 16 | | IVariablePersistenceManager variablePersistenceManager, |
| | 3 | 17 | | IActivityInvoker activityInvoker, |
| | 3 | 18 | | IActivityPropertyLogPersistenceEvaluator activityPropertyLogPersistenceEvaluator, |
| | 3 | 19 | | WorkflowHeartbeatGeneratorFactory workflowHeartbeatGeneratorFactory, |
| | 3 | 20 | | IServiceProvider serviceProvider, |
| | 3 | 21 | | ILogger<BackgroundActivityInvoker> logger) |
| | | 22 | | : IBackgroundActivityInvoker |
| | | 23 | | { |
| | 3 | 24 | | private readonly ILogger _logger = logger; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public async Task ExecuteAsync(ScheduledBackgroundActivity scheduledBackgroundActivity, CancellationToken cancellati |
| | | 28 | | { |
| | 3 | 29 | | var workflowInstanceId = scheduledBackgroundActivity.WorkflowInstanceId; |
| | 3 | 30 | | var workflowInstance = await workflowInstanceManager.FindByIdAsync(workflowInstanceId, cancellationToken); |
| | 3 | 31 | | if (workflowInstance == null) throw new("Workflow instance not found"); |
| | 3 | 32 | | var workflowState = workflowInstance.WorkflowState; |
| | 3 | 33 | | var workflow = await workflowDefinitionService.FindWorkflowGraphAsync(workflowInstance.DefinitionVersionId, canc |
| | 3 | 34 | | if (workflow == null) throw new("Workflow definition not found"); |
| | 3 | 35 | | var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync(serviceProvider, workflow, workflowSta |
| | 3 | 36 | | var activityNodeId = scheduledBackgroundActivity.ActivityNodeId; |
| | 7 | 37 | | var activityExecutionContext = workflowExecutionContext.ActivityExecutionContexts.First(x => x.NodeId == activit |
| | | 38 | | |
| | 3 | 39 | | using (workflowHeartbeatGeneratorFactory.CreateHeartbeatGenerator(workflowExecutionContext)) |
| | | 40 | | { |
| | 3 | 41 | | await variablePersistenceManager.LoadVariablesAsync(workflowExecutionContext); |
| | 3 | 42 | | activityExecutionContext.SetIsBackgroundExecution(); |
| | 3 | 43 | | await activityInvoker.InvokeAsync(activityExecutionContext); |
| | 3 | 44 | | await variablePersistenceManager.SaveVariablesAsync(workflowExecutionContext); |
| | 3 | 45 | | } |
| | 3 | 46 | | await ResumeWorkflowAsync(activityExecutionContext, scheduledBackgroundActivity); |
| | 3 | 47 | | } |
| | | 48 | | |
| | | 49 | | private async Task ResumeWorkflowAsync(ActivityExecutionContext activityExecutionContext, ScheduledBackgroundActivit |
| | | 50 | | { |
| | 3 | 51 | | var cancellationToken = activityExecutionContext.CancellationToken; |
| | 3 | 52 | | var activityNodeId = scheduledBackgroundActivity.ActivityNodeId; |
| | 3 | 53 | | if (cancellationToken.IsCancellationRequested) |
| | | 54 | | { |
| | 0 | 55 | | _logger.LogInformation("Background execution for activity {ActivityNodeId} was canceled", activityNodeId); |
| | 0 | 56 | | return; |
| | | 57 | | } |
| | | 58 | | |
| | 3 | 59 | | var inputKey = BackgroundActivityInvokerMiddleware.GetBackgroundActivityOutputKey(activityNodeId); |
| | 3 | 60 | | var outcomesKey = BackgroundActivityInvokerMiddleware.GetBackgroundActivityOutcomesKey(activityNodeId); |
| | 3 | 61 | | var completedKey = BackgroundActivityInvokerMiddleware.GetBackgroundActivityCompletedKey(activityNodeId); |
| | 3 | 62 | | var journalDataKey = BackgroundActivityInvokerMiddleware.GetBackgroundActivityJournalDataKey(activityNodeId); |
| | 3 | 63 | | var bookmarksKey = BackgroundActivityInvokerMiddleware.GetBackgroundActivityBookmarksKey(activityNodeId); |
| | 3 | 64 | | var scheduledActivitiesKey = BackgroundActivityInvokerMiddleware.GetBackgroundActivityScheduledActivitiesKey(act |
| | 3 | 65 | | var propsKey = BackgroundActivityInvokerMiddleware.GetBackgroundActivityPropertiesKey(activityNodeId); |
| | 3 | 66 | | var outcomes = activityExecutionContext.GetBackgroundOutcomes()?.ToList(); |
| | 3 | 67 | | var completed = activityExecutionContext.GetBackgroundCompleted(); |
| | 3 | 68 | | var scheduledActivities = activityExecutionContext.GetBackgroundScheduledActivities().ToList(); |
| | 3 | 69 | | var workflowInstanceId = scheduledBackgroundActivity.WorkflowInstanceId; |
| | 3 | 70 | | var outputValues = await activityPropertyLogPersistenceEvaluator.GetPersistableOutputAsync(activityExecutionCont |
| | 3 | 71 | | var bookmarkProps = new Dictionary<string, object> |
| | 3 | 72 | | { |
| | 3 | 73 | | [scheduledActivitiesKey] = JsonSerializer.Serialize(scheduledActivities), |
| | 3 | 74 | | [inputKey] = outputValues, |
| | 3 | 75 | | [journalDataKey] = activityExecutionContext.JournalData, |
| | 3 | 76 | | [bookmarksKey] = activityExecutionContext.Bookmarks.ToList(), |
| | 3 | 77 | | [propsKey] = activityExecutionContext.Properties.ToDictionary() // ChangeTrackingDictionary is not persistab |
| | 3 | 78 | | }; |
| | | 79 | | |
| | 3 | 80 | | if (outcomes != null) bookmarkProps[outcomesKey] = outcomes; |
| | 6 | 81 | | if (completed != null) bookmarkProps[completedKey] = completed; |
| | | 82 | | |
| | 3 | 83 | | var resumeBookmarkOptions = new ResumeBookmarkOptions |
| | 3 | 84 | | { |
| | 3 | 85 | | Properties = bookmarkProps, |
| | 3 | 86 | | }; |
| | 3 | 87 | | var enqueuedBookmark = new NewBookmarkQueueItem |
| | 3 | 88 | | { |
| | 3 | 89 | | WorkflowInstanceId = workflowInstanceId, |
| | 3 | 90 | | BookmarkId = scheduledBackgroundActivity.BookmarkId, |
| | 3 | 91 | | Options = resumeBookmarkOptions |
| | 3 | 92 | | }; |
| | 3 | 93 | | await bookmarkQueue.EnqueueAsync(enqueuedBookmark, cancellationToken); |
| | 3 | 94 | | } |
| | | 95 | | } |