-
Notifications
You must be signed in to change notification settings - Fork 221
/
wait.go
90 lines (78 loc) · 2.3 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
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
package machine
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/jpillora/backoff"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/internal/flyerr"
)
func WaitForStartOrStop(ctx context.Context, machine *fly.Machine, action string, timeout time.Duration) error {
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 invalidAction
}
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)
if err == nil {
return nil
}
switch {
case errors.Is(waitCtx.Err(), context.Canceled):
return err
case errors.Is(waitCtx.Err(), context.DeadlineExceeded):
return WaitTimeoutErr{
machineID: machine.ID,
timeout: timeout,
desiredState: waitOnAction,
}
default:
var flapsErr *flaps.FlapsError
if strings.Contains(err.Error(), "machine failed to reach desired state") && machine.Config.Restart != nil && machine.Config.Restart.Policy == fly.MachineRestartPolicyNo {
return fmt.Errorf("machine failed to reach desired start state, and restart policy was set to %s restart", machine.Config.Restart.Policy)
}
if errors.As(err, &flapsErr) && flapsErr.ResponseStatusCode == http.StatusBadRequest {
return fmt.Errorf("failed waiting for machine: %w", err)
}
time.Sleep(b.Duration())
}
}
}
type WaitTimeoutErr struct {
machineID string
timeout time.Duration
desiredState string
}
func (e WaitTimeoutErr) Error() string {
return "timeout reached waiting for machine's state to change"
}
func (e WaitTimeoutErr) Description() string {
return fmt.Sprintf("The machine %s took more than %s to reach \"%s\"", e.machineID, e.timeout, e.desiredState)
}
func (e WaitTimeoutErr) DesiredState() string {
return e.desiredState
}
var invalidAction flyerr.GenericErr = flyerr.GenericErr{
Err: "action must be either start or stop",
Descript: "",
Suggest: "This is a bug in wait function, please report this at https://community.fly.io",
DocUrl: "",
}