|
| 1 | +package machine |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/superfly/fly-go/flaps" |
| 13 | + "github.com/superfly/flyctl/internal/appconfig" |
| 14 | + "github.com/superfly/flyctl/internal/command" |
| 15 | + "github.com/superfly/flyctl/internal/flag" |
| 16 | + "github.com/superfly/flyctl/internal/flapsutil" |
| 17 | + "github.com/superfly/flyctl/iostreams" |
| 18 | +) |
| 19 | + |
| 20 | +func newWait() *cobra.Command { |
| 21 | + const ( |
| 22 | + short = "Wait for a machine to reach a state" |
| 23 | + long = short + "\n" |
| 24 | + |
| 25 | + usage = "wait [id]" |
| 26 | + ) |
| 27 | + |
| 28 | + cmd := command.New(usage, short, long, runMachineWait, |
| 29 | + command.RequireSession, |
| 30 | + command.LoadAppNameIfPresent, |
| 31 | + ) |
| 32 | + |
| 33 | + cmd.Args = cobra.RangeArgs(0, 1) |
| 34 | + |
| 35 | + flag.Add( |
| 36 | + cmd, |
| 37 | + flag.App(), |
| 38 | + flag.AppConfig(), |
| 39 | + selectFlag, |
| 40 | + flag.String{ |
| 41 | + Name: "state", |
| 42 | + Description: "Machine state to wait for", |
| 43 | + Default: "settled", |
| 44 | + }, |
| 45 | + flag.Duration{ |
| 46 | + Name: "wait-timeout", |
| 47 | + Shorthand: "w", |
| 48 | + Description: "Time duration to wait for the machine to reach the requested state.", |
| 49 | + Default: 5 * time.Minute, |
| 50 | + }, |
| 51 | + ) |
| 52 | + |
| 53 | + return cmd |
| 54 | +} |
| 55 | + |
| 56 | +func runMachineWait(ctx context.Context) error { |
| 57 | + var ( |
| 58 | + io = iostreams.FromContext(ctx) |
| 59 | + desiredState = flag.GetString(ctx, "state") |
| 60 | + waitTimeout = flag.GetDuration(ctx, "wait-timeout") |
| 61 | + ) |
| 62 | + |
| 63 | + if desiredState == "" { |
| 64 | + return fmt.Errorf("state cannot be empty") |
| 65 | + } |
| 66 | + |
| 67 | + machineID := flag.FirstArg(ctx) |
| 68 | + haveMachineID := len(flag.Args(ctx)) > 0 |
| 69 | + machine, ctx, err := selectOneMachine(ctx, "", machineID, haveMachineID) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + appName := appconfig.NameFromContext(ctx) |
| 75 | + client := flapsutil.ClientFromContext(ctx) |
| 76 | + |
| 77 | + fmt.Fprintf(io.Out, "Waiting up to %s for machine %s to reach %q...\n", waitTimeout, machine.ID, desiredState) |
| 78 | + |
| 79 | + startedWaitAt := time.Now() |
| 80 | + const maxAttempts = 3 |
| 81 | + |
| 82 | + for attempt := 1; attempt <= maxAttempts; attempt++ { |
| 83 | + remainingTimeout := waitTimeout |
| 84 | + if waitTimeout > 0 { |
| 85 | + remainingTimeout = waitTimeout - time.Since(startedWaitAt) |
| 86 | + if remainingTimeout <= 0 { |
| 87 | + return fmt.Errorf("machine %s did not reach %q within %s", machine.ID, desiredState, waitTimeout) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + err = client.Wait(ctx, appName, machine, desiredState, remainingTimeout) |
| 92 | + if err == nil { |
| 93 | + break |
| 94 | + } |
| 95 | + |
| 96 | + if attempt == maxAttempts || !isRetryableWaitError(err) { |
| 97 | + return fmt.Errorf("machine %s did not reach %q within %s: %w", machine.ID, desiredState, waitTimeout, err) |
| 98 | + } |
| 99 | + |
| 100 | + fmt.Fprintf(io.Out, "Retrying wait for machine %s due to transient error: %v\n", machine.ID, err) |
| 101 | + |
| 102 | + machine, err = client.Get(ctx, appName, machine.ID) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("machine %s could not be refetched before retrying wait: %w", machine.ID, err) |
| 105 | + } |
| 106 | + |
| 107 | + retryDelay := retryDelayForAttempt(attempt) |
| 108 | + if waitTimeout > 0 { |
| 109 | + remainingTimeout = waitTimeout - time.Since(startedWaitAt) |
| 110 | + if remainingTimeout <= 0 { |
| 111 | + return fmt.Errorf("machine %s did not reach %q within %s", machine.ID, desiredState, waitTimeout) |
| 112 | + } |
| 113 | + if retryDelay > remainingTimeout { |
| 114 | + retryDelay = remainingTimeout |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + select { |
| 119 | + case <-time.After(retryDelay): |
| 120 | + case <-ctx.Done(): |
| 121 | + return ctx.Err() |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if desiredState == "settled" { |
| 126 | + machine, err = client.Get(ctx, appName, machine.ID) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("machine %s reached settled state but could not fetch final state: %w", machine.ID, err) |
| 129 | + } |
| 130 | + desiredState = machine.State |
| 131 | + } |
| 132 | + |
| 133 | + fmt.Fprintf(io.Out, "Machine %s reached state %q\n", machine.ID, desiredState) |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func isRetryableWaitError(err error) bool { |
| 139 | + if err == nil { |
| 140 | + return false |
| 141 | + } |
| 142 | + |
| 143 | + message := strings.ToLower(err.Error()) |
| 144 | + if strings.Contains(message, "currently replaced") { |
| 145 | + return true |
| 146 | + } |
| 147 | + |
| 148 | + var flapsErr *flaps.FlapsError |
| 149 | + if errors.As(err, &flapsErr) { |
| 150 | + if flapsErr.ResponseStatusCode == http.StatusTooManyRequests || (flapsErr.ResponseStatusCode >= 500 && flapsErr.ResponseStatusCode < 600) { |
| 151 | + return true |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + transientSubstrings := []string{ |
| 156 | + "connection reset by peer", |
| 157 | + "connection refused", |
| 158 | + "network is unreachable", |
| 159 | + "temporary failure in name resolution", |
| 160 | + "i/o timeout", |
| 161 | + "timeout", |
| 162 | + "eof", |
| 163 | + } |
| 164 | + |
| 165 | + for _, s := range transientSubstrings { |
| 166 | + if strings.Contains(message, s) { |
| 167 | + return true |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return false |
| 172 | +} |
| 173 | + |
| 174 | +func retryDelayForAttempt(attempt int) time.Duration { |
| 175 | + if attempt <= 0 { |
| 176 | + return 500 * time.Millisecond |
| 177 | + } |
| 178 | + |
| 179 | + delay := 500 * time.Millisecond |
| 180 | + for i := 1; i < attempt; i++ { |
| 181 | + delay *= 2 |
| 182 | + } |
| 183 | + |
| 184 | + return delay |
| 185 | +} |
0 commit comments