forked from fschnko/repeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretry.go
More file actions
30 lines (24 loc) · 765 Bytes
/
retry.go
File metadata and controls
30 lines (24 loc) · 765 Bytes
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
// Copyright © 2017 Artem Feschenko. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repeat
import (
"context"
"github.com/fschnko/errpack"
)
// Retry performs a rerun based on the configuration of the runner
// until it succeeds or stops the runner.
// Returns the success flag and error packet, if any.
func Retry(ctx context.Context, callback func() error, opts ...OptFunc) (bool, error) {
r := NewRunner(ctx, opts...)
errp := errpack.New("retry")
for r.Next() {
err := r.Execute(callback)
if err == nil {
return true, errp.ErrorOrNil()
}
errp.Add(err)
}
errp.Add(&ExecuteError{Count: r.Count(), Reason: r.Reason()})
return false, errp.ErrorOrNil()
}