| | | 1 | | using System.Runtime.ExceptionServices; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Workflows.CommitStates; |
| | | 4 | | using Elsa.Workflows.Management; |
| | | 5 | | using Elsa.Workflows.Management.Entities; |
| | | 6 | | using Elsa.Workflows.Runtime.Entities; |
| | | 7 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 8 | | using Elsa.Workflows.Runtime.Requests; |
| | | 9 | | using Elsa.Workflows.State; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Runtime; |
| | | 12 | | |
| | 539 | 13 | | public class DefaultCommitStateHandler( |
| | 539 | 14 | | IWorkflowInstanceManager workflowInstanceManager, |
| | 539 | 15 | | IBookmarksPersister bookmarkPersister, |
| | 539 | 16 | | IVariablePersistenceManager variablePersistenceManager, |
| | 539 | 17 | | IWorkflowCommitTransaction workflowCommitTransaction, |
| | 539 | 18 | | IWorkflowCommitNotificationBuffer workflowCommitNotificationBuffer, |
| | 539 | 19 | | INotificationSender notificationSender, |
| | 539 | 20 | | ILogRecordSink<ActivityExecutionRecord> activityExecutionLogRecordSink, |
| | 539 | 21 | | ILogRecordSink<WorkflowExecutionLogRecord> workflowExecutionLogRecordSink) : ICommitStateHandler |
| | | 22 | | { |
| | | 23 | | public async Task CommitAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken |
| | | 24 | | { |
| | 0 | 25 | | var workflowState = workflowInstanceManager.ExtractWorkflowState(workflowExecutionContext); |
| | 0 | 26 | | await CommitAsync(workflowExecutionContext, workflowState, cancellationToken); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task CommitAsync(WorkflowExecutionContext workflowExecutionContext, WorkflowState workflowState, Cancel |
| | | 30 | | { |
| | 570 | 31 | | WorkflowInstance? workflowInstance = null; |
| | 570 | 32 | | using var notificationScope = workflowCommitNotificationBuffer.Begin(); |
| | 570 | 33 | | await workflowCommitTransaction.ExecuteAsync(async ct => |
| | 570 | 34 | | { |
| | 570 | 35 | | var updateBookmarksRequest = new UpdateBookmarksRequest(workflowExecutionContext, workflowExecutionContext.B |
| | 570 | 36 | | await bookmarkPersister.PersistBookmarksAsync(updateBookmarksRequest); |
| | 570 | 37 | | await activityExecutionLogRecordSink.PersistExecutionLogsAsync(workflowExecutionContext, ct); |
| | 570 | 38 | | await workflowExecutionLogRecordSink.PersistExecutionLogsAsync(workflowExecutionContext, ct); |
| | 570 | 39 | | await variablePersistenceManager.SaveVariablesAsync(workflowExecutionContext); |
| | 570 | 40 | | workflowInstance = await workflowInstanceManager.SaveAsync(workflowState, ct); |
| | 1139 | 41 | | }, cancellationToken); |
| | | 42 | | |
| | 569 | 43 | | ClearActivityExecutionContextTaint(workflowExecutionContext); |
| | 569 | 44 | | workflowExecutionContext.ExecutionLog.Clear(); |
| | 569 | 45 | | workflowExecutionContext.ClearCompletedActivityExecutionContexts(); |
| | 569 | 46 | | ExceptionDispatchInfo? flushException = null; |
| | | 47 | | try |
| | | 48 | | { |
| | 569 | 49 | | await notificationScope.FlushAsync(cancellationToken); |
| | 568 | 50 | | } |
| | 1 | 51 | | catch (Exception ex) when (ex is not OperationCanceledException and not OutOfMemoryException) |
| | | 52 | | { |
| | 1 | 53 | | flushException = ExceptionDispatchInfo.Capture(ex); |
| | 1 | 54 | | } |
| | | 55 | | |
| | 569 | 56 | | await workflowExecutionContext.ExecuteDeferredTasksAsync(); |
| | 569 | 57 | | await notificationSender.SendAsync(new WorkflowStateCommitted(workflowExecutionContext, workflowState, workflowI |
| | 569 | 58 | | flushException?.Throw(); |
| | 568 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static void ClearActivityExecutionContextTaint(WorkflowExecutionContext workflowExecutionContext) |
| | | 62 | | { |
| | 14026 | 63 | | foreach (var activityExecutionContext in workflowExecutionContext.ActivityExecutionContexts.Where(x => x.IsDirty |
| | 4296 | 64 | | activityExecutionContext.ClearTaint(); |
| | 569 | 65 | | } |
| | | 66 | | } |