-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathnode.go
451 lines (388 loc) · 8.73 KB
/
node.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
package astiencoder
import (
"context"
"sort"
"sync"
"time"
"github.com/asticode/go-astitools/stat"
"github.com/asticode/go-astitools/worker"
)
// Node represents a node
type Node interface {
NodeChild
NodeDescriptor
NodeParent
Starter
Stater
}
// NodeDescriptor represents an object that can describe a node
type NodeDescriptor interface {
Metadata() NodeMetadata
}
// NodeMetadata represents node metadata
type NodeMetadata struct {
Description string
Label string
Name string
}
// NodeChild represents an object with parent nodes
type NodeChild interface {
AddParent(n Node)
ParentIsStarted(m NodeMetadata)
ParentIsStopped(m NodeMetadata)
Parents() []Node
}
// NodeParent represents an object with child nodes
type NodeParent interface {
AddChild(n Node)
ChildIsStarted(m NodeMetadata)
ChildIsStopped(m NodeMetadata)
Children() []Node
}
// Statuses
const (
StatusPaused = "paused"
StatusRunning = "running"
StatusStopped = "stopped"
)
// Starter represents an object that can start/pause/continue/stop
type Starter interface {
Continue()
Pause()
Start(ctx context.Context, t CreateTaskFunc)
Status() string
Stop()
}
// Stater represents an object that can return its stater
type Stater interface {
Stater() *astistat.Stater
}
// ConnectNodes connects 2 nodes
func ConnectNodes(parent, child Node) {
parent.AddChild(child)
child.AddParent(parent)
}
// BaseNode represents a base node
type BaseNode struct {
cancel context.CancelFunc
cancelPause context.CancelFunc
children map[string]Node
childrenStarted map[string]bool
ctx context.Context
ctxPause context.Context
e EmitEventFunc
m *sync.Mutex
md NodeMetadata
oStart *sync.Once
oStop *sync.Once
parents map[string]Node
parentsStarted map[string]bool
s *astistat.Stater
status string
}
// NewBaseNode creates a new base node
func NewBaseNode(e EmitEventFunc, m NodeMetadata) (n *BaseNode) {
n = &BaseNode{
children: make(map[string]Node),
childrenStarted: make(map[string]bool),
m: &sync.Mutex{},
e: e,
md: m,
oStart: &sync.Once{},
oStop: &sync.Once{},
parents: make(map[string]Node),
parentsStarted: make(map[string]bool),
status: StatusStopped,
}
if e != nil {
n.s = astistat.NewStater(2*time.Second, n.statsHandleFunc)
}
return
}
// Context returns the node context
func (n *BaseNode) Context() context.Context {
return n.ctx
}
// CreateTaskFunc is a method that can create a task
type CreateTaskFunc func() *astiworker.Task
// BaseNodeStartFunc represents a node start func
type BaseNodeStartFunc func()
// BaseNodeExecFunc represents a node exec func
type BaseNodeExecFunc func(t *astiworker.Task)
// Status implements the Starter interface
func (n *BaseNode) Status() string {
n.m.Lock()
defer n.m.Unlock()
return n.status
}
// Start starts the node
func (n *BaseNode) Start(ctx context.Context, tc CreateTaskFunc, execFunc BaseNodeExecFunc) {
// Make sure the node can only be started once
n.oStart.Do(func() {
// Check context
if ctx.Err() != nil {
return
}
// Create task
t := tc()
// Reset context
n.ctx, n.cancel = context.WithCancel(ctx)
// Reset once
n.oStop = &sync.Once{}
// Loop through children
for _, c := range n.Children() {
c.ParentIsStarted(n.md)
}
// Loop through parents
for _, p := range n.Parents() {
p.ChildIsStarted(n.md)
}
// Update status
n.m.Lock()
n.status = StatusRunning
n.m.Unlock()
// Send event
if n.e != nil {
n.e(Event{
Name: EventNameNodeStarted,
Payload: n.md.Name,
})
}
// Execute the rest in a goroutine
go func() {
// Task is done
defer t.Done()
// Send event
if n.e != nil {
defer n.e(Event{
Name: EventNameNodeStopped,
Payload: n.md.Name,
})
}
// Make sure the status is updated once everything is done
defer func() {
n.m.Lock()
defer n.m.Unlock()
n.status = StatusStopped
}()
// Let children and parents know the node is stopped
defer func() {
// Loop through children
for _, c := range n.Children() {
c.ParentIsStopped(n.md)
}
// Loop through parents
for _, p := range n.Parents() {
p.ChildIsStopped(n.md)
}
}()
// Make sure the node is stopped properly
defer n.Stop()
// Handle the stater
if n.s != nil {
// Make sure the stater is stopped properly
defer n.s.Stop()
// Start stater
n.s.Start(n.ctx)
}
// Exec func
execFunc(t)
}()
})
}
// Stop stops the node
func (n *BaseNode) Stop() {
// Make sure the node can only be stopped once
n.oStop.Do(func() {
// Cancel context
if n.cancel != nil {
n.cancel()
}
// Reset once
n.oStart = &sync.Once{}
})
}
// Pause implements the Starter interface
func (n *BaseNode) Pause() {
// Status is not running
if n.Status() != StatusRunning {
return
}
// Reset ctx
n.ctxPause, n.cancelPause = context.WithCancel(n.ctx)
// Update status
n.m.Lock()
n.status = StatusPaused
n.m.Unlock()
// Send event
if n.e != nil {
n.e(Event{
Name: EventNameNodePaused,
Payload: n.md.Name,
})
}
}
// Continue implements the Starter interface
func (n *BaseNode) Continue() {
// Status is not paused
if n.Status() != StatusPaused {
return
}
// Cancel ctx
if n.cancelPause != nil {
n.cancelPause()
}
// Update status
n.m.Lock()
n.status = StatusRunning
n.m.Unlock()
// Send event
if n.e != nil {
n.e(Event{
Name: EventNameNodeContinued,
Payload: n.md.Name,
})
}
}
// HandlePause handles the pause
func (n *BaseNode) HandlePause() {
// Status is not paused
if n.Status() != StatusPaused {
return
}
// Wait for ctx to be done
<-n.ctxPause.Done()
}
// AddChild implements the NodeParent interface
func (n *BaseNode) AddChild(i Node) {
n.m.Lock()
defer n.m.Unlock()
if _, ok := n.children[i.Metadata().Name]; ok {
return
}
n.children[i.Metadata().Name] = i
}
// ChildIsStarted implements the NodeParent interface
func (n *BaseNode) ChildIsStarted(m NodeMetadata) {
n.m.Lock()
defer n.m.Unlock()
if _, ok := n.children[m.Name]; !ok {
return
}
n.childrenStarted[m.Name] = true
}
// ChildIsStopped implements the NodeParent interface
func (n *BaseNode) ChildIsStopped(m NodeMetadata) {
n.m.Lock()
defer n.m.Unlock()
if _, ok := n.children[m.Name]; !ok {
return
}
delete(n.childrenStarted, m.Name)
if len(n.childrenStarted) == 0 {
n.Stop()
}
}
// Children implements the NodeParent interface
func (n *BaseNode) Children() (ns []Node) {
n.m.Lock()
defer n.m.Unlock()
var ks []string
for k := range n.children {
ks = append(ks, k)
}
sort.Strings(ks)
for _, k := range ks {
ns = append(ns, n.children[k])
}
return
}
// AddParent implements the NodeChild interface
func (n *BaseNode) AddParent(i Node) {
n.m.Lock()
defer n.m.Unlock()
if _, ok := n.parents[i.Metadata().Name]; ok {
return
}
n.parents[i.Metadata().Name] = i
}
// ParentIsStarted implements the NodeChild interface
func (n *BaseNode) ParentIsStarted(m NodeMetadata) {
n.m.Lock()
defer n.m.Unlock()
if _, ok := n.parents[m.Name]; !ok {
return
}
n.parentsStarted[m.Name] = true
}
// ParentIsStopped implements the NodeChild interface
func (n *BaseNode) ParentIsStopped(m NodeMetadata) {
n.m.Lock()
defer n.m.Unlock()
if _, ok := n.parents[m.Name]; !ok {
return
}
delete(n.parentsStarted, m.Name)
if len(n.parentsStarted) == 0 {
n.Stop()
}
}
// Parents implements the NodeChild interface
func (n *BaseNode) Parents() (ns []Node) {
n.m.Lock()
defer n.m.Unlock()
var ks []string
for k := range n.parents {
ks = append(ks, k)
}
sort.Strings(ks)
for _, k := range ks {
ns = append(ns, n.parents[k])
}
return
}
// Metadata implements the Node interface
func (n *BaseNode) Metadata() NodeMetadata {
return n.md
}
// Stater returns the node stater
func (n *BaseNode) Stater() *astistat.Stater {
return n.s
}
// EventStats represents stats event
type EventStats struct {
Name string `json:"name"`
Stats []EventStat `json:"stats"`
}
// EventStat represents a stat event
type EventStat struct {
Description string `json:"description"`
Label string `json:"label"`
Unit string `json:"unit"`
Value interface{} `json:"value"`
}
func (n *BaseNode) statsHandleFunc(stats []astistat.Stat) {
// No stats
if len(stats) == 0 {
return
}
// Create event
e := EventStats{
Name: n.md.Name,
Stats: []EventStat{},
}
// Loop through stats
for _, s := range stats {
e.Stats = append(e.Stats, EventStat{
Description: s.Description,
Label: s.Label,
Unit: s.Unit,
Value: s.Value,
})
}
// Send event
n.e(Event{
Name: EventNameStats,
Payload: e,
})
}