| | | 1 | | using Elsa.Workflows.Activities; |
| | | 2 | | using Elsa.Workflows.Management; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Elsa.Workflows.Options; |
| | | 5 | | using Elsa.Workflows.State; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Runtime; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a host of a workflow instance to interact with. |
| | | 13 | | /// </summary> |
| | | 14 | | [Obsolete("Use IWorkflowRuntime and IWorkflowClient services instead.")] |
| | | 15 | | public class WorkflowHost : IWorkflowHost |
| | | 16 | | { |
| | | 17 | | private readonly IServiceScopeFactory _serviceScopeFactory; |
| | | 18 | | private readonly ILogger<WorkflowHost> _logger; |
| | | 19 | | private CancellationTokenSource? _linkedTokenSource; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="WorkflowHost"/> class. |
| | | 23 | | /// </summary> |
| | 0 | 24 | | public WorkflowHost( |
| | 0 | 25 | | IServiceScopeFactory serviceScopeFactory, |
| | 0 | 26 | | WorkflowGraph workflowGraph, |
| | 0 | 27 | | WorkflowState workflowState, |
| | 0 | 28 | | ILogger<WorkflowHost> logger) |
| | | 29 | | { |
| | 0 | 30 | | WorkflowGraph = workflowGraph; |
| | 0 | 31 | | WorkflowState = workflowState; |
| | 0 | 32 | | _serviceScopeFactory = serviceScopeFactory; |
| | 0 | 33 | | _logger = logger; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | 0 | 37 | | public WorkflowGraph WorkflowGraph { get; } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 0 | 40 | | public Workflow Workflow => WorkflowGraph.Workflow; |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | 0 | 43 | | public WorkflowState WorkflowState { get; set; } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | 0 | 46 | | public object? LastResult { get; set; } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public async Task<bool> CanStartWorkflowAsync(RunWorkflowOptions? @params = default, CancellationToken cancellationT |
| | | 50 | | { |
| | 0 | 51 | | var strategyType = Workflow.Options.ActivationStrategyType; |
| | | 52 | | |
| | 0 | 53 | | if (strategyType == null) |
| | 0 | 54 | | return true; |
| | | 55 | | |
| | 0 | 56 | | await using var scope = _serviceScopeFactory.CreateAsyncScope(); |
| | 0 | 57 | | var instantiationStrategies = scope.ServiceProvider.GetServices<IWorkflowActivationStrategy>(); |
| | 0 | 58 | | var strategy = instantiationStrategies.FirstOrDefault(x => x.GetType() == strategyType); |
| | | 59 | | |
| | 0 | 60 | | if (strategy == null) |
| | 0 | 61 | | return true; |
| | | 62 | | |
| | 0 | 63 | | var correlationId = @params?.CorrelationId; |
| | 0 | 64 | | var strategyContext = new WorkflowInstantiationStrategyContext(Workflow, correlationId, cancellationToken); |
| | 0 | 65 | | return await strategy.GetAllowActivationAsync(strategyContext); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public async Task<RunWorkflowResult> RunWorkflowAsync(RunWorkflowOptions? @params = null, CancellationToken cancella |
| | | 70 | | { |
| | 0 | 71 | | if (WorkflowState.Status != WorkflowStatus.Running) |
| | | 72 | | { |
| | 0 | 73 | | _logger.LogWarning("Attempt to resume workflow {WorkflowInstanceId} that is not in the Running state. The ac |
| | 0 | 74 | | return new(null!, WorkflowState, Workflow, null, Journal.Empty); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | var runOptions = new RunWorkflowOptions |
| | 0 | 78 | | { |
| | 0 | 79 | | WorkflowInstanceId = WorkflowState.Id, |
| | 0 | 80 | | CorrelationId = @params?.CorrelationId, |
| | 0 | 81 | | Input = @params?.Input, |
| | 0 | 82 | | Properties = @params?.Properties, |
| | 0 | 83 | | ActivityHandle = @params?.ActivityHandle, |
| | 0 | 84 | | BookmarkId = @params?.BookmarkId, |
| | 0 | 85 | | TriggerActivityId = @params?.TriggerActivityId, |
| | 0 | 86 | | ParentWorkflowInstanceId = @params?.ParentWorkflowInstanceId |
| | 0 | 87 | | }; |
| | 0 | 88 | | _linkedTokenSource = new(); |
| | 0 | 89 | | var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _linkedTokenSou |
| | | 90 | | |
| | 0 | 91 | | await using var scope = _serviceScopeFactory.CreateAsyncScope(); |
| | 0 | 92 | | var workflowRunner = scope.ServiceProvider.GetRequiredService<IWorkflowRunner>(); |
| | 0 | 93 | | var workflowResult = await workflowRunner.RunAsync(WorkflowGraph, WorkflowState, runOptions, linkedCancellationT |
| | | 94 | | |
| | 0 | 95 | | WorkflowState = workflowResult.WorkflowState; |
| | 0 | 96 | | await PersistStateAsync(scope, cancellationToken); |
| | | 97 | | |
| | 0 | 98 | | _linkedTokenSource.Dispose(); |
| | 0 | 99 | | _linkedTokenSource = null; |
| | | 100 | | |
| | 0 | 101 | | return workflowResult; |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc /> |
| | | 105 | | public async Task CancelWorkflowAsync(CancellationToken cancellationToken = default) |
| | | 106 | | { |
| | | 107 | | // If the workflow is currently executing, cancel it. This will cause the workflow to be cancelled gracefully. |
| | 0 | 108 | | if (_linkedTokenSource != null) |
| | | 109 | | { |
| | 0 | 110 | | _linkedTokenSource.Cancel(); |
| | 0 | 111 | | return; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // Otherwise, cancel the workflow by executing the canceler. This will set up a workflow execution pipeline that |
| | 0 | 115 | | await using var scope = _serviceScopeFactory.CreateAsyncScope(); |
| | 0 | 116 | | var serviceProvider = scope.ServiceProvider; |
| | 0 | 117 | | var workflowCanceler = serviceProvider.GetRequiredService<IWorkflowCanceler>(); |
| | 0 | 118 | | WorkflowState = await workflowCanceler.CancelWorkflowAsync(WorkflowGraph, WorkflowState, cancellationToken); |
| | 0 | 119 | | await PersistStateAsync(scope, cancellationToken); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <inheritdoc /> |
| | | 123 | | public async Task PersistStateAsync(CancellationToken cancellationToken = default) |
| | | 124 | | { |
| | 0 | 125 | | await using var scope = _serviceScopeFactory.CreateAsyncScope(); |
| | 0 | 126 | | await PersistStateAsync(scope, cancellationToken); |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | private async Task PersistStateAsync(IServiceScope scope, CancellationToken cancellationToken = default) |
| | | 130 | | { |
| | 0 | 131 | | var workflowInstanceManager = scope.ServiceProvider.GetRequiredService<IWorkflowInstanceManager>(); |
| | 0 | 132 | | await workflowInstanceManager.SaveAsync(WorkflowState, cancellationToken); |
| | 0 | 133 | | } |
| | | 134 | | } |