@@ -23,110 +23,112 @@ import (
23
23
)
24
24
25
25
// NewTaskRunner creates a TaskRunner instance based on the task type.
26
- func NewTaskRunner (taskName string , task model.Task , taskSupport TaskSupport ) (TaskRunner , error ) {
26
+ func NewTaskRunner (taskName string , task model.Task , workflowDef * model. Workflow ) (TaskRunner , error ) {
27
27
switch t := task .(type ) {
28
28
case * model.SetTask :
29
- return NewSetTaskRunner (taskName , t , taskSupport )
29
+ return NewSetTaskRunner (taskName , t )
30
30
case * model.RaiseTask :
31
- return NewRaiseTaskRunner (taskName , t , taskSupport )
31
+ return NewRaiseTaskRunner (taskName , t , workflowDef )
32
32
case * model.DoTask :
33
- return NewDoTaskRunner (t .Do , taskSupport )
33
+ return NewDoTaskRunner (t .Do , workflowDef )
34
34
case * model.ForTask :
35
- return NewForTaskRunner (taskName , t , taskSupport )
35
+ return NewForTaskRunner (taskName , t , workflowDef )
36
+ case * model.CallHTTP :
37
+ return NewCallHttpRunner (taskName , t )
36
38
default :
37
39
return nil , fmt .Errorf ("unsupported task type '%T' for task '%s'" , t , taskName )
38
40
}
39
41
}
40
42
41
- func NewDoTaskRunner (taskList * model.TaskList , taskSupport TaskSupport ) (* DoTaskRunner , error ) {
43
+ func NewDoTaskRunner (taskList * model.TaskList , workflowDef * model. Workflow ) (* DoTaskRunner , error ) {
42
44
return & DoTaskRunner {
43
45
TaskList : taskList ,
44
- TaskSupport : taskSupport ,
46
+ WorkflowDef : workflowDef ,
45
47
}, nil
46
48
}
47
49
48
50
type DoTaskRunner struct {
49
51
TaskList * model.TaskList
50
- TaskSupport TaskSupport
52
+ WorkflowDef * model. Workflow
51
53
}
52
54
53
- func (d * DoTaskRunner ) Run (input interface {}) (output interface {}, err error ) {
55
+ func (d * DoTaskRunner ) Run (input interface {}, taskSupport TaskSupport ) (output interface {}, err error ) {
54
56
if d .TaskList == nil {
55
57
return input , nil
56
58
}
57
- return d .runTasks (input , d . TaskList )
59
+ return d .runTasks (input , taskSupport )
58
60
}
59
61
60
62
func (d * DoTaskRunner ) GetTaskName () string {
61
63
return ""
62
64
}
63
65
64
66
// runTasks runs all defined tasks sequentially.
65
- func (d * DoTaskRunner ) runTasks (input interface {}, tasks * model. TaskList ) (output interface {}, err error ) {
67
+ func (d * DoTaskRunner ) runTasks (input interface {}, taskSupport TaskSupport ) (output interface {}, err error ) {
66
68
output = input
67
- if tasks == nil {
69
+ if d . TaskList == nil {
68
70
return output , nil
69
71
}
70
72
71
73
idx := 0
72
- currentTask := (* tasks )[idx ]
74
+ currentTask := (* d . TaskList )[idx ]
73
75
74
76
for currentTask != nil {
75
- if err = d . TaskSupport .SetTaskDef (currentTask ); err != nil {
77
+ if err = taskSupport .SetTaskDef (currentTask ); err != nil {
76
78
return nil , err
77
79
}
78
- if err = d . TaskSupport .SetTaskReferenceFromName (currentTask .Key ); err != nil {
80
+ if err = taskSupport .SetTaskReferenceFromName (currentTask .Key ); err != nil {
79
81
return nil , err
80
82
}
81
83
82
- if shouldRun , err := d .shouldRunTask (input , currentTask ); err != nil {
84
+ if shouldRun , err := d .shouldRunTask (input , taskSupport , currentTask ); err != nil {
83
85
return output , err
84
86
} else if ! shouldRun {
85
- idx , currentTask = tasks .Next (idx )
87
+ idx , currentTask = d . TaskList .Next (idx )
86
88
continue
87
89
}
88
90
89
- d . TaskSupport .SetTaskStatus (currentTask .Key , ctx .PendingStatus )
91
+ taskSupport .SetTaskStatus (currentTask .Key , ctx .PendingStatus )
90
92
91
93
// Check if this task is a SwitchTask and handle it
92
94
if switchTask , ok := currentTask .Task .(* model.SwitchTask ); ok {
93
- flowDirective , err := d .evaluateSwitchTask (input , currentTask .Key , switchTask )
95
+ flowDirective , err := d .evaluateSwitchTask (input , taskSupport , currentTask .Key , switchTask )
94
96
if err != nil {
95
- d . TaskSupport .SetTaskStatus (currentTask .Key , ctx .FaultedStatus )
97
+ taskSupport .SetTaskStatus (currentTask .Key , ctx .FaultedStatus )
96
98
return output , err
97
99
}
98
- d . TaskSupport .SetTaskStatus (currentTask .Key , ctx .CompletedStatus )
100
+ taskSupport .SetTaskStatus (currentTask .Key , ctx .CompletedStatus )
99
101
100
102
// Process FlowDirective: update idx/currentTask accordingly
101
- idx , currentTask = tasks .KeyAndIndex (flowDirective .Value )
103
+ idx , currentTask = d . TaskList .KeyAndIndex (flowDirective .Value )
102
104
if currentTask == nil {
103
105
return nil , fmt .Errorf ("flow directive target '%s' not found" , flowDirective .Value )
104
106
}
105
107
continue
106
108
}
107
109
108
- runner , err := NewTaskRunner (currentTask .Key , currentTask .Task , d . TaskSupport )
110
+ runner , err := NewTaskRunner (currentTask .Key , currentTask .Task , taskSupport . GetWorkflowDef () )
109
111
if err != nil {
110
112
return output , err
111
113
}
112
114
113
- d . TaskSupport .SetTaskStatus (currentTask .Key , ctx .RunningStatus )
114
- if output , err = d .runTask (input , runner , currentTask .Task .GetBase ()); err != nil {
115
- d . TaskSupport .SetTaskStatus (currentTask .Key , ctx .FaultedStatus )
115
+ taskSupport .SetTaskStatus (currentTask .Key , ctx .RunningStatus )
116
+ if output , err = d .runTask (input , taskSupport , runner , currentTask .Task .GetBase ()); err != nil {
117
+ taskSupport .SetTaskStatus (currentTask .Key , ctx .FaultedStatus )
116
118
return output , err
117
119
}
118
120
119
- d . TaskSupport .SetTaskStatus (currentTask .Key , ctx .CompletedStatus )
121
+ taskSupport .SetTaskStatus (currentTask .Key , ctx .CompletedStatus )
120
122
input = deepCloneValue (output )
121
- idx , currentTask = tasks .Next (idx )
123
+ idx , currentTask = d . TaskList .Next (idx )
122
124
}
123
125
124
126
return output , nil
125
127
}
126
128
127
- func (d * DoTaskRunner ) shouldRunTask (input interface {}, task * model.TaskItem ) (bool , error ) {
129
+ func (d * DoTaskRunner ) shouldRunTask (input interface {}, taskSupport TaskSupport , task * model.TaskItem ) (bool , error ) {
128
130
if task .GetBase ().If != nil {
129
- output , err := traverseAndEvaluateBool (task .GetBase ().If .String (), input , d . TaskSupport .GetContext ())
131
+ output , err := traverseAndEvaluateBool (task .GetBase ().If .String (), input , taskSupport .GetContext ())
130
132
if err != nil {
131
133
return false , model .NewErrExpression (err , task .Key )
132
134
}
@@ -135,15 +137,15 @@ func (d *DoTaskRunner) shouldRunTask(input interface{}, task *model.TaskItem) (b
135
137
return true , nil
136
138
}
137
139
138
- func (d * DoTaskRunner ) evaluateSwitchTask (input interface {}, taskKey string , switchTask * model.SwitchTask ) (* model.FlowDirective , error ) {
140
+ func (d * DoTaskRunner ) evaluateSwitchTask (input interface {}, taskSupport TaskSupport , taskKey string , switchTask * model.SwitchTask ) (* model.FlowDirective , error ) {
139
141
var defaultThen * model.FlowDirective
140
142
for _ , switchItem := range switchTask .Switch {
141
143
for _ , switchCase := range switchItem {
142
144
if switchCase .When == nil {
143
145
defaultThen = switchCase .Then
144
146
continue
145
147
}
146
- result , err := traverseAndEvaluateBool (model .NormalizeExpr (switchCase .When .String ()), input , d . TaskSupport .GetContext ())
148
+ result , err := traverseAndEvaluateBool (model .NormalizeExpr (switchCase .When .String ()), input , taskSupport .GetContext ())
147
149
if err != nil {
148
150
return nil , model .NewErrExpression (err , taskKey )
149
151
}
@@ -162,86 +164,86 @@ func (d *DoTaskRunner) evaluateSwitchTask(input interface{}, taskKey string, swi
162
164
}
163
165
164
166
// runTask executes an individual task.
165
- func (d * DoTaskRunner ) runTask (input interface {}, runner TaskRunner , task * model.TaskBase ) (output interface {}, err error ) {
167
+ func (d * DoTaskRunner ) runTask (input interface {}, taskSupport TaskSupport , runner TaskRunner , task * model.TaskBase ) (output interface {}, err error ) {
166
168
taskName := runner .GetTaskName ()
167
169
168
- d . TaskSupport .SetTaskStartedAt (time .Now ())
169
- d . TaskSupport .SetTaskRawInput (input )
170
- d . TaskSupport .SetTaskName (taskName )
170
+ taskSupport .SetTaskStartedAt (time .Now ())
171
+ taskSupport .SetTaskRawInput (input )
172
+ taskSupport .SetTaskName (taskName )
171
173
172
174
if task .Input != nil {
173
- if input , err = d .processTaskInput (task , input , taskName ); err != nil {
175
+ if input , err = d .processTaskInput (task , input , taskSupport ); err != nil {
174
176
return nil , err
175
177
}
176
178
}
177
179
178
- output , err = runner .Run (input )
180
+ output , err = runner .Run (input , taskSupport )
179
181
if err != nil {
180
182
return nil , err
181
183
}
182
184
183
- d . TaskSupport .SetTaskRawOutput (output )
185
+ taskSupport .SetTaskRawOutput (output )
184
186
185
- if output , err = d .processTaskOutput (task , output , taskName ); err != nil {
187
+ if output , err = d .processTaskOutput (task , output , taskSupport ); err != nil {
186
188
return nil , err
187
189
}
188
190
189
- if err = d .processTaskExport (task , output , taskName ); err != nil {
191
+ if err = d .processTaskExport (task , output , taskSupport ); err != nil {
190
192
return nil , err
191
193
}
192
194
193
195
return output , nil
194
196
}
195
197
196
198
// processTaskInput processes task input validation and transformation.
197
- func (d * DoTaskRunner ) processTaskInput (task * model.TaskBase , taskInput interface {}, taskName string ) (output interface {}, err error ) {
199
+ func (d * DoTaskRunner ) processTaskInput (task * model.TaskBase , taskInput interface {}, taskSupport TaskSupport ) (output interface {}, err error ) {
198
200
if task .Input == nil {
199
201
return taskInput , nil
200
202
}
201
203
202
- if err = validateSchema (taskInput , task .Input .Schema , taskName ); err != nil {
204
+ if err = validateSchema (taskInput , task .Input .Schema , d . GetTaskName () ); err != nil {
203
205
return nil , err
204
206
}
205
207
206
- if output , err = traverseAndEvaluate (task .Input .From , taskInput , taskName , d . TaskSupport .GetContext ()); err != nil {
208
+ if output , err = traverseAndEvaluate (task .Input .From , taskInput , d . GetTaskName (), taskSupport .GetContext ()); err != nil {
207
209
return nil , err
208
210
}
209
211
210
212
return output , nil
211
213
}
212
214
213
215
// processTaskOutput processes task output validation and transformation.
214
- func (d * DoTaskRunner ) processTaskOutput (task * model.TaskBase , taskOutput interface {}, taskName string ) (output interface {}, err error ) {
216
+ func (d * DoTaskRunner ) processTaskOutput (task * model.TaskBase , taskOutput interface {}, taskSupport TaskSupport ) (output interface {}, err error ) {
215
217
if task .Output == nil {
216
218
return taskOutput , nil
217
219
}
218
220
219
- if output , err = traverseAndEvaluate (task .Output .As , taskOutput , taskName , d . TaskSupport .GetContext ()); err != nil {
221
+ if output , err = traverseAndEvaluate (task .Output .As , taskOutput , d . GetTaskName (), taskSupport .GetContext ()); err != nil {
220
222
return nil , err
221
223
}
222
224
223
- if err = validateSchema (output , task .Output .Schema , taskName ); err != nil {
225
+ if err = validateSchema (output , task .Output .Schema , d . GetTaskName () ); err != nil {
224
226
return nil , err
225
227
}
226
228
227
229
return output , nil
228
230
}
229
231
230
- func (d * DoTaskRunner ) processTaskExport (task * model.TaskBase , taskOutput interface {}, taskName string ) (err error ) {
232
+ func (d * DoTaskRunner ) processTaskExport (task * model.TaskBase , taskOutput interface {}, taskSupport TaskSupport ) (err error ) {
231
233
if task .Export == nil {
232
234
return nil
233
235
}
234
236
235
- output , err := traverseAndEvaluate (task .Export .As , taskOutput , taskName , d . TaskSupport .GetContext ())
237
+ output , err := traverseAndEvaluate (task .Export .As , taskOutput , d . GetTaskName (), taskSupport .GetContext ())
236
238
if err != nil {
237
239
return err
238
240
}
239
241
240
- if err = validateSchema (output , task .Export .Schema , taskName ); err != nil {
242
+ if err = validateSchema (output , task .Export .Schema , d . GetTaskName () ); err != nil {
241
243
return nil
242
244
}
243
245
244
- d . TaskSupport .SetWorkflowInstanceCtx (output )
246
+ taskSupport .SetWorkflowInstanceCtx (output )
245
247
246
248
return nil
247
249
}
0 commit comments