| | | 1 | | using Elsa.Alterations.Core.Contracts; |
| | | 2 | | using Elsa.Alterations.Core.Models; |
| | | 3 | | using Elsa.Alterations.Workflows; |
| | | 4 | | using Elsa.Common; |
| | | 5 | | using Elsa.Common.Models; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | using Elsa.Workflows.Management; |
| | | 8 | | using Elsa.Workflows.Runtime; |
| | | 9 | | using Elsa.Workflows.Runtime.Contracts; |
| | | 10 | | using Elsa.Workflows.Runtime.Requests; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Alterations.Services; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Stores the new plan and schedules it for immediate execution. |
| | | 16 | | /// </summary> |
| | | 17 | | public class DefaultAlterationPlanScheduler : IAlterationPlanScheduler |
| | | 18 | | { |
| | | 19 | | private readonly IWorkflowDefinitionService _workflowDefinitionService; |
| | | 20 | | private readonly IWorkflowDispatcher _workflowDispatcher; |
| | | 21 | | private readonly IIdentityGenerator _identityGenerator; |
| | | 22 | | private readonly IJsonSerializer _jsonSerializer; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="DefaultAlterationPlanScheduler"/> class. |
| | | 26 | | /// </summary> |
| | 1 | 27 | | public DefaultAlterationPlanScheduler(IWorkflowDefinitionService workflowDefinitionService, IWorkflowDispatcher work |
| | | 28 | | { |
| | 1 | 29 | | _workflowDefinitionService = workflowDefinitionService; |
| | 1 | 30 | | _workflowDispatcher = workflowDispatcher; |
| | 1 | 31 | | _identityGenerator = identityGenerator; |
| | 1 | 32 | | _jsonSerializer = jsonSerializer; |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public async Task<string> SubmitAsync(AlterationPlanParams planParams, CancellationToken cancellationToken = default |
| | | 37 | | { |
| | 0 | 38 | | if(string.IsNullOrWhiteSpace(planParams.Id)) |
| | 0 | 39 | | planParams.Id = _identityGenerator.GenerateId(); |
| | | 40 | | |
| | 0 | 41 | | var definitionId = ExecuteAlterationPlanWorkflow.WorkflowDefinitionId; |
| | 0 | 42 | | var workflowGraph = await _workflowDefinitionService.FindWorkflowGraphAsync(definitionId, VersionOptions.Publish |
| | | 43 | | |
| | 0 | 44 | | if (workflowGraph == null) |
| | 0 | 45 | | throw new($"Workflow definition with ID '{definitionId}' not found"); |
| | | 46 | | |
| | 0 | 47 | | var serializedPlan = _jsonSerializer.Serialize(planParams); |
| | 0 | 48 | | var request = new DispatchWorkflowDefinitionRequest(workflowGraph.Workflow.Identity.Id) |
| | 0 | 49 | | { |
| | 0 | 50 | | Input = new Dictionary<string, object> |
| | 0 | 51 | | { |
| | 0 | 52 | | ["Plan"] = serializedPlan |
| | 0 | 53 | | } |
| | 0 | 54 | | }; |
| | 0 | 55 | | await _workflowDispatcher.DispatchAsync(request, cancellationToken); |
| | | 56 | | |
| | 0 | 57 | | return planParams.Id; |
| | 0 | 58 | | } |
| | | 59 | | } |