2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 11:05:58 +08:00
asynq/rdb.go

167 lines
5.0 KiB
Go
Raw Normal View History

2019-11-20 11:44:41 +08:00
package asynq
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/go-redis/redis/v7"
)
// Redis keys
const (
queuePrefix = "asynq:queues:" // LIST - asynq:queues:<qname>
defaultQueue = queuePrefix + "default" // LIST
allQueues = "asynq:queues" // SET
scheduled = "asynq:scheduled" // ZSET
retry = "asynq:retry" // ZSET
dead = "asynq:dead" // ZSET
inProgress = "asynq:in_progress" // SET
2019-11-20 11:44:41 +08:00
)
var errDequeueTimeout = errors.New("blocking dequeue operation timed out")
2019-11-20 11:44:41 +08:00
2019-11-26 12:10:35 +08:00
// rdb encapsulates the interactions with redis server.
2019-11-20 11:44:41 +08:00
type rdb struct {
client *redis.Client
}
2019-11-27 22:41:54 +08:00
func newRDB(opt *RedisOpt) *rdb {
client := redis.NewClient(&redis.Options{
Addr: opt.Addr,
Password: opt.Password,
DB: opt.DB,
})
2019-11-20 11:44:41 +08:00
return &rdb{client}
}
2019-11-26 11:58:24 +08:00
// enqueue inserts the given task to the end of the queue.
// It also adds the queue name to the "all-queues" list.
func (r *rdb) enqueue(msg *taskMessage) error {
2019-11-20 11:44:41 +08:00
bytes, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("could not marshal %+v to json: %v", msg, err)
2019-11-20 11:44:41 +08:00
}
qname := queuePrefix + msg.Queue
2019-11-26 12:10:35 +08:00
pipe := r.client.Pipeline()
pipe.SAdd(allQueues, qname)
pipe.LPush(qname, string(bytes))
_, err = pipe.Exec()
2019-11-20 11:44:41 +08:00
if err != nil {
return fmt.Errorf("could not enqueue the task %+v to %q: %v", msg, qname, err)
2019-11-20 11:44:41 +08:00
}
return nil
}
2019-11-26 12:10:35 +08:00
// dequeue blocks until there is a task available to be processed,
// once a task is available, it adds the task to "in progress" list
// and returns the task.
func (r *rdb) dequeue(qname string, timeout time.Duration) (*taskMessage, error) {
data, err := r.client.BRPopLPush(qname, inProgress, timeout).Result()
2019-11-28 11:36:56 +08:00
if err == redis.Nil {
return nil, errDequeueTimeout
}
2019-11-20 11:44:41 +08:00
if err != nil {
return nil, fmt.Errorf("command `BRPOPLPUSH %q %q %v` failed: %v", qname, inProgress, timeout, err)
2019-11-20 11:44:41 +08:00
}
var msg taskMessage
err = json.Unmarshal([]byte(data), &msg)
if err != nil {
return nil, fmt.Errorf("could not unmarshal %v to json: %v", data, err)
2019-11-20 11:44:41 +08:00
}
fmt.Printf("[DEBUG] perform task %+v from %s\n", msg, qname)
2019-11-20 11:44:41 +08:00
return &msg, nil
}
func (r *rdb) lrem(key string, msg *taskMessage) error {
2019-11-22 13:45:27 +08:00
bytes, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("could not marshal %+v to json: %v", msg, err)
2019-11-22 13:45:27 +08:00
}
// NOTE: count ZERO means "remove all elements equal to val"
err = r.client.LRem(key, 0, string(bytes)).Err()
2019-11-22 13:45:27 +08:00
if err != nil {
return fmt.Errorf("command `LREM %s 0 %s` failed: %v", key, string(bytes), err)
2019-11-22 13:45:27 +08:00
}
return nil
}
2019-11-27 23:16:16 +08:00
// schedule adds the task to the zset to be processd at the specified time.
func (r *rdb) schedule(zset string, processAt time.Time, msg *taskMessage) error {
2019-11-20 11:44:41 +08:00
bytes, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("could not marshal %+v to json: %v", msg, err)
2019-11-20 11:44:41 +08:00
}
2019-11-27 23:16:16 +08:00
score := float64(processAt.Unix())
err = r.client.ZAdd(zset, &redis.Z{Member: string(bytes), Score: score}).Err()
2019-11-20 11:44:41 +08:00
if err != nil {
return fmt.Errorf("command `ZADD %s %.1f %s` failed: %v", zset, score, string(bytes), err)
2019-11-20 11:44:41 +08:00
}
return nil
}
const maxDeadTask = 100
const deadExpirationInDays = 90
// kill sends the taskMessage to "dead" set.
// It also trims the sorted set by timestamp and set size.
func (r *rdb) kill(msg *taskMessage) error {
bytes, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("could not marshal %+v to json: %v", msg, err)
2019-11-20 11:44:41 +08:00
}
now := time.Now()
pipe := r.client.Pipeline()
pipe.ZAdd(dead, &redis.Z{Member: string(bytes), Score: float64(now.Unix())})
limit := now.AddDate(0, 0, -deadExpirationInDays).Unix() // 90 days ago
pipe.ZRemRangeByScore(dead, "-inf", strconv.Itoa(int(limit)))
pipe.ZRemRangeByRank(dead, 0, -maxDeadTask) // trim the set to 100
_, err = pipe.Exec()
return err
}
// listQueues returns the list of all queues.
2019-11-20 23:01:24 +08:00
// NOTE: Add default to the slice if empty because
// BLPOP will error out if empty list is passed.
2019-11-20 11:44:41 +08:00
func (r *rdb) listQueues() []string {
2019-11-20 23:01:24 +08:00
queues := r.client.SMembers(allQueues).Val()
if len(queues) == 0 {
queues = append(queues, queuePrefix+"default")
}
return queues
2019-11-20 11:44:41 +08:00
}
// moveAll moves all tasks from src list to dst list.
func (r *rdb) moveAll(src, dst string) error {
script := redis.NewScript(`
local len = redis.call("LLEN", KEYS[1])
for i = len, 1, -1 do
redis.call("RPOPLPUSH", KEYS[1], KEYS[2])
end
return len
`)
_, err := script.Run(r.client, []string{src, dst}).Result()
return err
}
// forward moves all tasks with a score less than the current unix time
// from the given zset to the default queue.
2019-11-26 22:52:58 +08:00
// TODO(hibiken): Find a better method name that reflects what this does.
func (r *rdb) forward(from string) error {
script := redis.NewScript(`
local msgs = redis.call("ZRANGEBYSCORE", KEYS[1], "-inf", ARGV[1])
for _, msg in ipairs(msgs) do
redis.call("ZREM", KEYS[1], msg)
redis.call("SADD", KEYS[2], KEYS[3])
redis.call("LPUSH", KEYS[3], msg)
end
return msgs
`)
now := float64(time.Now().Unix())
2019-11-26 22:52:58 +08:00
res, err := script.Run(r.client, []string{from, allQueues, defaultQueue}, now).Result()
fmt.Printf("[DEBUG] got %d tasks from %q\n", len(res.([]interface{})), from)
return err
}