-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstate.go
More file actions
215 lines (189 loc) · 5.08 KB
/
state.go
File metadata and controls
215 lines (189 loc) · 5.08 KB
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
package fleet
import (
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/coreos/fleet/schema"
)
// waitForUnitSubStates polls each of the indicated jobs until each of their
// systemd substates is equal to that which the caller indicates
func waitForUnitSubStates(units []string, desiredState string) (outchan chan *schema.Unit, errchan chan error) {
var wg sync.WaitGroup
errchan = make(chan error)
outchan = make(chan *schema.Unit)
// check each unit for desired state
for _, name := range units {
wg.Add(1)
go checkUnitSubState(name, desiredState, outchan, errchan, &wg)
}
// wait for all jobs to complete
go func() {
wg.Wait()
close(outchan)
close(errchan)
}()
return outchan, errchan
}
func checkUnitSubState(name string, desiredState string, outchan chan *schema.Unit, errchan chan error, wg *sync.WaitGroup) {
defer wg.Done()
for {
if assertUnitSubState(name, desiredState, outchan, errchan) {
return
}
time.Sleep(3000 * time.Millisecond)
}
}
func assertUnitSubState(name string, desiredState string, outchan chan *schema.Unit, errchan chan error) bool {
u, err := cAPI.Unit(name)
if err != nil {
errchan <- fmt.Errorf("Error retrieving Job(%s) from Registry: %v", name, err)
return false
}
// send unit across the output channel
outchan <- u
unitState, err := unitState(name)
if err != nil {
errchan <- fmt.Errorf("Error retrieving Unit state from Registry: %v", err)
return false
}
if unitState.SystemdSubState == desiredState {
return true
}
return false
}
// unitState retrieves a UnitState based on its name.
// FIXME: add this to fleet's API
func unitState(name string) (*schema.UnitState, error) {
unitStates, err := cAPI.UnitStates()
if err != nil {
return nil, err
}
for _, state := range unitStates {
if state.Name == name {
return state, nil
}
}
return nil, errors.New("Could not find unit state: " + name)
}
// waitForUnitStates polls each of the indicated jobs until each of their
// states is equal to that which the caller indicates
func waitForUnitStates(units []string, desiredState string) (outchan chan *schema.Unit, errchan chan error) {
var wg sync.WaitGroup
errchan = make(chan error)
outchan = make(chan *schema.Unit)
// check each unit for desired state
for _, name := range units {
wg.Add(1)
go checkUnitState(name, desiredState, outchan, errchan, &wg)
}
// wait for all jobs to complete
go func() {
wg.Wait()
close(outchan)
close(errchan)
}()
return outchan, errchan
}
func checkUnitState(name string, desiredState string, outchan chan *schema.Unit, errchan chan error, wg *sync.WaitGroup) {
defer wg.Done()
for {
if assertUnitState(name, desiredState, outchan, errchan) {
return
}
time.Sleep(3000 * time.Millisecond)
}
}
func assertUnitState(name string, desiredState string, outchan chan *schema.Unit, errchan chan error) bool {
u, err := cAPI.Unit(name)
if err != nil {
errchan <- fmt.Errorf("Error retrieving Job(%s) from Registry: %v", name, err)
return false
}
// send unit across the output channel
outchan <- u
// HACK: global units have no soul
if u.CurrentState == "" {
return true
}
if u.DesiredState == u.CurrentState {
return true
}
return false
}
func printUnitSubState(name string, outchan chan *schema.Unit, errchan chan error) error {
// print output while jobs are transitioning
defer fmt.Printf("\n")
for {
select {
case u := <-outchan:
// return on closed channel
if u == nil {
return nil
}
// ignore units that don't match our unit
if u.Name != name {
continue
}
// retrieve the unit's state
unitState, err := unitState(u.Name)
if err != nil {
return err
}
fmt.Printf("\033[0;33m%v:\033[0m %v \r",
u.Name, unitState.SystemdSubState)
// read from error channel
case err := <-errchan:
// continue processing if error channel closed
if err == nil {
continue
} else if strings.Contains(err.Error(), "timeout reached") {
// ignore intermittent timeout errors
continue
}
return err
}
time.Sleep(1000 * time.Millisecond)
}
}
func printUnitState(name string, outchan chan *schema.Unit, errchan chan error) error {
// print output while jobs are transitioning
defer fmt.Printf("\n")
for {
select {
case u := <-outchan:
// return on closed channel
if u == nil {
return nil
}
// ignore units that don't match our unit
if u.Name != name {
continue
}
// HACK: global units have no soul
if u.CurrentState == "" {
continue
}
// otherwise print output
if u.CurrentState != u.DesiredState {
fmt.Printf("\033[0;33m%v:\033[0m %v (pending) \r",
u.Name, u.CurrentState)
} else {
fmt.Printf("\033[0;33m%v:\033[0m %v \r",
u.Name, u.CurrentState)
}
// read from error channel
case err := <-errchan:
// continue processing if error channel closed
if err == nil {
continue
} else if strings.Contains(err.Error(), "timeout reached") {
// ignore intermittent timeout errors
continue
}
return err
}
time.Sleep(1000 * time.Millisecond)
}
}