forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
61 lines (46 loc) · 1.62 KB
/
base.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
package waits
import (
"time"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
)
// the base of all wait types
type baseWait struct {
}
// Timeout would return the timeout of this wait for wait types that do that
func (w *baseWait) Timeout() *int { return nil }
// TimeoutOn would return when this wait times out for wait types that do that
func (w *baseWait) TimeoutOn() *time.Time { return nil }
// Begin beings waiting
func (w *baseWait) Begin(run flows.FlowRun) {}
// base of all wait types than can timeout
type baseTimeoutWait struct {
baseWait
Timeout_ *int `json:"timeout,omitempty"`
TimeoutOn_ *time.Time `json:"timeout_on,omitempty"`
}
// Timeout returns the timeout of this wait in seconds or nil if no timeout is set
func (w *baseTimeoutWait) Timeout() *int { return w.Timeout_ }
// TimeoutOn returns when this wait times out
func (w *baseTimeoutWait) TimeoutOn() *time.Time { return w.TimeoutOn_ }
// Begin beings waiting at this wait
func (w *baseTimeoutWait) Begin(run flows.FlowRun) {
if w.Timeout_ != nil {
timeoutOn := time.Now().UTC().Add(time.Second * time.Duration(*w.Timeout_))
w.TimeoutOn_ = &timeoutOn
}
w.baseWait.Begin(run)
}
// CanResume returns true if a wait timed out event has been received
func (w *baseTimeoutWait) CanResume(callerEvents []flows.Event) bool {
return containsEventOfType(callerEvents, events.TypeWaitTimedOut)
}
// utility function to look for an event of a given type
func containsEventOfType(events []flows.Event, eventType string) bool {
for _, event := range events {
if event.Type() == eventType {
return true
}
}
return false
}