Run the function with stats and retries if required
(Retry bool, showStats bool, cmd *cobra.Command, f func() error)
| 238 | |
| 239 | // Run the function with stats and retries if required |
| 240 | func Run(Retry bool, showStats bool, cmd *cobra.Command, f func() error) { |
| 241 | ctx := context.Background() |
| 242 | ci := fs.GetConfig(ctx) |
| 243 | var cmdErr error |
| 244 | stopStats := func() {} |
| 245 | if !showStats && ShowStats() { |
| 246 | showStats = true |
| 247 | } |
| 248 | if ci.Progress { |
| 249 | stopStats = startProgress() |
| 250 | } else if showStats { |
| 251 | stopStats = StartStats() |
| 252 | } |
| 253 | SigInfoHandler() |
| 254 | for try := 1; try <= ci.Retries; try++ { |
| 255 | cmdErr = f() |
| 256 | cmdErr = fs.CountError(ctx, cmdErr) |
| 257 | lastErr := accounting.GlobalStats().GetLastError() |
| 258 | if cmdErr == nil { |
| 259 | cmdErr = lastErr |
| 260 | } |
| 261 | if !Retry || !accounting.GlobalStats().Errored() { |
| 262 | if try > 1 { |
| 263 | fs.Errorf(nil, "Attempt %d/%d succeeded", try, ci.Retries) |
| 264 | } |
| 265 | break |
| 266 | } |
| 267 | if accounting.GlobalStats().HadFatalError() { |
| 268 | fs.Errorf(nil, "Fatal error received - not attempting retries") |
| 269 | break |
| 270 | } |
| 271 | if accounting.GlobalStats().Errored() && !accounting.GlobalStats().HadRetryError() { |
| 272 | fs.Errorf(nil, "Can't retry any of the errors - not attempting retries") |
| 273 | break |
| 274 | } |
| 275 | if retryAfter := accounting.GlobalStats().RetryAfter(); !retryAfter.IsZero() { |
| 276 | d := time.Until(retryAfter) |
| 277 | if d > 0 { |
| 278 | fs.Logf(nil, "Received retry after error - sleeping until %s (%v)", retryAfter.Format(time.RFC3339Nano), d) |
| 279 | time.Sleep(d) |
| 280 | } |
| 281 | } |
| 282 | if lastErr != nil { |
| 283 | fs.Errorf(nil, "Attempt %d/%d failed with %d errors and: %v", try, ci.Retries, accounting.GlobalStats().GetErrors(), lastErr) |
| 284 | } else { |
| 285 | fs.Errorf(nil, "Attempt %d/%d failed with %d errors", try, ci.Retries, accounting.GlobalStats().GetErrors()) |
| 286 | } |
| 287 | if try < ci.Retries { |
| 288 | accounting.GlobalStats().ResetErrors() |
| 289 | } |
| 290 | if ci.RetriesInterval > 0 { |
| 291 | time.Sleep(time.Duration(ci.RetriesInterval)) |
| 292 | } |
| 293 | } |
| 294 | stopStats() |
| 295 | if showStats && (accounting.GlobalStats().Errored() || *statsInterval > 0) { |
| 296 | accounting.GlobalStats().Log() |
| 297 | } |
no test coverage detected