Skip to content

Commit 32d20fd

Browse files
committed
feat: improve retry handling and logging in tasks
- Add RetryFactor, RetryMin, and RetryMax fields to NewMessage and NewTask functions - Update AllowOption struct to have public fields and adjust references in NewOptions function - Add test cases for RetryMin, RetryMax, and RetryFactor in job_test.go and option_test.go - Log retry remaining times and delay time in queue.go Signed-off-by: appleboy <[email protected]>
1 parent e0a1817 commit 32d20fd

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

job/job.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,27 @@ func NewMessage(m core.QueuedMessage, opts ...AllowOption) *Message {
6868
o := NewOptions(opts...)
6969

7070
return &Message{
71-
RetryCount: o.retryCount,
72-
RetryDelay: o.retryDelay,
73-
Timeout: o.timeout,
74-
Payload: m.Bytes(),
71+
RetryCount: o.retryCount,
72+
RetryDelay: o.retryDelay,
73+
RetryFactor: o.retryFactor,
74+
RetryMin: o.retryMin,
75+
RetryMax: o.retryMax,
76+
Timeout: o.timeout,
77+
Payload: m.Bytes(),
7578
}
7679
}
7780

7881
func NewTask(task TaskFunc, opts ...AllowOption) *Message {
7982
o := NewOptions(opts...)
8083

8184
return &Message{
82-
Timeout: o.timeout,
83-
RetryCount: o.retryCount,
84-
RetryDelay: o.retryDelay,
85-
Task: task,
85+
Timeout: o.timeout,
86+
RetryCount: o.retryCount,
87+
RetryDelay: o.retryDelay,
88+
RetryFactor: o.retryFactor,
89+
RetryMin: o.retryMin,
90+
RetryMax: o.retryMax,
91+
Task: task,
8692
}
8793
}
8894

job/job_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestMessageEncodeDecode(t *testing.T) {
2323
RetryCount: Int64(100),
2424
RetryDelay: Time(30 * time.Millisecond),
2525
Timeout: Time(3 * time.Millisecond),
26+
RetryMin: Time(200 * time.Millisecond),
2627
},
2728
)
2829

@@ -33,4 +34,7 @@ func TestMessageEncodeDecode(t *testing.T) {
3334
assert.Equal(t, 30*time.Millisecond, out.RetryDelay)
3435
assert.Equal(t, 3*time.Millisecond, out.Timeout)
3536
assert.Equal(t, "foo", string(out.Payload))
37+
assert.Equal(t, 200*time.Millisecond, out.RetryMin)
38+
assert.Equal(t, 10*time.Second, out.RetryMax)
39+
assert.Equal(t, 2.0, out.RetryFactor)
3640
}

job/option.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func newDefaultOptions() Options {
2626
type AllowOption struct {
2727
RetryCount *int64
2828
RetryDelay *time.Duration
29-
retryFactor *float64
30-
retryMin *time.Duration
31-
retryMax *time.Duration
29+
RetryFactor *float64
30+
RetryMin *time.Duration
31+
RetryMax *time.Duration
3232
Timeout *time.Duration
3333
}
3434

@@ -49,16 +49,16 @@ func NewOptions(opts ...AllowOption) Options {
4949
o.timeout = *opts[0].Timeout
5050
}
5151

52-
if opts[0].retryFactor != nil && *opts[0].retryFactor != o.retryFactor {
53-
o.retryFactor = *opts[0].retryFactor
52+
if opts[0].RetryFactor != nil && *opts[0].RetryFactor != o.retryFactor {
53+
o.retryFactor = *opts[0].RetryFactor
5454
}
5555

56-
if opts[0].retryMin != nil && *opts[0].retryMin != o.retryMin {
57-
o.retryMin = *opts[0].retryMin
56+
if opts[0].RetryMin != nil && *opts[0].RetryMin != o.retryMin {
57+
o.retryMin = *opts[0].RetryMin
5858
}
5959

60-
if opts[0].retryMax != nil && *opts[0].retryMax != o.retryMax {
61-
o.retryMax = *opts[0].retryMax
60+
if opts[0].RetryMax != nil && *opts[0].RetryMax != o.retryMax {
61+
o.retryMax = *opts[0].RetryMax
6262
}
6363
}
6464

job/option_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ func TestOptions(t *testing.T) {
1919
assert.Equal(t, int64(100), o.retryCount)
2020
assert.Equal(t, 30*time.Millisecond, o.retryDelay)
2121
assert.Equal(t, 3*time.Millisecond, o.timeout)
22+
assert.Equal(t, 100*time.Millisecond, o.retryMin)
23+
assert.Equal(t, 10*time.Second, o.retryMax)
24+
assert.Equal(t, 2.0, o.retryFactor)
2225
}

queue.go

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func (q *Queue) handle(m *job.Message) error {
225225

226226
select {
227227
case <-time.After(delay): // retry delay
228+
q.logger.Infof("retry remaining times: %d, delay time: %s", m.RetryCount, delay)
228229
case <-ctx.Done(): // timeout reached
229230
err = ctx.Err()
230231
break loop

0 commit comments

Comments
 (0)