2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 19:06:46 +08:00

Use lua script to implement (*rdb).moveAll

This commit is contained in:
Ken Hibino 2019-11-26 10:09:42 -08:00
parent 4fd82c5975
commit 97b96f6992

18
rdb.go
View File

@ -135,15 +135,15 @@ func (r *rdb) listQueues() []string {
// moveAll moves all tasks from src list to dst list. // moveAll moves all tasks from src list to dst list.
func (r *rdb) moveAll(src, dst string) error { func (r *rdb) moveAll(src, dst string) error {
// TODO(hibiken): Lua script script := redis.NewScript(`
txf := func(tx *redis.Tx) error { local len = redis.call("LLEN", KEYS[1])
length := tx.LLen(src).Val() for i = len, 1, -1 do
for i := 0; i < int(length); i++ { redis.call("RPOPLPUSH", KEYS[1], KEYS[2])
tx.RPopLPush(src, dst) end
} return len
return nil `)
} _, err := script.Run(r.client, []string{src, dst}).Result()
return r.client.Watch(txf, src) return err
} }
// forward moves all tasks with a score less than the current unix time // forward moves all tasks with a score less than the current unix time