-
Notifications
You must be signed in to change notification settings - Fork 234
/
monitor.go
253 lines (213 loc) · 6.19 KB
/
monitor.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
package deployment
import (
"context"
"errors"
"reflect"
"time"
"github.com/hashicorp/go-multierror"
"github.com/superfly/flyctl/api"
)
var (
ErrNoDeployment = errors.New("No deployment available to monitor")
errDeploymentNotReady = errors.New("Deployment not ready to monitor")
errDeploymentComplete = errors.New("Deployment is already complete")
)
func NewDeploymentMonitor(client *api.Client, appID string, evaluationID string) *DeploymentMonitor {
return &DeploymentMonitor{
AppID: appID,
evaluationID: evaluationID,
client: client,
}
}
type DeploymentMonitor struct {
AppID string
evaluationID string
client *api.Client
err error
successCount int
failureCount int
DeploymentStarted func(idx int, deployment *api.DeploymentStatus) error
DeploymentUpdated func(deployment *api.DeploymentStatus, updatedAllocs []*api.AllocationStatus) error
DeploymentFailed func(deployment *api.DeploymentStatus, failedAllocs []*api.AllocationStatus) error
DeploymentSucceeded func(deployment *api.DeploymentStatus) error
}
var pollInterval = 750 * time.Millisecond
func (dm *DeploymentMonitor) start(ctx context.Context) <-chan *deploymentStatus {
statusCh := make(chan *deploymentStatus)
go func() {
var currentDeployment *deploymentStatus
defer func() {
if currentDeployment != nil {
currentDeployment.Close()
}
close(statusCh)
}()
currentID := ""
prevID := ""
num := 0
startTime := time.Now()
var delay time.Duration
processFn := func() error {
reqCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
deployment, err := dm.client.GetDeploymentStatus(reqCtx, dm.AppID, currentID, dm.evaluationID)
if err != nil {
if ctxErr := reqCtx.Err(); ctxErr != nil {
return ctxErr
}
return err
}
// wait for a deployment for up to 30 seconds. Could be due to a delay submitting the job or because
// there is no active deployment
if deployment == nil {
if time.Now().After(startTime.Add(5 * time.Minute)) {
// nothing to show after 5 minutes, break
return ErrNoDeployment
}
return errDeploymentNotReady
}
if dm.evaluationID == "" && deployment.ID == prevID {
// deployment already done, bail
return errDeploymentComplete
}
if dm.evaluationID == "" && currentDeployment == nil && !deployment.InProgress {
// wait for deployment (new deployment not yet created)
return errDeploymentNotReady
}
// this is a new deployment
if currentDeployment == nil {
currentDeployment = newDeploymentStatus(deployment)
num++
currentDeployment.number = num
statusCh <- currentDeployment
currentID = deployment.ID
}
currentDeployment.Update(deployment)
if !deployment.InProgress && currentDeployment != nil {
// deployment is complete, close out and reset for next iteration
currentDeployment.Close()
if deployment.Successful {
dm.successCount++
} else {
dm.failureCount++
}
currentDeployment = nil
prevID = currentID
currentID = ""
return errDeploymentComplete
}
return nil
}
for {
select {
case <-time.After(delay):
switch err := processFn(); err {
case nil:
// we're still monitoring, ensure the poll interval is > 0 and continue
delay = pollInterval
case errDeploymentComplete:
// we're done, exit
return
case errDeploymentNotReady:
// we're waiting for a deployment, set the poll interval to a small value and continue
delay = pollInterval / 2
case context.DeadlineExceeded:
delay = pollInterval
default:
dm.err = multierror.Append(err)
return
}
case <-ctx.Done():
if ctx.Err() != nil {
dm.err = multierror.Append(dm.err, ctx.Err())
}
return
}
}
}()
return statusCh
}
func (dm *DeploymentMonitor) Success() bool {
return dm.failureCount == 0
}
func (dm *DeploymentMonitor) Failed() bool {
return dm.failureCount > 0
}
func (dm *DeploymentMonitor) Error() error {
return dm.err
}
func (dm *DeploymentMonitor) Start(ctx context.Context) {
for deployment := range dm.start(ctx) {
if dm.DeploymentStarted != nil {
if err := dm.DeploymentStarted(deployment.number, deployment.deployment); err != nil {
dm.err = multierror.Append(dm.err, err)
return
}
}
for updatedAllocs := range deployment.update {
if dm.DeploymentUpdated != nil {
if err := dm.DeploymentUpdated(deployment.deployment, updatedAllocs); err != nil {
dm.err = multierror.Append(dm.err, err)
return
}
}
}
if deployment.deployment.Successful {
if dm.DeploymentSucceeded != nil {
if err := dm.DeploymentSucceeded(deployment.deployment); err != nil {
dm.err = multierror.Append(dm.err, err)
return
}
}
} else {
if dm.DeploymentFailed != nil {
if err := dm.DeploymentFailed(deployment.deployment, deployment.FailingAllocs()); err != nil {
dm.err = multierror.Append(dm.err, err)
return
}
}
}
}
}
type deploymentStatus struct {
number int
deployment *api.DeploymentStatus
allocStatus map[string]*api.AllocationStatus
update chan []*api.AllocationStatus
}
func newDeploymentStatus(deployment *api.DeploymentStatus) *deploymentStatus {
return &deploymentStatus{
deployment: deployment,
allocStatus: map[string]*api.AllocationStatus{},
update: make(chan []*api.AllocationStatus),
}
}
func (ds *deploymentStatus) Update(updatedDeployment *api.DeploymentStatus) {
if reflect.DeepEqual(ds.deployment, updatedDeployment) {
return
}
// deployment data has changed, cache & forward the updates
ds.deployment = updatedDeployment
updatedAllocs := []*api.AllocationStatus{}
for _, aNew := range updatedDeployment.Allocations {
aPrev, ok := ds.allocStatus[aNew.ID]
if ok && reflect.DeepEqual(aNew, aPrev) {
continue
}
ds.allocStatus[aNew.ID] = aNew
updatedAllocs = append(updatedAllocs, aNew)
}
ds.update <- updatedAllocs
}
func (ds *deploymentStatus) Close() {
close(ds.update)
}
func (dm *deploymentStatus) FailingAllocs() []*api.AllocationStatus {
out := []*api.AllocationStatus{}
for _, alloc := range dm.allocStatus {
if !alloc.Healthy || alloc.Status == "failed" {
out = append(out, alloc)
}
}
return out
}