File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed
Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments