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