Skip to content

Commit 78a4fef

Browse files
authored
TWEAK: add discard max retry middleware (#48)
1 parent 78bb48d commit 78a4fef

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

middleware/discard/max_retry.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package discard
2+
3+
import (
4+
"github.com/taylorchu/work"
5+
)
6+
7+
// MaxRetry discards a job if its retry count is over limit.
8+
func MaxRetry(n int64) work.HandleMiddleware {
9+
return func(f work.HandleFunc) work.HandleFunc {
10+
return func(job *work.Job, opt *work.DequeueOptions) error {
11+
err := f(job, opt)
12+
if job.Retries >= n {
13+
return work.ErrUnrecoverable
14+
}
15+
return err
16+
}
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package discard
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
"github.com/taylorchu/work"
9+
)
10+
11+
func TestMaxRetry(t *testing.T) {
12+
job := work.NewJob()
13+
opt := &work.DequeueOptions{
14+
Namespace: "n1",
15+
QueueID: "q1",
16+
}
17+
d := MaxRetry(1)
18+
h := d(func(*work.Job, *work.DequeueOptions) error {
19+
return errors.New("no reason")
20+
})
21+
22+
err := h(job, opt)
23+
require.Error(t, err)
24+
require.NotEqual(t, work.ErrUnrecoverable, err)
25+
26+
job.Retries = 1
27+
err = h(job, opt)
28+
require.Equal(t, work.ErrUnrecoverable, err)
29+
}

0 commit comments

Comments
 (0)