forked from project-flogo/flow
/
instances.go
600 lines (461 loc) · 15.2 KB
/
instances.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package instance
import (
"errors"
"fmt"
"github.com/qingcloudhx/core/support"
"github.com/qingcloudhx/core/support/log"
"github.com/qingcloudhx/flow/definition"
"github.com/qingcloudhx/flow/model"
flowsupport "github.com/qingcloudhx/flow/support"
)
type IndependentInstance struct {
*Instance
id string
stepID int
workItemQueue *support.SyncQueue //todo: change to faster non-threadsafe queue
wiCounter int
trackingChanges bool
ChangeTracker *InstanceChangeTracker
subFlowCtr int
flowModel *model.FlowModel
patch *flowsupport.Patch
interceptor *flowsupport.Interceptor
subFlows map[int]*Instance
}
// New creates a new Flow Instance from the specified Flow
func NewIndependentInstance(instanceID string, flowURI string, flow *definition.Definition, logger log.Logger) (*IndependentInstance, error) {
var err error
inst := &IndependentInstance{}
inst.Instance = &Instance{}
inst.master = inst
inst.id = instanceID
inst.stepID = 0
inst.workItemQueue = support.NewSyncQueue()
inst.flowDef = flow
inst.flowURI = flowURI
inst.flowModel, err = getFlowModel(flow)
if err != nil {
return nil, err
}
inst.logger = logger
inst.status = model.FlowStatusNotStarted
inst.ChangeTracker = NewInstanceChangeTracker()
inst.taskInsts = make(map[string]*TaskInst)
inst.linkInsts = make(map[int]*LinkInst)
logger.Infof("[flow] NewIndependentInstance flowDef:%+v", flow)
return inst, nil
}
func (inst *IndependentInstance) newEmbeddedInstance(taskInst *TaskInst, flowURI string, flow *definition.Definition) *Instance {
logger := inst.logger
logger.Infof("[flow] newEmbeddedInstance subFlowCtr:%d,flowURI:%s", inst.subFlowCtr, flowURI)
inst.subFlowCtr++
embeddedInst := &Instance{}
embeddedInst.subFlowId = inst.subFlowCtr
embeddedInst.master = inst
embeddedInst.host = taskInst
embeddedInst.flowDef = flow
embeddedInst.status = model.FlowStatusNotStarted
embeddedInst.taskInsts = make(map[string]*TaskInst)
embeddedInst.linkInsts = make(map[int]*LinkInst)
embeddedInst.flowURI = flowURI
embeddedInst.logger = inst.logger
if inst.subFlows == nil {
inst.subFlows = make(map[int]*Instance)
}
inst.subFlows[embeddedInst.subFlowId] = embeddedInst
inst.ChangeTracker.SubFlowChange(taskInst.flowInst.subFlowId, CtAdd, embeddedInst.subFlowId, "")
return embeddedInst
}
func (inst *IndependentInstance) startEmbedded(embedded *Instance, startAttrs map[string]interface{}) error {
if embedded.master != inst {
return errors.New("embedded instance is not from this independent instance")
}
//attrs := make(map[string]data.TypedValue, len(startAttrs))
//
//inputMd := embedded.flowDef.Metadata().Input
//
//for name, value := range startAttrs {
//
// if tv, exists := inputMd[name]; exists {
// attrs[name] = data.NewTypedValue(tv.Type(), value)
// }
//}
embedded.attrs = startAttrs
inst.startInstance(embedded)
return nil
}
func (inst *IndependentInstance) Start(startAttrs map[string]interface{}) bool {
md := inst.flowDef.Metadata()
if md != nil && md.Input != nil {
inst.attrs = make(map[string]interface{}, len(md.Input))
for name, value := range md.Input {
if value != nil {
inst.attrs[name] = value.Value()
} else {
inst.attrs[name] = nil
}
}
} else {
inst.attrs = make(map[string]interface{}, len(startAttrs))
}
for name, value := range startAttrs {
inst.attrs[name] = value
}
return inst.startInstance(inst.Instance)
}
func (inst *IndependentInstance) ApplyPatch(patch *flowsupport.Patch) {
if inst.patch == nil {
inst.patch = patch
inst.patch.Init()
}
}
func (inst *IndependentInstance) ApplyInterceptor(interceptor *flowsupport.Interceptor) {
if inst.interceptor == nil {
inst.interceptor = interceptor
inst.interceptor.Init()
}
}
// GetChanges returns the Change Tracker object
func (inst *IndependentInstance) GetChanges() *InstanceChangeTracker {
return inst.ChangeTracker
}
// ResetChanges resets an changes that were being tracked
func (inst *IndependentInstance) ResetChanges() {
if inst.ChangeTracker != nil {
inst.ChangeTracker.ResetChanges()
}
//todo: can we reuse this to avoid gc
inst.ChangeTracker = NewInstanceChangeTracker()
}
// StepID returns the current step ID of the Flow Instance
func (inst *IndependentInstance) StepID() int {
return inst.stepID
}
func (inst *IndependentInstance) DoStep() bool {
hasNext := false
inst.ResetChanges()
inst.stepID++
if inst.status == model.FlowStatusActive {
// get item to be worked on
item, ok := inst.workItemQueue.Pop()
if ok {
//dev logging
//logger.Debug("Retrieved item from Flow Instance work queue")
workItem := item.(*WorkItem)
// get the corresponding behavior
behavior := inst.flowModel.GetDefaultTaskBehavior()
if typeID := workItem.taskInst.task.TypeID(); typeID != "" {
behavior = inst.flowModel.GetTaskBehavior(typeID)
}
// track the fact that the work item was removed from the queue
inst.ChangeTracker.trackWorkItem(&WorkItemQueueChange{ChgType: CtDel, ID: workItem.ID, WorkItem: workItem})
inst.execTask(behavior, workItem.taskInst)
hasNext = true
} else {
// dev logging
//logger.Debug("Flow Instance work queue empty")
}
}
return hasNext
}
func (inst *IndependentInstance) scheduleEval(taskInst *TaskInst) {
inst.wiCounter++
workItem := NewWorkItem(inst.wiCounter, taskInst)
inst.logger.Debugf("Scheduling task '%s'", taskInst.task.ID())
inst.workItemQueue.Push(workItem)
// track the fact that the work item was added to the queue
inst.ChangeTracker.trackWorkItem(&WorkItemQueueChange{ChgType: CtAdd, ID: workItem.ID, WorkItem: workItem})
}
// execTask executes the specified Work Item of the Flow Instance
func (inst *IndependentInstance) execTask(behavior model.TaskBehavior, taskInst *TaskInst) {
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("unhandled Error executing task '%s' : %v", taskInst.task.ID(), r)
inst.logger.Error(err)
// todo: useful for debugging
//logger.Debugf("StackTrace: %s", debug.Stack())
if !taskInst.flowInst.isHandlingError {
taskInst.appendErrorData(NewActivityEvalError(taskInst.task.Name(), "unhandled", err.Error()))
inst.HandleGlobalError(taskInst.flowInst, err)
}
// else what should we do?
}
}()
var err error
var evalResult model.EvalResult
if taskInst.status == model.TaskStatusWaiting {
evalResult, err = behavior.PostEval(taskInst)
} else {
evalResult, err = behavior.Eval(taskInst)
}
if err != nil {
//taskInst.returnError = err
inst.handleTaskError(behavior, taskInst, err)
return
}
switch evalResult {
case model.EvalDone:
//taskInst.SetStatus(model.TaskStatusDone)
inst.handleTaskDone(behavior, taskInst)
case model.EvalSkip:
//taskInst.SetStatus(model.TaskStatusSkipped)
inst.handleTaskDone(behavior, taskInst)
case model.EvalWait:
taskInst.SetStatus(model.TaskStatusWaiting)
case model.EvalFail:
taskInst.SetStatus(model.TaskStatusFailed)
case model.EvalRepeat:
taskInst.SetStatus(model.TaskStatusReady)
//task needs to iterate or retry
inst.scheduleEval(taskInst)
}
}
// handleTaskDone handles the completion of a task in the Flow Instance
func (inst *IndependentInstance) handleTaskDone(taskBehavior model.TaskBehavior, taskInst *TaskInst) {
notifyFlow := false
var taskEntries []*model.TaskEntry
var err error
if taskInst.Status() == model.TaskStatusSkipped {
notifyFlow, taskEntries = taskBehavior.Skip(taskInst)
} else {
notifyFlow, taskEntries, err = taskBehavior.Done(taskInst)
}
containerInst := taskInst.flowInst
if err != nil {
taskInst.appendErrorData(err)
inst.HandleGlobalError(containerInst, err)
return
}
flowDone := false
task := taskInst.Task()
if notifyFlow {
flowBehavior := inst.flowModel.GetFlowBehavior()
flowDone = flowBehavior.TaskDone(containerInst)
}
if flowDone || containerInst.forceCompletion {
//flow completed or return was called explicitly, so lets complete the flow
flowBehavior := inst.flowModel.GetFlowBehavior()
flowBehavior.Done(containerInst)
flowDone = true
containerInst.SetStatus(model.FlowStatusCompleted)
if containerInst != inst.Instance {
//not top level flow so we have to schedule next step
// spawned from task instance
host, ok := containerInst.host.(*TaskInst)
if ok {
//if the flow failed, set the error
for name, value := range containerInst.returnData {
//todo review how we should handle an error encountered here
host.SetOutput(name, value)
}
inst.scheduleEval(host)
}
//if containerInst.isHandlingError {
// //was the error handler, so directly under instance
// host,ok := containerInst.host.(*EmbeddedInstance)
// if ok {
// host.SetStatus(model.FlowStatusCompleted)
// host.returnData = containerInst.returnData
// host.returnError = containerInst.returnError
// }
// //todo if not a task inst, what should we do?
//} else {
// // spawned from task instance
//
// //todo if not a task inst, what should we do?
//}
// flow has completed so remove it
delete(inst.subFlows, containerInst.subFlowId)
}
} else {
// not done, so enter tasks specified by the Done behavior call
err := inst.enterTasks(containerInst, taskEntries)
if err != nil {
//todo review how we should handle an error encountered here
log.RootLogger().Errorf("encountered error when entering tasks: %v", err)
}
}
// task is done, so we can release it
containerInst.releaseTask(task)
}
// handleTaskError handles the completion of a task in the Flow Instance
func (inst *IndependentInstance) handleTaskError(taskBehavior model.TaskBehavior, taskInst *TaskInst, err error) {
handled, taskEntries := taskBehavior.Error(taskInst, err)
containerInst := taskInst.flowInst
if handled {
//Add error details to scope
taskInst.setTaskError(err)
if len(taskEntries) != 0 {
err := inst.enterTasks(containerInst, taskEntries)
if err != nil {
//todo review how we should handle an error encountered here
log.RootLogger().Errorf("encountered error when entering tasks: %v", err)
}
}
containerInst.releaseTask(taskInst.Task())
} else {
if containerInst.isHandlingError {
//fail
inst.SetStatus(model.FlowStatusFailed)
} else {
taskInst.appendErrorData(err)
inst.HandleGlobalError(containerInst, err)
}
return
}
}
// HandleGlobalError handles instance errors
func (inst *IndependentInstance) HandleGlobalError(containerInst *Instance, err error) {
if containerInst.isHandlingError {
//todo: log error information
containerInst.SetStatus(model.FlowStatusFailed)
return
}
containerInst.isHandlingError = true
flowBehavior := inst.flowModel.GetFlowBehavior()
//not currently handling error, so check if it has an error handler
if containerInst.flowDef.GetErrorHandler() != nil {
// todo: should we clear out the existing workitem queue for items from containerInst?
//clear existing instances
inst.taskInsts = make(map[string]*TaskInst)
taskEntries := flowBehavior.StartErrorHandler(containerInst)
err := inst.enterTasks(containerInst, taskEntries)
if err != nil {
//todo review how we should handle an error encountered here
log.RootLogger().Errorf("encountered error when entering tasks: %v", err)
}
} else {
containerInst.SetStatus(model.FlowStatusFailed)
if containerInst != inst.Instance {
// spawned from task instance
host, ok := containerInst.host.(*TaskInst)
if ok {
behavior := inst.flowModel.GetDefaultTaskBehavior()
if typeID := host.task.TypeID(); typeID != "" {
behavior = inst.flowModel.GetTaskBehavior(typeID)
}
inst.handleTaskError(behavior, host, err)
//fail the task
//inst.scheduleEval(host)
}
} else {
inst.returnError = err
}
}
}
func (inst *IndependentInstance) startInstance(toStart *Instance) bool {
toStart.SetStatus(model.FlowStatusActive)
//if pi.Attrs == nil {
// pi.Attrs = make(map[string]*data.Attribute)
//}
//
//for _, attr := range startAttrs {
// pi.Attrs[attr.Name()] = attr
//}
//logger.Infof("FlowInstance Flow: %v", pi.FlowModel)
//need input mappings
flowBehavior := inst.flowModel.GetFlowBehavior()
if ok, taskEntries := flowBehavior.Start(toStart); ok {
err := inst.enterTasks(toStart, taskEntries)
if err != nil {
//todo review how we should handle an error encountered here
log.RootLogger().Errorf("encountered error when entering tasks: %v", err)
}
return true
} else {
return false
}
}
func (inst *IndependentInstance) enterTasks(activeInst *Instance, taskEntries []*model.TaskEntry) error {
logger := inst.logger
logger.Infof("[flow] enterTasks taskEntries:%+v", taskEntries)
for _, taskEntry := range taskEntries {
//logger.Debugf("EnterTask - TaskEntry: %v", taskEntry)
taskToEnterBehavior := inst.flowModel.GetTaskBehavior(taskEntry.Task.TypeID())
enterTaskData, _ := activeInst.FindOrCreateTaskData(taskEntry.Task)
enterResult := taskToEnterBehavior.Enter(enterTaskData)
if enterResult == model.EnterEval {
err := applySettingsMapper(enterTaskData)
if err != nil {
return err
}
inst.scheduleEval(enterTaskData)
} else if enterResult == model.EnterSkip {
//todo optimize skip, just keep skipping and don't schedule eval
inst.scheduleEval(enterTaskData)
}
}
return nil
}
//// Restart indicates that this FlowInstance was restarted
func (inst *IndependentInstance) Restart(id string, manager *flowsupport.FlowManager) error {
inst.id = id
var err error
inst.flowDef, err = manager.GetFlow(inst.flowURI)
if err != nil {
return err
}
if inst.flowDef == nil {
return errors.New("unable to resolve flow: " + inst.flowURI)
}
inst.flowModel, err = getFlowModel(inst.flowDef)
if err != nil {
return err
}
inst.master = inst
inst.init(inst.Instance)
return nil
}
func (inst *IndependentInstance) init(flowInst *Instance) {
for _, v := range flowInst.taskInsts {
v.flowInst = flowInst
v.task = flowInst.flowDef.GetTask(v.taskID)
}
for _, v := range flowInst.linkInsts {
v.flowInst = flowInst
v.link = flowInst.flowDef.GetLink(v.linkID)
}
}
//////////////////////////////////////////////////////////////////
// WorkItem describes an item of work (event for a Task) that should be executed on Step
type WorkItem struct {
ID int `json:"id"`
taskInst *TaskInst `json:"-"`
TaskID string `json:"taskID"`
SubFlowID int `json:"subFlowId"`
}
// NewWorkItem constructs a new WorkItem for the specified TaskInst
func NewWorkItem(id int, taskInst *TaskInst) *WorkItem {
var workItem WorkItem
workItem.ID = id
workItem.taskInst = taskInst
workItem.TaskID = taskInst.task.ID()
workItem.SubFlowID = taskInst.flowInst.subFlowId
return &workItem
}
func NewActivityEvalError(taskName string, errorType string, errorText string) *ActivityEvalError {
return &ActivityEvalError{taskName: taskName, errType: errorType, errText: errorText}
}
type ActivityEvalError struct {
taskName string
errType string
errText string
}
func (e *ActivityEvalError) TaskName() string {
return e.taskName
}
func (e *ActivityEvalError) Type() string {
return e.errType
}
func (e *ActivityEvalError) Error() string {
return e.errText
}
//////////////
// todo fix the following
func getFlowModel(flow *definition.Definition) (*model.FlowModel, error) {
if flow.ModelID() == "" {
return model.Default(), nil
}
return model.Get(flow.ModelID())
//todo if model not found, should throw error
}