-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.go
114 lines (94 loc) · 1.82 KB
/
runner.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
package main
import (
"log"
"sync"
"time"
)
// A Session is ready when all it's windows are ready.
// A window is ready when all it's panes are ready.
// A pane is ready when the readycheck is successful.
// A pane without a readycheck is always ready.
type Object struct {
Name string
ReadyCheck struct {
Test string
Interval time.Duration
Retries int
}
DependsOn []string `yaml:"depends_on"`
mutex sync.Mutex
ready bool
}
var allRunners []Runner
var byName = make(map[string]Runner)
type Runner interface {
GetObject() *Object
DependenciesReady() bool
IsReady() bool
MarkReady()
DoReadyCheck()
Run()
}
func (o *Object) GetObject() *Object {
return o
}
func (o *Object) DependenciesReady() bool {
for _, name := range o.DependsOn {
other := byName[name]
if !other.IsReady() {
return false
}
}
return true
}
func (o *Object) IsReady() bool {
o.mutex.Lock()
defer o.mutex.Unlock()
return o.ready
}
func (o *Object) MarkReady() {
o.mutex.Lock()
o.ready = true
o.mutex.Unlock()
}
func addRunner(r Runner) {
allRunners = append(allRunners, r)
name := r.GetObject().Name
if name == "" {
return
}
if _, ok := byName[name]; ok {
log.Fatalf("Duplicate name: '%s'", name)
}
byName[name] = r
}
func (o *Object) Validate() {
for _, name := range o.DependsOn {
if _, ok := byName[name]; !ok {
log.Fatalf("Dependency does not exist: %s", name)
}
}
}
func validateDependencies() {
for _, r := range allRunners {
r.GetObject().Validate()
}
}
func runAll() {
validateDependencies()
var wg sync.WaitGroup
for _, r := range allRunners {
// Don't use loop variables in goroutine
wg.Add(1)
go func(r Runner) {
for !r.DependenciesReady() {
time.Sleep(10 * time.Millisecond)
}
r.Run()
r.DoReadyCheck()
r.MarkReady()
wg.Done()
}(r)
}
wg.Wait()
}