@@ -48,10 +48,12 @@ public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow)
48
48
_logger . LogError ( "Workflow {0} version {1} is not registered" , workflow . WorkflowDefinitionId , workflow . Version ) ;
49
49
return wfResult ;
50
50
}
51
+
52
+ _cancellationProcessor . ProcessCancellations ( workflow , def , wfResult ) ;
51
53
52
54
foreach ( var pointer in exePointers )
53
55
{
54
- if ( pointer . Status == PointerStatus . Cancelled )
56
+ if ( ! pointer . Active )
55
57
continue ;
56
58
57
59
var step = def . Steps . FindById ( pointer . StepId ) ;
@@ -64,97 +66,17 @@ public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow)
64
66
WorkflowId = workflow . Id ,
65
67
ExecutionPointerId = pointer . Id ,
66
68
ErrorTime = _datetimeProvider . Now . ToUniversalTime ( ) ,
67
- Message = String . Format ( "Unable to find step {0 } in workflow definition" , pointer . StepId )
69
+ Message = $ "Unable to find step { pointer . StepId } in workflow definition"
68
70
} ) ;
69
71
continue ;
70
72
}
71
73
72
74
try
73
- {
74
- switch ( step . InitForExecution ( wfResult , def , workflow , pointer ) )
75
- {
76
- case ExecutionPipelineDirective . Defer :
77
- continue ;
78
- case ExecutionPipelineDirective . EndWorkflow :
79
- workflow . Status = WorkflowStatus . Complete ;
80
- workflow . CompleteTime = _datetimeProvider . Now . ToUniversalTime ( ) ;
81
- continue ;
82
- }
83
-
84
- if ( pointer . Status != PointerStatus . Running )
85
- {
86
- pointer . Status = PointerStatus . Running ;
87
- _publisher . PublishNotification ( new StepStarted ( )
88
- {
89
- EventTimeUtc = _datetimeProvider . Now ,
90
- Reference = workflow . Reference ,
91
- ExecutionPointerId = pointer . Id ,
92
- StepId = step . Id ,
93
- WorkflowInstanceId = workflow . Id ,
94
- WorkflowDefinitionId = workflow . WorkflowDefinitionId ,
95
- Version = workflow . Version
96
- } ) ;
97
- }
98
-
99
- if ( ! pointer . StartTime . HasValue )
100
- {
101
- pointer . StartTime = _datetimeProvider . Now . ToUniversalTime ( ) ;
102
- }
75
+ {
76
+ if ( ! InitializeStep ( workflow , step , wfResult , def , pointer ) )
77
+ continue ;
103
78
104
- using ( var scope = _scopeProvider . CreateScope ( ) )
105
- {
106
- _logger . LogDebug ( "Starting step {0} on workflow {1}" , step . Name , workflow . Id ) ;
107
-
108
- IStepBody body = step . ConstructBody ( scope . ServiceProvider ) ;
109
-
110
- if ( body == null )
111
- {
112
- _logger . LogError ( "Unable to construct step body {0}" , step . BodyType . ToString ( ) ) ;
113
- pointer . SleepUntil = _datetimeProvider . Now . ToUniversalTime ( ) . Add ( _options . ErrorRetryInterval ) ;
114
- wfResult . Errors . Add ( new ExecutionError ( )
115
- {
116
- WorkflowId = workflow . Id ,
117
- ExecutionPointerId = pointer . Id ,
118
- ErrorTime = _datetimeProvider . Now . ToUniversalTime ( ) ,
119
- Message = String . Format ( "Unable to construct step body {0}" , step . BodyType . ToString ( ) )
120
- } ) ;
121
- continue ;
122
- }
123
-
124
- IStepExecutionContext context = new StepExecutionContext ( )
125
- {
126
- Workflow = workflow ,
127
- Step = step ,
128
- PersistenceData = pointer . PersistenceData ,
129
- ExecutionPointer = pointer ,
130
- Item = pointer . ContextItem
131
- } ;
132
-
133
- foreach ( var input in step . Inputs )
134
- input . AssignInput ( workflow . Data , body , context ) ;
135
-
136
-
137
- switch ( step . BeforeExecute ( wfResult , context , pointer , body ) )
138
- {
139
- case ExecutionPipelineDirective . Defer :
140
- continue ;
141
- case ExecutionPipelineDirective . EndWorkflow :
142
- workflow . Status = WorkflowStatus . Complete ;
143
- workflow . CompleteTime = _datetimeProvider . Now . ToUniversalTime ( ) ;
144
- continue ;
145
- }
146
-
147
- var result = await body . RunAsync ( context ) ;
148
-
149
- if ( result . Proceed )
150
- {
151
- foreach ( var output in step . Outputs )
152
- output . AssignOutput ( workflow . Data , body , context ) ;
153
- }
154
-
155
- _executionResultProcessor . ProcessExecutionResult ( workflow , def , pointer , step , result , wfResult ) ;
156
- step . AfterExecute ( wfResult , context , result , pointer ) ;
157
- }
79
+ await ExecuteStep ( workflow , step , pointer , wfResult , def ) ;
158
80
}
159
81
catch ( Exception ex )
160
82
{
@@ -171,16 +93,105 @@ public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow)
171
93
Host . ReportStepError ( workflow , step , ex ) ;
172
94
}
173
95
_cancellationProcessor . ProcessCancellations ( workflow , def , wfResult ) ;
174
-
175
-
176
-
177
96
}
178
97
ProcessAfterExecutionIteration ( workflow , def , wfResult ) ;
179
98
DetermineNextExecutionTime ( workflow ) ;
180
99
181
100
return wfResult ;
182
101
}
183
-
102
+
103
+ private bool InitializeStep ( WorkflowInstance workflow , WorkflowStep step , WorkflowExecutorResult wfResult , WorkflowDefinition def , ExecutionPointer pointer )
104
+ {
105
+ switch ( step . InitForExecution ( wfResult , def , workflow , pointer ) )
106
+ {
107
+ case ExecutionPipelineDirective . Defer :
108
+ return false ;
109
+ case ExecutionPipelineDirective . EndWorkflow :
110
+ workflow . Status = WorkflowStatus . Complete ;
111
+ workflow . CompleteTime = _datetimeProvider . Now . ToUniversalTime ( ) ;
112
+ return false ;
113
+ }
114
+
115
+ if ( pointer . Status != PointerStatus . Running )
116
+ {
117
+ pointer . Status = PointerStatus . Running ;
118
+ _publisher . PublishNotification ( new StepStarted ( )
119
+ {
120
+ EventTimeUtc = _datetimeProvider . Now ,
121
+ Reference = workflow . Reference ,
122
+ ExecutionPointerId = pointer . Id ,
123
+ StepId = step . Id ,
124
+ WorkflowInstanceId = workflow . Id ,
125
+ WorkflowDefinitionId = workflow . WorkflowDefinitionId ,
126
+ Version = workflow . Version
127
+ } ) ;
128
+ }
129
+
130
+ if ( ! pointer . StartTime . HasValue )
131
+ {
132
+ pointer . StartTime = _datetimeProvider . Now . ToUniversalTime ( ) ;
133
+ }
134
+
135
+ return true ;
136
+ }
137
+
138
+ private async Task ExecuteStep ( WorkflowInstance workflow , WorkflowStep step , ExecutionPointer pointer , WorkflowExecutorResult wfResult , WorkflowDefinition def )
139
+ {
140
+ using ( var scope = _scopeProvider . CreateScope ( ) )
141
+ {
142
+ _logger . LogDebug ( "Starting step {0} on workflow {1}" , step . Name , workflow . Id ) ;
143
+
144
+ IStepBody body = step . ConstructBody ( scope . ServiceProvider ) ;
145
+
146
+ if ( body == null )
147
+ {
148
+ _logger . LogError ( "Unable to construct step body {0}" , step . BodyType . ToString ( ) ) ;
149
+ pointer . SleepUntil = _datetimeProvider . Now . ToUniversalTime ( ) . Add ( _options . ErrorRetryInterval ) ;
150
+ wfResult . Errors . Add ( new ExecutionError ( )
151
+ {
152
+ WorkflowId = workflow . Id ,
153
+ ExecutionPointerId = pointer . Id ,
154
+ ErrorTime = _datetimeProvider . Now . ToUniversalTime ( ) ,
155
+ Message = $ "Unable to construct step body { step . BodyType . ToString ( ) } "
156
+ } ) ;
157
+ return ;
158
+ }
159
+
160
+ IStepExecutionContext context = new StepExecutionContext ( )
161
+ {
162
+ Workflow = workflow ,
163
+ Step = step ,
164
+ PersistenceData = pointer . PersistenceData ,
165
+ ExecutionPointer = pointer ,
166
+ Item = pointer . ContextItem
167
+ } ;
168
+
169
+ foreach ( var input in step . Inputs )
170
+ input . AssignInput ( workflow . Data , body , context ) ;
171
+
172
+ switch ( step . BeforeExecute ( wfResult , context , pointer , body ) )
173
+ {
174
+ case ExecutionPipelineDirective . Defer :
175
+ return ;
176
+ case ExecutionPipelineDirective . EndWorkflow :
177
+ workflow . Status = WorkflowStatus . Complete ;
178
+ workflow . CompleteTime = _datetimeProvider . Now . ToUniversalTime ( ) ;
179
+ return ;
180
+ }
181
+
182
+ var result = await body . RunAsync ( context ) ;
183
+
184
+ if ( result . Proceed )
185
+ {
186
+ foreach ( var output in step . Outputs )
187
+ output . AssignOutput ( workflow . Data , body , context ) ;
188
+ }
189
+
190
+ _executionResultProcessor . ProcessExecutionResult ( workflow , def , pointer , step , result , wfResult ) ;
191
+ step . AfterExecute ( wfResult , context , result , pointer ) ;
192
+ }
193
+ }
194
+
184
195
private void ProcessAfterExecutionIteration ( WorkflowInstance workflow , WorkflowDefinition workflowDef , WorkflowExecutorResult workflowResult )
185
196
{
186
197
var pointers = workflow . ExecutionPointers . Where ( x => x . EndTime == null ) ;
0 commit comments