| | | 1 | | using Elsa.Workflows.CommitStates; |
| | | 2 | | using Elsa.Workflows.Runtime.Middleware.Workflows; |
| | | 3 | | using Elsa.Workflows.State; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Runtime.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Decorator that disposes the execution-cycle handle stored in |
| | | 9 | | /// <see cref="WorkflowExecutionContext.TransientProperties"/> AFTER the inner commit handler has persisted the |
| | | 10 | | /// workflow's terminal state. This ordering is essential for the drain orchestrator's force-cancel path: the |
| | | 11 | | /// orchestrator awaits <see cref="ExecutionCycleHandle.Disposed"/> with a settle timeout before it overwrites the |
| | | 12 | | /// persisted sub-status with <see cref="WorkflowSubStatus.Interrupted"/>. Without this decorator the handle would |
| | | 13 | | /// dispose at the end of the pipeline middleware (i.e., BEFORE commit), the orchestrator's wait would complete |
| | | 14 | | /// prematurely, and the orchestrator's <c>Interrupted</c> write would either find no instance row (early dispatches) |
| | | 15 | | /// or be clobbered by the runner's subsequent commit (mid-execution dispatches). |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class ExecutionCycleAwareCommitStateHandler : ICommitStateHandler |
| | | 18 | | { |
| | | 19 | | private readonly DefaultCommitStateHandler _inner; |
| | | 20 | | |
| | 1044 | 21 | | public ExecutionCycleAwareCommitStateHandler(DefaultCommitStateHandler inner) => _inner = inner; |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public async Task CommitAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken |
| | | 25 | | { |
| | 0 | 26 | | try { await _inner.CommitAsync(workflowExecutionContext, cancellationToken); } |
| | 0 | 27 | | finally { DisposeCycleHandleIfPresent(workflowExecutionContext); } |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public async Task CommitAsync(WorkflowExecutionContext workflowExecutionContext, WorkflowState workflowState, Cancel |
| | | 32 | | { |
| | 1090 | 33 | | try { await _inner.CommitAsync(workflowExecutionContext, workflowState, cancellationToken); } |
| | 545 | 34 | | finally { DisposeCycleHandleIfPresent(workflowExecutionContext); } |
| | 545 | 35 | | } |
| | | 36 | | |
| | | 37 | | private static void DisposeCycleHandleIfPresent(WorkflowExecutionContext context) |
| | | 38 | | { |
| | 545 | 39 | | if (context.TransientProperties.TryGetValue(ExecutionCycleTrackingMiddleware.ExecutionCycleHandleKey, out var ra |
| | 545 | 40 | | handle.Dispose(); |
| | 545 | 41 | | } |
| | | 42 | | } |