2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-25 07:12:17 +08:00

(fix): RestoreUnfinished to select correct queue

This commit is contained in:
Ken Hibino 2020-01-26 13:55:23 -08:00
parent 166497748b
commit 6fbaa2ed6c
3 changed files with 61 additions and 21 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Fixed
- Fixed restoring unfinished tasks back to correct queues.
### Changed ### Changed
- `asynqmon ls` command is now paginated (default 30 tasks from first page) - `asynqmon ls` command is now paginated (default 30 tasks from first page)

View File

@ -273,13 +273,16 @@ func (r *RDB) Kill(msg *base.TaskMessage, errMsg string) error {
// and reports the number of tasks restored. // and reports the number of tasks restored.
func (r *RDB) RestoreUnfinished() (int64, error) { func (r *RDB) RestoreUnfinished() (int64, error) {
script := redis.NewScript(` script := redis.NewScript(`
local len = redis.call("LLEN", KEYS[1]) local msgs = redis.call("LRANGE", KEYS[1], 0, -1)
for i = len, 1, -1 do for _, msg in ipairs(msgs) do
redis.call("RPOPLPUSH", KEYS[1], KEYS[2]) local decoded = cjson.decode(msg)
local qkey = ARGV[1] .. decoded["Queue"]
redis.call("LREM", KEYS[1], 0, msg)
redis.call("RPUSH", qkey, msg)
end end
return len return table.getn(msgs)
`) `)
res, err := script.Run(r.client, []string{base.InProgressQueue, base.DefaultQueue}).Result() res, err := script.Run(r.client, []string{base.InProgressQueue}, base.QueuePrefix).Result()
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -544,41 +544,72 @@ func TestRestoreUnfinished(t *testing.T) {
t1 := h.NewTaskMessage("send_email", nil) t1 := h.NewTaskMessage("send_email", nil)
t2 := h.NewTaskMessage("export_csv", nil) t2 := h.NewTaskMessage("export_csv", nil)
t3 := h.NewTaskMessage("sync_stuff", nil) t3 := h.NewTaskMessage("sync_stuff", nil)
t4 := h.NewTaskMessageWithQueue("important", nil, "critical")
t5 := h.NewTaskMessageWithQueue("minor", nil, "low")
tests := []struct { tests := []struct {
inProgress []*base.TaskMessage inProgress []*base.TaskMessage
enqueued []*base.TaskMessage enqueued map[string][]*base.TaskMessage
want int64 want int64
wantInProgress []*base.TaskMessage wantInProgress []*base.TaskMessage
wantEnqueued []*base.TaskMessage wantEnqueued map[string][]*base.TaskMessage
}{ }{
{ {
inProgress: []*base.TaskMessage{t1, t2, t3}, inProgress: []*base.TaskMessage{t1, t2, t3},
enqueued: []*base.TaskMessage{}, enqueued: map[string][]*base.TaskMessage{
base.DefaultQueueName: {},
},
want: 3, want: 3,
wantInProgress: []*base.TaskMessage{}, wantInProgress: []*base.TaskMessage{},
wantEnqueued: []*base.TaskMessage{t1, t2, t3}, wantEnqueued: map[string][]*base.TaskMessage{
base.DefaultQueueName: {t1, t2, t3},
},
}, },
{ {
inProgress: []*base.TaskMessage{}, inProgress: []*base.TaskMessage{},
enqueued: []*base.TaskMessage{t1, t2, t3}, enqueued: map[string][]*base.TaskMessage{
base.DefaultQueueName: {t1, t2, t3},
},
want: 0, want: 0,
wantInProgress: []*base.TaskMessage{}, wantInProgress: []*base.TaskMessage{},
wantEnqueued: []*base.TaskMessage{t1, t2, t3}, wantEnqueued: map[string][]*base.TaskMessage{
base.DefaultQueueName: {t1, t2, t3},
},
}, },
{ {
inProgress: []*base.TaskMessage{t2, t3}, inProgress: []*base.TaskMessage{t2, t3},
enqueued: []*base.TaskMessage{t1}, enqueued: map[string][]*base.TaskMessage{
base.DefaultQueueName: {t1},
},
want: 2, want: 2,
wantInProgress: []*base.TaskMessage{}, wantInProgress: []*base.TaskMessage{},
wantEnqueued: []*base.TaskMessage{t1, t2, t3}, wantEnqueued: map[string][]*base.TaskMessage{
base.DefaultQueueName: {t1, t2, t3},
},
},
{
inProgress: []*base.TaskMessage{t2, t3, t4, t5},
enqueued: map[string][]*base.TaskMessage{
base.DefaultQueueName: {t1},
"critical": {},
"low": {},
},
want: 4,
wantInProgress: []*base.TaskMessage{},
wantEnqueued: map[string][]*base.TaskMessage{
base.DefaultQueueName: {t1, t2, t3},
"critical": {t4},
"low": {t5},
},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
h.FlushDB(t, r.client) // clean up db before each test case h.FlushDB(t, r.client) // clean up db before each test case
h.SeedInProgressQueue(t, r.client, tc.inProgress) h.SeedInProgressQueue(t, r.client, tc.inProgress)
h.SeedEnqueuedQueue(t, r.client, tc.enqueued) for qname, msgs := range tc.enqueued {
h.SeedEnqueuedQueue(t, r.client, msgs, qname)
}
got, err := r.RestoreUnfinished() got, err := r.RestoreUnfinished()
if got != tc.want || err != nil { if got != tc.want || err != nil {
@ -591,9 +622,11 @@ func TestRestoreUnfinished(t *testing.T) {
t.Errorf("mismatch found in %q: (-want, +got):\n%s", base.InProgressQueue, diff) t.Errorf("mismatch found in %q: (-want, +got):\n%s", base.InProgressQueue, diff)
} }
gotEnqueued := h.GetEnqueuedMessages(t, r.client) for qname, want := range tc.wantEnqueued {
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, h.SortMsgOpt); diff != "" { gotEnqueued := h.GetEnqueuedMessages(t, r.client, qname)
t.Errorf("mismatch found in %q: (-want, +got):\n%s", base.DefaultQueue, diff) if diff := cmp.Diff(want, gotEnqueued, h.SortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q: (-want, +got):\n%s", base.QueueKey(qname), diff)
}
} }
} }
} }