| | | 1 | | using System.Net.Mime; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Workflows.Management; |
| | | 4 | | using Elsa.Workflows.Runtime; |
| | | 5 | | using Elsa.Workflows.State; |
| | | 6 | | using FastEndpoints; |
| | | 7 | | using Microsoft.AspNetCore.Http; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Execute; |
| | | 10 | | |
| | | 11 | | public static class WorkflowExecutionHelper |
| | | 12 | | { |
| | | 13 | | public static async Task ExecuteWorkflowAsync( |
| | | 14 | | IExecutionRequest request, |
| | | 15 | | IWorkflowDefinitionService workflowDefinitionService, |
| | | 16 | | IWorkflowRuntime workflowRuntime, |
| | | 17 | | IWorkflowStarter workflowStarter, |
| | | 18 | | IApiSerializer apiSerializer, |
| | | 19 | | HttpContext httpContext, |
| | | 20 | | CancellationToken cancellationToken) |
| | | 21 | | { |
| | 11 | 22 | | var definitionId = request.DefinitionId; |
| | 11 | 23 | | var versionOptions = request.VersionOptions ?? VersionOptions.Published; |
| | 11 | 24 | | var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(definitionId, versionOptions, cancell |
| | | 25 | | |
| | 11 | 26 | | if (workflowGraph == null) |
| | | 27 | | { |
| | 0 | 28 | | await httpContext.Response.SendNotFoundAsync(cancellation: cancellationToken); |
| | 0 | 29 | | return; |
| | | 30 | | } |
| | | 31 | | |
| | 11 | 32 | | var startRequest = new StartWorkflowRequest |
| | 11 | 33 | | { |
| | 11 | 34 | | Workflow = workflowGraph.Workflow, |
| | 11 | 35 | | CorrelationId = request.CorrelationId, |
| | 11 | 36 | | Name = request.Name, |
| | 11 | 37 | | Input = request.GetInputAsDictionary(), |
| | 11 | 38 | | Variables = request.GetVariablesAsDictionary(), |
| | 11 | 39 | | TriggerActivityId = request.TriggerActivityId, |
| | 11 | 40 | | ActivityHandle = request.ActivityHandle |
| | 11 | 41 | | }; |
| | | 42 | | |
| | 11 | 43 | | var startResponse = await workflowStarter.StartWorkflowAsync(startRequest, cancellationToken); |
| | | 44 | | |
| | 11 | 45 | | if(!httpContext.Response.HasStarted) |
| | 11 | 46 | | httpContext.Response.Headers.Append("x-elsa-workflow-cannot-start", startResponse.CannotStart.ToString()); |
| | | 47 | | |
| | 11 | 48 | | if (startResponse.CannotStart) |
| | | 49 | | { |
| | 0 | 50 | | httpContext.Response.StatusCode = StatusCodes.Status200OK; |
| | 0 | 51 | | await httpContext.Response.SendOkAsync(cancellationToken); |
| | 0 | 52 | | return; |
| | | 53 | | } |
| | | 54 | | |
| | 11 | 55 | | var instanceId = startResponse.WorkflowInstanceId!; |
| | | 56 | | |
| | 11 | 57 | | if(!httpContext.Response.HasStarted) |
| | 11 | 58 | | httpContext.Response.Headers.Append("x-elsa-workflow-instance-id", instanceId); |
| | | 59 | | |
| | 11 | 60 | | var workflowClient = await workflowRuntime.CreateClientAsync(instanceId, cancellationToken); |
| | | 61 | | |
| | 11 | 62 | | if (startResponse.SubStatus == WorkflowSubStatus.Faulted) |
| | | 63 | | { |
| | 0 | 64 | | var workflowState = await workflowClient.ExportStateAsync(cancellationToken); |
| | 0 | 65 | | await HandleFaultAsync(workflowState, apiSerializer, httpContext, cancellationToken); |
| | | 66 | | } |
| | | 67 | | else |
| | | 68 | | { |
| | 11 | 69 | | if (!httpContext.Response.HasStarted) |
| | | 70 | | { |
| | 11 | 71 | | httpContext.Response.Headers.Append("x-elsa-response", "true"); |
| | 11 | 72 | | if (httpContext.Response.StatusCode == StatusCodes.Status200OK) |
| | | 73 | | { |
| | 11 | 74 | | var workflowState = await workflowClient.ExportStateAsync(cancellationToken); |
| | 11 | 75 | | var response = apiSerializer.Serialize(new Response(workflowState)); |
| | 11 | 76 | | httpContext.Response.ContentType = MediaTypeNames.Application.Json; |
| | 11 | 77 | | await httpContext.Response.WriteAsync(response, cancellationToken); |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |
| | 11 | 81 | | } |
| | | 82 | | |
| | | 83 | | private static async Task HandleFaultAsync(WorkflowState workflowState, IApiSerializer apiSerializer, HttpContext ht |
| | | 84 | | { |
| | 0 | 85 | | var faultedResponse = apiSerializer.Serialize(new Response(workflowState)); |
| | 0 | 86 | | httpContext.Response.ContentType = MediaTypeNames.Application.Json; |
| | 0 | 87 | | httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; |
| | 0 | 88 | | await httpContext.Response.WriteAsync(faultedResponse, cancellationToken); |
| | 0 | 89 | | } |
| | | 90 | | } |
| | | 91 | | |