| | | 1 | | using Elsa.Workflows.Runtime; |
| | | 2 | | using Elsa.Workflows.Runtime.Messages; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Scheduling.Tasks; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A task that runs a workflow. |
| | | 9 | | /// </summary> |
| | | 10 | | public class RunWorkflowTask : ITask |
| | | 11 | | { |
| | | 12 | | private readonly ScheduleNewWorkflowInstanceRequest _request; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initializes a new instance of the <see cref="RunWorkflowTask"/> class. |
| | | 16 | | /// </summary> |
| | 3 | 17 | | public RunWorkflowTask(ScheduleNewWorkflowInstanceRequest request) |
| | | 18 | | { |
| | 3 | 19 | | _request = request; |
| | 3 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public async ValueTask ExecuteAsync(TaskExecutionContext context) |
| | | 24 | | { |
| | 0 | 25 | | var workflowRuntime = context.ServiceProvider.GetRequiredService<IWorkflowRuntime>(); |
| | 0 | 26 | | var workflowClient = await workflowRuntime.CreateClientAsync(); |
| | 0 | 27 | | var request = new CreateAndRunWorkflowInstanceRequest |
| | 0 | 28 | | { |
| | 0 | 29 | | WorkflowDefinitionHandle = _request.WorkflowDefinitionHandle, |
| | 0 | 30 | | TriggerActivityId = _request.TriggerActivityId, |
| | 0 | 31 | | Input = _request.Input, |
| | 0 | 32 | | Properties = _request.Properties, |
| | 0 | 33 | | ParentId = _request.ParentId, |
| | 0 | 34 | | CorrelationId = _request.CorrelationId |
| | 0 | 35 | | }; |
| | 0 | 36 | | await workflowClient.CreateAndRunInstanceAsync(request); |
| | 0 | 37 | | } |
| | | 38 | | } |