forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow.go
88 lines (68 loc) · 1.94 KB
/
flow.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
package waits
import (
"fmt"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/utils"
)
const TypeFlow string = "flow"
type FlowWait struct {
FlowUUID flows.FlowUUID `json:"flow_uuid" validate:"required,uuid4"`
}
func (w *FlowWait) Resolve(key string) interface{} {
switch key {
case "flow_uuid":
return w.FlowUUID
}
return nil
}
func (w *FlowWait) Default() interface{} {
return TypeFlow
}
func (w *FlowWait) String() string {
return TypeFlow
}
var _ utils.VariableResolver = (*FlowWait)(nil)
func (w *FlowWait) Type() string { return TypeFlow }
func (w *FlowWait) Begin(run flows.FlowRun, step flows.Step) error {
run.AddEvent(step, &events.FlowWaitEvent{FlowUUID: w.FlowUUID})
run.SetWait(w)
return nil
}
func (w *FlowWait) GetEndEvent(run flows.FlowRun, step flows.Step) (flows.Event, error) {
child := run.Child()
// we don't have a child, so stop execution
if child == nil {
return nil, nil
}
// child isn't complete yet, shouldn't end
if child.Status() == flows.StatusActive {
return nil, nil
}
// see if we already have an exit event on our step for this flow
evts := step.Events()
for i := len(evts) - 1; i >= 0; i-- {
evt := evts[i]
exit, isExit := evt.(*events.FlowExitedEvent)
if isExit && exit.FlowUUID == w.FlowUUID {
return exit, nil
}
}
// our flow didn't exit, return nil
return nil, nil
}
func (w *FlowWait) End(run flows.FlowRun, step flows.Step, event flows.Event) error {
flowEvent, isFlow := event.(*events.FlowExitedEvent)
if !isFlow {
return fmt.Errorf("must end flow wait with flow_exited event, got: %#v", event.Type())
}
// make sure the flows match
if flowEvent.FlowUUID != w.FlowUUID {
return fmt.Errorf("must end flow wait with flow_exited for the same flow, expected '%s', got '%s'", w.FlowUUID, flowEvent.FlowUUID)
}
// log this event
run.AddEvent(step, flowEvent)
// and clear our wait
run.SetWait(nil)
return nil
}