forked from vijaynalawade/flogo-contrib
/
taskbehavior.go
253 lines (184 loc) · 6.24 KB
/
taskbehavior.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package simple
import (
"github.com/TIBCOSoftware/flogo-contrib/action/flow/definition"
"github.com/TIBCOSoftware/flogo-contrib/action/flow/model"
)
////////////////////////////////////////////////////////////////////////////////////////////////////////
// TaskBehavior implements model.TaskBehavior
type TaskBehavior struct {
}
// Enter implements model.TaskBehavior.Enter
func (tb *TaskBehavior) Enter(ctx model.TaskContext) (enterResult model.EnterResult) {
task := ctx.Task()
log.Debugf("Enter Task '%s'", task.ID())
ctx.SetStatus(model.TaskStatusEntered)
//check if all predecessor links are done
linkInsts := ctx.GetFromLinkInstances()
ready := true
skipped := false
if len(linkInsts) == 0 {
// has no predecessor links, so task is ready
ready = true
} else {
skipped = true
log.Debugf("Task '%s' has %d incoming Links", task.ID(), len(linkInsts))
for _, linkInst := range linkInsts {
log.Debugf("Task '%s': Link from Task '%s' has status '%s'", task.ID(), linkInst.Link().FromTask().ID(), linkStatus(linkInst))
if linkInst.Status() < model.LinkStatusFalse {
ready = false
break
} else if linkInst.Status() == model.LinkStatusTrue {
skipped = false
}
}
}
if ready {
if skipped {
ctx.SetStatus(model.TaskStatusSkipped)
return model.ENTER_SKIP
} else {
log.Debugf("Task '%s' Ready", ctx.Task().ID())
ctx.SetStatus(model.TaskStatusReady)
}
return model.ENTER_EVAL
} else {
log.Debugf("Task '%s' Not Ready", ctx.Task().ID())
}
return model.ENTER_NOTREADY
}
// Eval implements model.TaskBehavior.Eval
func (tb *TaskBehavior) Eval(ctx model.TaskContext) (evalResult model.EvalResult, err error) {
if ctx.Status() == model.TaskStatusSkipped {
return model.EVAL_SKIP, nil
}
task := ctx.Task()
log.Debugf("Eval Task '%s'", task.ID())
done, err := ctx.EvalActivity()
if err != nil {
log.Errorf("Error evaluating activity '%s'[%s] - %s", ctx.Task().ID(), ctx.Task().ActivityConfig().Ref(), err.Error())
ctx.SetStatus(model.TaskStatusFailed)
return model.EVAL_FAIL, err
}
if done {
evalResult = model.EVAL_DONE
} else {
evalResult = model.EVAL_WAIT
}
return evalResult, nil
}
// PostEval implements model.TaskBehavior.PostEval
func (tb *TaskBehavior) PostEval(ctx model.TaskContext) (evalResult model.EvalResult, err error) {
log.Debugf("PostEval Task '%s'", ctx.Task().ID())
_, err = ctx.PostEvalActivity()
//what to do if eval isn't "done"?
if err != nil {
log.Errorf("Error post evaluating activity '%s'[%s] - %s", ctx.Task().ID(), ctx.Task().ActivityConfig().Ref(), err.Error())
ctx.SetStatus(model.TaskStatusFailed)
return model.EVAL_FAIL, err
}
return model.EVAL_DONE, nil
}
// Done implements model.TaskBehavior.Done
func (tb *TaskBehavior) Done(ctx model.TaskContext) (notifyFlow bool, taskEntries []*model.TaskEntry, err error) {
linkInsts := ctx.GetToLinkInstances()
numLinks := len(linkInsts)
if ctx.Status() != model.TaskStatusDone {
ctx.SetStatus(model.TaskStatusDone)
}
log.Debugf("Task '%s' is done", ctx.Task().ID())
// process outgoing links
if numLinks > 0 {
taskEntries = make([]*model.TaskEntry, 0, numLinks)
log.Debugf("Task '%s' has %d outgoing links", ctx.Task().ID(), numLinks)
for _, linkInst := range linkInsts {
follow := true
if linkInst.Link().Type() == definition.LtError {
//todo should we skip or ignore?
continue
}
if linkInst.Link().Type() == definition.LtExpression {
//todo handle error
log.Debugf("Task '%s': Evaluating Outgoing Expression Link to Task '%s'", ctx.Task().ID(), linkInst.Link().ToTask().ID())
follow, err = ctx.EvalLink(linkInst.Link())
if err != nil {
return false, nil, err
}
}
if follow {
linkInst.SetStatus(model.LinkStatusTrue)
log.Debugf("Task '%s': Following Link to task '%s'", ctx.Task().ID(), linkInst.Link().ToTask().ID())
taskEntry := &model.TaskEntry{Task: linkInst.Link().ToTask()}
taskEntries = append(taskEntries, taskEntry)
} else {
linkInst.SetStatus(model.LinkStatusFalse)
taskEntry := &model.TaskEntry{Task: linkInst.Link().ToTask()}
taskEntries = append(taskEntries, taskEntry)
}
}
//continue on to successor tasks
return false, taskEntries, nil
}
log.Debugf("Notifying flow that end task '%s' is done", ctx.Task().ID())
// there are no outgoing links, so just notify parent that we are done
return true, nil, nil
}
// Done implements model.TaskBehavior.Skip
func (tb *TaskBehavior) Skip(ctx model.TaskContext) (notifyFlow bool, taskEntries []*model.TaskEntry) {
linkInsts := ctx.GetToLinkInstances()
numLinks := len(linkInsts)
ctx.SetStatus(model.TaskStatusSkipped)
log.Debugf("Task '%s' was skipped", ctx.Task().ID())
// process outgoing links
if numLinks > 0 {
taskEntries = make([]*model.TaskEntry, 0, numLinks)
log.Debugf("Task '%s' has %d outgoing links", ctx.Task().ID(), numLinks)
for _, linkInst := range linkInsts {
linkInst.SetStatus(model.LinkStatusSkipped)
taskEntry := &model.TaskEntry{Task: linkInst.Link().ToTask()}
taskEntries = append(taskEntries, taskEntry)
}
return false, taskEntries
}
log.Debugf("Notifying flow that end task '%s' is skipped", ctx.Task().ID())
return true, nil
}
// Done implements model.TaskBehavior.Error
func (tb *TaskBehavior) Error(ctx model.TaskContext, err error) (handled bool, taskEntries []*model.TaskEntry) {
linkInsts := ctx.GetToLinkInstances()
numLinks := len(linkInsts)
handled = false
// process outgoing links
if numLinks > 0 {
for _, linkInst := range linkInsts {
if linkInst.Link().Type() == definition.LtError {
handled = true
}
break
}
if handled {
taskEntries = make([]*model.TaskEntry, 0, numLinks)
for _, linkInst := range linkInsts {
if linkInst.Link().Type() == definition.LtError {
linkInst.SetStatus(model.LinkStatusTrue)
} else {
linkInst.SetStatus(model.LinkStatusFalse)
}
taskEntry := &model.TaskEntry{Task: linkInst.Link().ToTask()}
taskEntries = append(taskEntries, taskEntry)
}
return true, taskEntries
}
}
return false, nil
}
func linkStatus(inst model.LinkInstance) string {
switch inst.Status() {
case model.LinkStatusFalse:
return "false"
case model.LinkStatusTrue:
return "true"
case model.LinkStatusSkipped:
return "skipped"
}
return "unknown"
}