Skip to content

Commit 9a6ea6d

Browse files
authored
Flag for inverting task failure status (#20)
1 parent f40932c commit 9a6ea6d

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ linters:
33
disable:
44
- gomnd
55
- lll
6+
- maligned
67
- wsl
78

89
issues:

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ The `-backoff` flag is used with `-sleep`, and will double the time delay betwee
125125
> $ retry -sleep=15s -backoff wget https://example.com
126126
> ```
127127
128+
### Invert status
129+
130+
The `-invert` flag is used to flip a task's failure status. Successful task runs will become failures, and vice versa. Useful for when you want to retry a command until it fails.
131+
132+
> Run `curl https://example.com/health`, a maximum of 20 times, until it becomes unresponsive.
133+
>
134+
> ```bash
135+
> $ retry -attempts=20 -invert curl https://example.com/health
136+
> ```
137+
128138
### Random jitter
129139
130140
The `-jitter` flag adds a random time range to the sleep duration. Jitter added on top of exponential backoff.

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func main() {
3131
flag.IntVar(&flags.Attempts, "attempts", 3, "maximum number of attempts")
3232
flag.BoolVar(&flags.Backoff, "backoff", false, "use exponential backoff when sleeping")
3333
flag.IntVar(&flags.Consecutive, "consecutive", 0, "required number of back to back successes")
34+
flag.BoolVar(&flags.Invert, "invert", false, "wait for task to fail rather than succeed")
3435
flag.DurationVar(&flags.Jitter, "jitter", 0, "time range randomly added to sleep")
3536
flag.DurationVar(&flags.TotalTime, "max-time", time.Minute, "maximum total time")
3637
flag.BoolVar(&flags.quiet, "quiet", false, "silence all output")

retry/retry.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ type Spec struct {
3636
// row in order for the task to be considered successful overall.
3737
Consecutive int
3838

39+
// Invert is used to indicate that the task success status should be
40+
//reversed. Failed tasks count as successful, and vice versa.
41+
Invert bool
42+
3943
// Jitter is the duration range to randomly add to the Sleep time.
4044
// Sleep + [0, Jitter)
4145
Jitter time.Duration
@@ -70,7 +74,7 @@ func Retry(spec Spec, task Task) error {
7074
case <-ctxMaxTime.Done():
7175
return ErrExceededTime
7276
case err := <-runnerChan(ctxMaxTask, task):
73-
if err != nil {
77+
if err != nil != spec.Invert {
7478
// Task failed, so drop the number of consecutive successful
7579
// runs back down to zero.
7680
consecutive = 0

retry/retry_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ func TestRetry(t *testing.T) {
4545
failed: true,
4646
duration: 0,
4747
},
48+
{
49+
title: "succeed invert",
50+
task: ExecTask{Name: "false"},
51+
spec: Spec{
52+
Consecutive: 3,
53+
Invert: true,
54+
},
55+
results: []result{
56+
{elapsed: 0, failed: true},
57+
{elapsed: 0, failed: true},
58+
{elapsed: 0, failed: true},
59+
},
60+
failed: false,
61+
duration: 0,
62+
},
63+
{
64+
title: "fail invert",
65+
task: ExecTask{Name: "true"},
66+
spec: Spec{
67+
Attempts: 3,
68+
Invert: true,
69+
},
70+
results: []result{
71+
{elapsed: 0, failed: false},
72+
{elapsed: 0, failed: false},
73+
{elapsed: 0, failed: false},
74+
},
75+
failed: true,
76+
duration: 0,
77+
},
4878
{
4979
title: "succeed slow with task time",
5080
task: ExecTask{Name: "sleep", Args: []string{"2"}},

0 commit comments

Comments
 (0)