-
Notifications
You must be signed in to change notification settings - Fork 239
/
wait.go
49 lines (43 loc) · 1.05 KB
/
wait.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
package machine
import (
"context"
"errors"
"fmt"
"time"
"github.com/jpillora/backoff"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/flaps"
)
func WaitForStartOrStop(ctx context.Context, machine *api.Machine, action string, timeout time.Duration) error {
var flapsClient = flaps.FromContext(ctx)
waitCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
var waitOnAction string
switch action {
case "start":
waitOnAction = "started"
case "stop":
waitOnAction = "stopped"
default:
return fmt.Errorf("action must be either start or stop")
}
b := &backoff.Backoff{
Min: 500 * time.Millisecond,
Max: 2 * time.Second,
Factor: 2,
Jitter: false,
}
for {
err := flapsClient.Wait(waitCtx, machine, waitOnAction, 60*time.Second)
switch {
case errors.Is(err, context.Canceled):
return err
case errors.Is(err, context.DeadlineExceeded):
return fmt.Errorf("timeout reached waiting for machine to %s %w", waitOnAction, err)
case err != nil:
time.Sleep(b.Duration())
continue
}
return nil
}
}