@@ -25,6 +25,7 @@ type Worker struct {
25
25
stopOnce sync.Once
26
26
startOnce sync.Once
27
27
stop chan struct {}
28
+ exit chan struct {}
28
29
opts options
29
30
}
30
31
@@ -34,6 +35,7 @@ func NewWorker(opts ...Option) *Worker {
34
35
w := & Worker {
35
36
opts : newOptions (opts ... ),
36
37
stop : make (chan struct {}),
38
+ exit : make (chan struct {}),
37
39
tasks : make (chan redis.XMessage ),
38
40
}
39
41
@@ -116,6 +118,10 @@ func (w *Worker) fetchTask() {
116
118
case <- w .stop :
117
119
// Todo: re-queue the task
118
120
w .opts .logger .Info ("re-queue the task: " , message .ID )
121
+ if err := w .queue (message .Values ); err != nil {
122
+ w .opts .logger .Error ("error to re-queue the task: " , message .ID )
123
+ }
124
+ close (w .exit )
119
125
return
120
126
}
121
127
}
@@ -178,6 +184,13 @@ func (w *Worker) Shutdown() error {
178
184
179
185
w .stopOnce .Do (func () {
180
186
close (w .stop )
187
+
188
+ // wait requeue
189
+ select {
190
+ case <- w .exit :
191
+ case <- time .After (200 * time .Millisecond ):
192
+ }
193
+
181
194
switch v := w .rdb .(type ) {
182
195
case * redis.Client :
183
196
v .Close ()
@@ -189,25 +202,29 @@ func (w *Worker) Shutdown() error {
189
202
return nil
190
203
}
191
204
192
- // Queue send notification to queue
193
- func (w * Worker ) Queue (task core.QueuedMessage ) error {
194
- if atomic .LoadInt32 (& w .stopFlag ) == 1 {
195
- return queue .ErrQueueShutdown
196
- }
197
-
205
+ func (w * Worker ) queue (data interface {}) error {
198
206
ctx := context .Background ()
199
207
200
208
// Publish a message.
201
209
err := w .rdb .XAdd (ctx , & redis.XAddArgs {
202
210
Stream : w .opts .streamName ,
203
211
MaxLen : 0 ,
204
212
MaxLenApprox : 0 ,
205
- Values : map [ string ] interface {}{ "body" : BytesToStr ( task . Bytes ())} ,
213
+ Values : data ,
206
214
}).Err ()
207
215
208
216
return err
209
217
}
210
218
219
+ // Queue send notification to queue
220
+ func (w * Worker ) Queue (task core.QueuedMessage ) error {
221
+ if atomic .LoadInt32 (& w .stopFlag ) == 1 {
222
+ return queue .ErrQueueShutdown
223
+ }
224
+
225
+ return w .queue (map [string ]interface {}{"body" : BytesToStr (task .Bytes ())})
226
+ }
227
+
211
228
// Run start the worker
212
229
func (w * Worker ) Run (task core.QueuedMessage ) error {
213
230
data , _ := task .(queue.Job )
0 commit comments