| | | 1 | | using Elsa.Workflows.Activities; |
| | | 2 | | using Elsa.Workflows.Management; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Elsa.Workflows.Runtime.Exceptions; |
| | | 5 | | using Elsa.Workflows.Runtime.Messages; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Runtime; |
| | | 8 | | |
| | 68 | 9 | | public class DefaultWorkflowStarter(IWorkflowDefinitionService workflowDefinitionService, IWorkflowActivationStrategyEva |
| | | 10 | | { |
| | | 11 | | public async Task<StartWorkflowResponse> StartWorkflowAsync(StartWorkflowRequest request, CancellationToken cancella |
| | | 12 | | { |
| | 16 | 13 | | var workflow = await GetWorkflowAsync(request, cancellationToken); |
| | | 14 | | |
| | 16 | 15 | | var canStart = await workflowActivationStrategyEvaluator.CanStartWorkflowAsync(new() |
| | 16 | 16 | | { |
| | 16 | 17 | | Workflow = workflow, |
| | 16 | 18 | | CorrelationId = request.CorrelationId |
| | 16 | 19 | | }); |
| | | 20 | | |
| | 16 | 21 | | if (!canStart) |
| | 0 | 22 | | return new() |
| | 0 | 23 | | { |
| | 0 | 24 | | CannotStart = true |
| | 0 | 25 | | }; |
| | | 26 | | |
| | 16 | 27 | | var workflowClient = await workflowRuntime.CreateClientAsync(cancellationToken); |
| | 16 | 28 | | var createWorkflowInstanceRequest = new CreateAndRunWorkflowInstanceRequest |
| | 16 | 29 | | { |
| | 16 | 30 | | WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(workflow.Identity.Id), |
| | 16 | 31 | | CorrelationId = request.CorrelationId, |
| | 16 | 32 | | Name = request.Name, |
| | 16 | 33 | | Input = request.Input, |
| | 16 | 34 | | Variables = request.Variables, |
| | 16 | 35 | | TriggerActivityId = request.TriggerActivityId, |
| | 16 | 36 | | ActivityHandle = request.ActivityHandle, |
| | 16 | 37 | | Properties = request.Properties, |
| | 16 | 38 | | ParentId = request.ParentId |
| | 16 | 39 | | }; |
| | | 40 | | |
| | 16 | 41 | | var runWorkflowResponse = await workflowClient.CreateAndRunInstanceAsync(createWorkflowInstanceRequest, cancella |
| | 16 | 42 | | return new() |
| | 16 | 43 | | { |
| | 16 | 44 | | CannotStart = false, |
| | 16 | 45 | | WorkflowInstanceId = runWorkflowResponse.WorkflowInstanceId, |
| | 16 | 46 | | Status = runWorkflowResponse.Status, |
| | 16 | 47 | | SubStatus = runWorkflowResponse.SubStatus, |
| | 16 | 48 | | Bookmarks = runWorkflowResponse.Bookmarks, |
| | 16 | 49 | | Incidents = runWorkflowResponse.Incidents |
| | 16 | 50 | | }; |
| | 16 | 51 | | } |
| | | 52 | | |
| | | 53 | | private async Task<Workflow> GetWorkflowAsync(StartWorkflowRequest request, CancellationToken cancellationToken) |
| | | 54 | | { |
| | 16 | 55 | | if (request.Workflow != null) |
| | 16 | 56 | | return request.Workflow; |
| | | 57 | | |
| | 0 | 58 | | if (request.WorkflowDefinitionHandle == null) |
| | 0 | 59 | | throw new InvalidOperationException("A workflow definition handle must be provided."); |
| | | 60 | | |
| | 0 | 61 | | var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(request.WorkflowDefinitionHandle, can |
| | | 62 | | |
| | 0 | 63 | | if (workflowGraph == null) |
| | 0 | 64 | | throw new WorkflowGraphNotFoundException("Workflow definition not found.", request.WorkflowDefinitionHandle) |
| | | 65 | | |
| | 0 | 66 | | return workflowGraph.Workflow; |
| | 16 | 67 | | } |
| | | 68 | | } |