executeWithRetry calls doExecute, retrying up to retryMaxAttempts times on failure.
(output io.Writer)
| 93 | |
| 94 | // executeWithRetry calls doExecute, retrying up to retryMaxAttempts times on failure. |
| 95 | func (e *executor) executeWithRetry(output io.Writer) error { |
| 96 | if e.retryMaxAttempts <= 1 { |
| 97 | return e.doExecute(output) |
| 98 | } |
| 99 | |
| 100 | var lastErr error |
| 101 | for attempt := 1; attempt <= e.retryMaxAttempts; attempt++ { |
| 102 | if attempt > 1 { |
| 103 | // Reset buffer, to make sure the output buffer is clean after new retry. |
| 104 | if r, ok := output.(resetable); ok { |
| 105 | r.Reset() |
| 106 | } |
| 107 | } |
| 108 | lastErr = e.doExecute(output) |
| 109 | if lastErr == nil { |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | color.Printf(color.Warning, "Attempted (%d/%d): %v\n", attempt, e.retryMaxAttempts, lastErr) |
| 114 | if attempt < e.retryMaxAttempts { |
| 115 | time.Sleep(time.Duration(attempt) * time.Second) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Show mesage without "[]". |
| 120 | e.title = strings.Trim(e.title, "[]") |
| 121 | return fmt.Errorf("%s failed after %d attempts -> %w", e.title, e.retryMaxAttempts, lastErr) |
| 122 | } |
| 123 | |
| 124 | // createLogFile creates and initializes a log file with environment variables and command info. |
| 125 | func (e *executor) createLogFile(cmd *exec.Cmd) (*os.File, error) { |
no test coverage detected