-
Notifications
You must be signed in to change notification settings - Fork 18
/
node.go
575 lines (492 loc) · 11.5 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
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
package astiencoder
import (
"context"
"sort"
"sync"
"github.com/asticode/go-astikit"
)
// Node represents a node
type Node interface {
Closer
NodeChild
NodeDescriptor
NodeParent
Starter
}
// 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
Tags []string
}
// Extend extends the node metadata
func (m NodeMetadata) Extend(name, label, description string, tags ...string) NodeMetadata {
if len(m.Description) == 0 {
m.Description = description
}
if len(m.Label) == 0 {
m.Label = label
}
if len(m.Name) == 0 {
m.Name = name
}
for _, t := range tags {
var found bool
for _, ft := range m.Tags {
if ft == t {
found = true
break
}
}
if !found {
m.Tags = append(m.Tags, t)
}
}
return m
}
// NodeChild represents an object with parent nodes
type NodeChild interface {
NodeChildMapper
ParentIsStarted(m NodeMetadata)
ParentIsStopped(m NodeMetadata)
}
// NodeChildMapper represents an object that can play with its parents map
type NodeChildMapper interface {
AddParent(n Node)
DelParent(n Node)
Parents() []Node
}
// NodeParent represents an object with child nodes
type NodeParent interface {
NodeParentMapper
ChildIsStarted(m NodeMetadata)
ChildIsStopped(m NodeMetadata)
}
// NodeParentMapper represents an object that can play with its children map
type NodeParentMapper interface {
AddChild(n Node)
Children() []Node
DelChild(n Node)
}
// Statuses
const (
StatusCreated = "created"
StatusPaused = "paused"
StatusRunning = "running"
StatusStopped = "stopped"
)
type Closer interface {
AddClose(astikit.CloseFunc)
AddCloseWithError(astikit.CloseFuncWithError)
Close() error
DoWhenUnclosed(func())
IsClosed() bool
NewChildCloser() *astikit.Closer
}
// Starter represents an object that can start/pause/continue/stop
type Starter interface {
Continue()
Pause()
Start(ctx context.Context, t CreateTaskFunc)
Status() string
Stop()
}
// ConnectNodes connects 2 nodes
func ConnectNodes(parent, child Node) {
parent.AddChild(child)
child.AddParent(parent)
}
// DisconnectNodes disconnects 2 nodes
func DisconnectNodes(parent, child Node) {
parent.DelChild(child)
child.DelParent(parent)
}
// NodeOptions represents node options
type NodeOptions struct {
AutoStop *NodeAutoStopOptions
Metadata NodeMetadata
}
type NodeAutoStopOptions struct {
WhenAllChildrenAreStopped bool
WhenAllParentsAreStopped bool
}
// BaseNode represents a base node
type BaseNode struct {
c *astikit.Closer
cancel context.CancelFunc
cancelPause context.CancelFunc
children map[string]Node
childrenStarted map[string]bool
ctx context.Context
ctxPause context.Context
eh *EventHandler
et EventTypeTransformer
o NodeOptions
m *sync.Mutex
oStart *sync.Once
oStop *sync.Once
parents map[string]Node
parentsStarted map[string]bool
s *Stater
ss []astikit.StatOptions
status string
target interface{}
}
// NewBaseNode creates a new base node
func NewBaseNode(o NodeOptions, c *astikit.Closer, eh *EventHandler, s *Stater, target interface{}, et EventTypeTransformer) (n *BaseNode) {
// Create node
n = &BaseNode{
c: c.NewChild(),
children: make(map[string]Node),
childrenStarted: make(map[string]bool),
m: &sync.Mutex{},
eh: eh,
et: et,
o: o,
oStart: &sync.Once{},
oStop: &sync.Once{},
parents: make(map[string]Node),
parentsStarted: make(map[string]bool),
s: s,
status: StatusCreated,
target: target,
}
// Set closer callback
n.c.OnClosed(func(err error) {
eh.Emit(Event{
Name: et(EventTypeClosed),
Target: target,
})
})
return
}
func (n *BaseNode) AddClose(fn astikit.CloseFunc) {
n.c.Add(fn)
}
func (n *BaseNode) AddCloseWithError(fn astikit.CloseFuncWithError) {
n.c.AddWithError(fn)
}
func (n *BaseNode) NewChildCloser() *astikit.Closer {
return n.c.NewChild()
}
func (n *BaseNode) Close() error {
return n.c.Close()
}
func (n *BaseNode) IsClosed() bool {
return n.c.IsClosed()
}
func (n *BaseNode) DoWhenUnclosed(fn func()) {
n.c.Do(fn)
}
// 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() *astikit.Task
// BaseNodeStartFunc represents a node start func
type BaseNodeStartFunc func()
// BaseNodeExecFunc represents a node exec func
type BaseNodeExecFunc func(t *astikit.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.o.Metadata)
}
// Loop through parents
for _, p := range n.Parents() {
p.ChildIsStarted(n.o.Metadata)
}
// Update status
n.m.Lock()
n.status = StatusRunning
n.m.Unlock()
// Send started event
n.eh.Emit(Event{
Name: n.et(EventTypeStarted),
Target: n.target,
})
// Execute the rest in a goroutine
go func() {
// Task is done
defer t.Done()
// Send stopped event
defer n.eh.Emit(Event{
Name: n.et(EventTypeStopped),
Target: n.target,
})
// 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.o.Metadata)
}
// Loop through parents
for _, p := range n.Parents() {
p.ChildIsStopped(n.o.Metadata)
}
}()
// Make sure the node is stopped properly
defer n.Stop()
// Handle stats
if n.s != nil {
// Make sure to delete stats
defer func() {
// Lock
n.m.Lock()
defer n.m.Unlock()
// Delete stats
n.s.DelStats(n.target, n.ss...)
}()
// Add stats
n.m.Lock()
n.s.AddStats(n.target, n.ss...)
n.m.Unlock()
}
// 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()
n.cancel = nil
}
// Cancel pause context as well
// We need to cancel stop context before cancelling pause context for the following workflow:
// - in its loop, node handles pause before checking stop ctx
// - in that case, we want the stop ctx to be accurate once the pause ctx is done
if n.cancelPause != nil {
n.cancelPause()
n.cancelPause = nil
}
// Reset once
n.oStart = &sync.Once{}
})
}
// Pause implements the Starter interface
func (n *BaseNode) Pause() {
n.pauseFunc(func() {
n.ctxPause, n.cancelPause = context.WithCancel(n.ctx)
})
}
// Pause implements the Starter interface
func (n *BaseNode) pauseFunc(fn func()) {
// Status is not running
if n.Status() != StatusRunning {
return
}
// Callback
fn()
// Update status
n.m.Lock()
n.status = StatusPaused
n.m.Unlock()
// Send paused event
n.eh.Emit(Event{
Name: n.et(EventTypePaused),
Target: n.target,
})
}
// Continue implements the Starter interface
func (n *BaseNode) Continue() {
n.continueFunc(func() {
if n.cancelPause != nil {
n.cancelPause()
n.cancelPause = nil
}
})
}
func (n *BaseNode) continueFunc(fn func()) {
// Status is not paused
if n.Status() != StatusPaused {
return
}
// Callback
fn()
// Update status
n.m.Lock()
n.status = StatusRunning
n.m.Unlock()
// Send continued event
n.eh.Emit(Event{
Name: n.et(EventTypeContinued),
Target: n.target,
})
}
// 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) {
// Lock
n.m.Lock()
// Node doesn't exist
if _, ok := n.children[i.Metadata().Name]; ok {
n.m.Unlock()
return
}
// Add child
n.children[i.Metadata().Name] = i
// Unlock
n.m.Unlock()
// Send event
// Mutex should be unlocked at this point
n.eh.Emit(Event{
Name: n.et(EventTypeChildAdded),
Payload: i,
Target: n.target,
})
}
// DelChild implements the NodeParent interface
func (n *BaseNode) DelChild(i Node) {
// Lock
n.m.Lock()
// Delete child
delete(n.children, i.Metadata().Name)
// Unlock
n.m.Unlock()
// Send event
// Mutex should be unlocked at this point
n.eh.Emit(Event{
Name: n.et(EventTypeChildRemoved),
Payload: i,
Target: n.target,
})
}
// 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.o.AutoStop == nil || n.o.AutoStop.WhenAllChildrenAreStopped) {
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
}
// DelParent implements the NodeChild interface
func (n *BaseNode) DelParent(i Node) {
n.m.Lock()
defer n.m.Unlock()
delete(n.parents, i.Metadata().Name)
}
// 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.o.AutoStop == nil || n.o.AutoStop.WhenAllParentsAreStopped) {
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.o.Metadata
}
// AddStats adds stats
func (n *BaseNode) AddStats(ss ...astikit.StatOptions) {
n.m.Lock()
defer n.m.Unlock()
n.ss = append(n.ss, ss...)
}