|
1 | 1 | using System;
|
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Microsoft.Extensions.Logging; |
2 | 4 | using WorkflowCore.Interface;
|
3 | 5 | using WorkflowCore.Models;
|
| 6 | +using WorkflowCore.Models.LifeCycleEvents; |
4 | 7 |
|
5 | 8 | namespace WorkflowCore.Primitives
|
6 | 9 | {
|
7 | 10 | public class SubWorkflowStepBody : StepBody
|
8 | 11 | {
|
| 12 | + private readonly IScopeProvider _scopeProvider; |
| 13 | + |
| 14 | + public SubWorkflowStepBody(IScopeProvider scopeProvider) |
| 15 | + { |
| 16 | + _scopeProvider = scopeProvider; |
| 17 | + } |
| 18 | + |
9 | 19 | public override ExecutionResult Run(IStepExecutionContext context)
|
10 | 20 | {
|
11 |
| - // TODO: What is this supposed to do? |
12 |
| - throw new NotImplementedException(); |
| 21 | + var eventKey = context.ExecutionPointer.EventKey; |
| 22 | + |
| 23 | + var scope = _scopeProvider.CreateScope(context); |
| 24 | + var workflowController = scope.ServiceProvider.GetRequiredService<IWorkflowController>(); |
| 25 | + var logger = scope.ServiceProvider.GetRequiredService<ILoggerFactory>().CreateLogger( |
| 26 | + typeof(SubWorkflowStepBody).Namespace + "." + nameof(SubWorkflowStepBody)); |
| 27 | + |
| 28 | + if (!context.ExecutionPointer.EventPublished) |
| 29 | + { |
| 30 | + var result = workflowController.StartWorkflow(SubWorkflowId, context.Workflow.Data, context.Workflow.Id).Result; |
| 31 | + |
| 32 | + logger.LogDebug("Started sub workflow {Name} with id='{SubId}' from workflow {WorkflowDefinitionId} ({Id})", |
| 33 | + SubWorkflowId, result, context.Workflow.WorkflowDefinitionId, context.Workflow.Id); |
| 34 | + |
| 35 | + logger.LogDebug("Workflow {Name} ({SubId}) is waiting for event WorkflowCompleted with key='{EventKey}'", |
| 36 | + SubWorkflowId, result, result); |
| 37 | + |
| 38 | + var effectiveDate = DateTime.MinValue; |
| 39 | + return ExecutionResult.WaitForEvent(nameof(WorkflowCompleted), result, effectiveDate); |
| 40 | + } |
| 41 | + |
| 42 | + logger.LogDebug("Sub workflow {Name} ({SubId}) completed", SubWorkflowId, |
| 43 | + context.ExecutionPointer.EventKey); |
| 44 | + |
| 45 | + var persistenceProvider = scope.ServiceProvider.GetRequiredService<IPersistenceProvider>(); |
| 46 | + |
| 47 | + Result = persistenceProvider.GetWorkflowInstance(context.ExecutionPointer.EventKey).Result.Data; |
| 48 | + return ExecutionResult.Next(); |
13 | 49 | }
|
| 50 | + |
| 51 | + public string SubWorkflowId { get; set; } |
| 52 | + |
| 53 | + public object Parameters { get; set; } |
| 54 | + |
| 55 | + public object Result { get; set; } |
14 | 56 | }
|
15 | 57 | }
|
0 commit comments