-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathworkflow.go
213 lines (179 loc) · 3.8 KB
/
workflow.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
package astiencoder
import (
"context"
"sync"
"github.com/asticode/go-astilog"
"github.com/asticode/go-astitools/worker"
"github.com/pkg/errors"
)
// Errors
var (
ErrNodeNotFound = errors.New("node.not.found")
)
// Workflow represents a workflow
type Workflow struct {
bn *BaseNode
c *Closer
e EmitEventFunc
j Job
m *sync.Mutex
name string
ns map[string]Node
rootCtx context.Context
t *astiworker.Task
tf CreateTaskFunc
}
func newWorkflow(name string, j Job, rootCtx context.Context, e EmitEventFunc, tf CreateTaskFunc, c *Closer) *Workflow {
return &Workflow{
bn: NewBaseNode(nil, NodeMetadata{
Description: "root",
Label: "root",
Name: "root",
}),
c: c,
e: e,
j: j,
m: &sync.Mutex{},
name: name,
ns: make(map[string]Node),
rootCtx: rootCtx,
tf: tf,
}
}
// Closer returns the closer
func (w *Workflow) Closer() *Closer {
return w.c
}
// EmitEventFunc returns the emit event func
func (w *Workflow) EmitEventFunc() EmitEventFunc {
return w.e
}
func (w *Workflow) indexNodes() {
// Lock
w.m.Lock()
defer w.m.Unlock()
// Index
w.indexNodesFunc(w.bn.Children())
}
func (w *Workflow) indexNodesFunc(ns []Node) {
// Loop through nodes
for _, n := range ns {
// Add node
w.ns[n.Metadata().Name] = n
// Index children nodes
w.indexNodesFunc(n.Children())
}
}
// Start starts the workflow
func (w *Workflow) Start() {
w.start(w.nodes()...)
}
func (w *Workflow) start(ns ...Node) {
w.bn.Start(w.rootCtx, w.tf, func(t *astiworker.Task) {
// Log
astilog.Debugf("astiencoder: starting workflow %s", w.name)
// Store task
w.t = t
// Loop through nodes
for _, n := range ns {
w.startNode(n)
}
// Send event
w.e(Event{
Name: EventNameWorkflowStarted,
Payload: w.name,
})
// Wait for task to be done
t.Wait()
// Close
if err := w.c.Close(); err != nil {
w.e(EventError(errors.Wrapf(err, "astiencoder: closing workflow %s failed", w.name)))
}
// Send event
w.e(Event{
Name: EventNameWorkflowStopped,
Payload: w.name,
})
})
}
func (w *Workflow) startNode(n Node) {
n.Start(w.bn.Context(), w.t.NewSubTask)
}
// Stop stops the workflow
func (w *Workflow) Stop() {
astilog.Debugf("astiencoder: stopping workflow %s", w.name)
w.bn.Stop()
}
// Pause pauses the workflow
func (w *Workflow) Pause() {
// Workflow is not running
if w.bn.Status() != StatusRunning {
return
}
// Pause
astilog.Debugf("astiencoder: pausing workflow %s", w.name)
for _, n := range w.nodes() {
n.Pause()
}
// Update status
w.bn.m.Lock()
w.bn.status = StatusPaused
w.bn.m.Unlock()
// Send event
w.e(Event{
Name: EventNameWorkflowPaused,
Payload: w.name,
})
}
// Continue continues the workflow
func (w *Workflow) Continue() {
// Workflow is not paused
if w.bn.Status() != StatusPaused {
return
}
// Continue
astilog.Debugf("astiencoder: continuing workflow %s", w.name)
for _, n := range w.nodes() {
n.Continue()
}
// Update status
w.bn.m.Lock()
w.bn.status = StatusRunning
w.bn.m.Unlock()
// Send event
w.e(Event{
Name: EventNameWorkflowStarted,
Payload: w.name,
})
}
// AddChild adds a child to the workflow
func (w *Workflow) AddChild(n Node) {
w.bn.AddChild(n)
}
// Children returns the workflow children
func (w *Workflow) Children() []Node {
return w.bn.Children()
}
// Status returns the workflow status
func (w *Workflow) Status() string {
return w.bn.Status()
}
// Node retrieves a node from the workflow
func (w *Workflow) Node(name string) (n Node, err error) {
w.m.Lock()
defer w.m.Unlock()
var ok bool
if n, ok = w.ns[name]; !ok {
err = ErrNodeNotFound
return
}
return
}
func (w *Workflow) nodes() (ns []Node) {
w.m.Lock()
defer w.m.Unlock()
for _, n := range w.ns {
ns = append(ns, n)
}
return
}