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

407 lines
11 KiB
Go
Raw Normal View History

2019-12-04 22:25:58 +08:00
// Package rdb encapsulates the interactions with redis.
2019-12-04 13:01:26 +08:00
package rdb
2019-11-20 11:44:41 +08:00
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/go-redis/redis/v7"
2019-12-04 13:01:26 +08:00
"github.com/google/uuid"
2019-11-20 11:44:41 +08:00
)
// Redis keys
const (
allQueues = "asynq:queues" // SET
2019-12-04 13:01:26 +08:00
queuePrefix = "asynq:queues:" // LIST - asynq:queues:<qname>
DefaultQueue = queuePrefix + "default" // LIST
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
)
2019-12-04 22:25:58 +08:00
// ErrDequeueTimeout indicates that the blocking dequeue operation timed out.
2019-12-04 13:01:26 +08:00
var ErrDequeueTimeout = errors.New("blocking dequeue operation timed out")
2019-11-20 11:44:41 +08:00
2019-12-04 22:25:58 +08:00
// RDB is a client interface to query and mutate task queues.
2019-12-04 13:01:26 +08:00
type RDB struct {
2019-11-20 11:44:41 +08:00
client *redis.Client
}
2019-12-04 13:01:26 +08:00
// NewRDB returns a new instance of RDB.
func NewRDB(client *redis.Client) *RDB {
return &RDB{client}
}
2019-12-04 22:25:58 +08:00
// TaskMessage is the internal representation of a task with additional metadata fields.
// Serialized data of this type gets written in redis.
2019-12-04 13:01:26 +08:00
type TaskMessage struct {
//-------- Task fields --------
2019-12-04 22:25:58 +08:00
// Type represents the kind of task.
Type string
// Payload holds data needed to process the task.
2019-12-04 13:01:26 +08:00
Payload map[string]interface{}
2019-12-04 22:25:58 +08:00
//-------- Metadata fields --------
// ID is a unique identifier for each task
2019-12-04 13:01:26 +08:00
ID uuid.UUID
2019-12-04 22:25:58 +08:00
// Queue is a name this message should be enqueued to
2019-12-04 13:01:26 +08:00
Queue string
2019-12-04 22:25:58 +08:00
// Retry is the max number of retry for this task.
2019-12-04 13:01:26 +08:00
Retry int
2019-12-04 22:25:58 +08:00
// Retried is the number of times we've retried this task so far
2019-12-04 13:01:26 +08:00
Retried int
2019-12-04 22:25:58 +08:00
// ErrorMsg holds the error message from the last failure
2019-12-04 13:01:26 +08:00
ErrorMsg string
}
// Stats represents a state of queues at a certain time.
type Stats struct {
Queued int
InProgress int
Scheduled int
Retry int
Dead int
Timestamp time.Time
}
// EnqueuedTask is a task in a queue and is ready to be processed.
2019-12-04 22:25:58 +08:00
// Note: This is read only and used for monitoring purpose.
2019-12-04 13:01:26 +08:00
type EnqueuedTask struct {
ID uuid.UUID
Type string
Payload map[string]interface{}
}
// InProgressTask is a task that's currently being processed.
2019-12-04 22:25:58 +08:00
// Note: This is read only and used for monitoring purpose.
2019-12-04 13:01:26 +08:00
type InProgressTask struct {
ID uuid.UUID
Type string
Payload map[string]interface{}
}
// ScheduledTask is a task that's scheduled to be processed in the future.
2019-12-04 22:25:58 +08:00
// Note: This is read only and used for monitoring purpose.
2019-12-04 13:01:26 +08:00
type ScheduledTask struct {
ID uuid.UUID
Type string
Payload map[string]interface{}
ProcessAt time.Time
}
// RetryTask is a task that's in retry queue because worker failed to process the task.
2019-12-04 22:25:58 +08:00
// Note: This is read only and used for monitoring purpose.
2019-12-04 13:01:26 +08:00
type RetryTask struct {
ID uuid.UUID
Type string
Payload map[string]interface{}
// TODO(hibiken): add LastFailedAt time.Time
ProcessAt time.Time
ErrorMsg string
Retried int
Retry int
}
// DeadTask is a task in that has exhausted all retries.
2019-12-04 22:25:58 +08:00
// Note: This is read only and used for monitoring purpose.
2019-12-04 13:01:26 +08:00
type DeadTask struct {
ID uuid.UUID
Type string
Payload map[string]interface{}
LastFailedAt time.Time
ErrorMsg string
}
// Close closes the connection with redis server.
func (r *RDB) Close() error {
return r.client.Close()
2019-11-20 11:44:41 +08:00
}
2019-12-04 13:01:26 +08:00
// Enqueue inserts the given task to the end of the queue.
2019-11-26 11:58:24 +08:00
// It also adds the queue name to the "all-queues" list.
2019-12-04 13:01:26 +08:00
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-12-04 13:01:26 +08:00
// Dequeue blocks until there is a task available to be processed,
2019-11-26 12:10:35 +08:00
// once a task is available, it adds the task to "in progress" list
// and returns the task.
2019-12-04 22:25:58 +08:00
func (r *RDB) Dequeue(timeout time.Duration) (*TaskMessage, error) {
data, err := r.client.BRPopLPush(DefaultQueue, InProgress, timeout).Result()
2019-11-28 11:36:56 +08:00
if err == redis.Nil {
2019-12-04 13:01:26 +08:00
return nil, ErrDequeueTimeout
2019-11-28 11:36:56 +08:00
}
2019-11-20 11:44:41 +08:00
if err != nil {
2019-12-04 22:25:58 +08:00
return nil, fmt.Errorf("command `BRPOPLPUSH %q %q %v` failed: %v", DefaultQueue, InProgress, timeout, err)
2019-11-20 11:44:41 +08:00
}
2019-12-04 13:01:26 +08:00
var msg TaskMessage
2019-11-20 11:44:41 +08:00
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
}
2019-12-04 22:25:58 +08:00
fmt.Printf("[DEBUG] perform task %+v from %s\n", msg, DefaultQueue)
2019-11-20 11:44:41 +08:00
return &msg, nil
}
2019-12-04 22:33:05 +08:00
// Done removes the task from in-progress queue to mark the task as done.
func (r *RDB) Done(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"
2019-12-04 22:33:05 +08:00
err = r.client.LRem(InProgress, 0, string(bytes)).Err()
2019-11-22 13:45:27 +08:00
if err != nil {
2019-12-04 22:33:05 +08:00
return fmt.Errorf("command `LREM %s 0 %s` failed: %v", InProgress, string(bytes), err)
2019-11-22 13:45:27 +08:00
}
return nil
}
// Schedule adds the task to the backlog queue to be processed in the future.
func (r *RDB) Schedule(msg *TaskMessage, processAt time.Time) error {
return r.schedule(Scheduled, processAt, msg)
}
// RetryLater adds the task to the backlog queue to be retried in the future.
func (r *RDB) RetryLater(msg *TaskMessage, processAt time.Time) error {
return r.schedule(Retry, processAt, msg)
}
// 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
}
2019-12-04 22:50:52 +08:00
// Kill sends the task to "dead" set.
// It also trims the set by timestamp and set size.
2019-12-04 13:01:26 +08:00
func (r *RDB) Kill(msg *TaskMessage) error {
2019-12-04 22:50:52 +08:00
const maxDeadTask = 10
const deadExpirationInDays = 90
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
}
now := time.Now()
pipe := r.client.Pipeline()
2019-12-04 13:01:26 +08:00
pipe.ZAdd(Dead, &redis.Z{Member: string(bytes), Score: float64(now.Unix())})
2019-11-20 11:44:41 +08:00
limit := now.AddDate(0, 0, -deadExpirationInDays).Unix() // 90 days ago
2019-12-04 13:01:26 +08:00
pipe.ZRemRangeByScore(Dead, "-inf", strconv.Itoa(int(limit)))
pipe.ZRemRangeByRank(Dead, 0, -maxDeadTask) // trim the set to 100
2019-11-20 11:44:41 +08:00
_, err = pipe.Exec()
return err
}
2019-12-04 22:50:52 +08:00
// RestoreUnfinished moves all tasks from in-progress list to the queue.
func (r *RDB) RestoreUnfinished() 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
`)
2019-12-04 22:50:52 +08:00
_, err := script.Run(r.client, []string{InProgress, DefaultQueue}).Result()
return err
}
2019-12-04 23:14:37 +08:00
// CheckScheduled checks for all scheduled tasks and moves any tasks that
// have to be processed to the queue.
func (r *RDB) CheckScheduled() error {
delayed := []string{Scheduled, Retry}
for _, zset := range delayed {
if err := r.forward(zset); err != nil {
return err
}
}
return nil
}
2019-12-04 13:01:26 +08:00
// Forward moves all tasks with a score less than the current unix time
// from the given zset to the default queue.
2019-12-04 23:14:37 +08:00
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-12-04 13:01:26 +08:00
res, err := script.Run(r.client, []string{from, allQueues, DefaultQueue}, now).Result()
2019-11-26 22:52:58 +08:00
fmt.Printf("[DEBUG] got %d tasks from %q\n", len(res.([]interface{})), from)
return err
}
2019-12-01 23:59:52 +08:00
2019-12-04 13:01:26 +08:00
// CurrentStats returns a current state of the queues.
func (r *RDB) CurrentStats() (*Stats, error) {
2019-12-01 23:59:52 +08:00
pipe := r.client.Pipeline()
2019-12-04 13:01:26 +08:00
qlen := pipe.LLen(DefaultQueue)
plen := pipe.LLen(InProgress)
slen := pipe.ZCard(Scheduled)
rlen := pipe.ZCard(Retry)
dlen := pipe.ZCard(Dead)
2019-12-01 23:59:52 +08:00
_, err := pipe.Exec()
if err != nil {
return nil, err
}
return &Stats{
Queued: int(qlen.Val()),
InProgress: int(plen.Val()),
Scheduled: int(slen.Val()),
Retry: int(rlen.Val()),
Dead: int(dlen.Val()),
Timestamp: time.Now(),
}, nil
}
2019-12-02 07:01:26 +08:00
2019-12-04 13:01:26 +08:00
func (r *RDB) ListEnqueued() ([]*TaskMessage, error) {
return r.rangeList(DefaultQueue)
2019-12-02 07:01:26 +08:00
}
2019-12-04 13:01:26 +08:00
func (r *RDB) ListInProgress() ([]*TaskMessage, error) {
return r.rangeList(InProgress)
2019-12-02 07:01:26 +08:00
}
2019-12-04 13:01:26 +08:00
func (r *RDB) ListScheduled() ([]*ScheduledTask, error) {
data, err := r.client.ZRangeWithScores(Scheduled, 0, -1).Result()
if err != nil {
return nil, err
}
var tasks []*ScheduledTask
for _, z := range data {
s, ok := z.Member.(string)
if !ok {
continue // bad data, ignore and continue
}
2019-12-04 13:01:26 +08:00
var msg TaskMessage
err := json.Unmarshal([]byte(s), &msg)
if err != nil {
continue // bad data, ignore and continue
}
processAt := time.Unix(int64(z.Score), 0)
tasks = append(tasks, &ScheduledTask{
ID: msg.ID,
Type: msg.Type,
Payload: msg.Payload,
ProcessAt: processAt,
})
}
return tasks, nil
2019-12-02 07:01:26 +08:00
}
2019-12-04 13:01:26 +08:00
func (r *RDB) ListRetry() ([]*RetryTask, error) {
data, err := r.client.ZRangeWithScores(Retry, 0, -1).Result()
if err != nil {
return nil, err
}
var tasks []*RetryTask
for _, z := range data {
s, ok := z.Member.(string)
if !ok {
continue // bad data, ignore and continue
}
2019-12-04 13:01:26 +08:00
var msg TaskMessage
err := json.Unmarshal([]byte(s), &msg)
if err != nil {
continue // bad data, ignore and continue
}
processAt := time.Unix(int64(z.Score), 0)
tasks = append(tasks, &RetryTask{
ID: msg.ID,
Type: msg.Type,
Payload: msg.Payload,
ErrorMsg: msg.ErrorMsg,
Retry: msg.Retry,
Retried: msg.Retried,
ProcessAt: processAt,
})
}
return tasks, nil
2019-12-02 07:01:26 +08:00
}
2019-12-04 13:01:26 +08:00
func (r *RDB) ListDead() ([]*DeadTask, error) {
data, err := r.client.ZRangeWithScores(Dead, 0, -1).Result()
if err != nil {
return nil, err
}
var tasks []*DeadTask
for _, z := range data {
s, ok := z.Member.(string)
if !ok {
continue // bad data, ignore and continue
}
2019-12-04 13:01:26 +08:00
var msg TaskMessage
err := json.Unmarshal([]byte(s), &msg)
if err != nil {
continue // bad data, ignore and continue
}
lastFailedAt := time.Unix(int64(z.Score), 0)
tasks = append(tasks, &DeadTask{
ID: msg.ID,
Type: msg.Type,
Payload: msg.Payload,
ErrorMsg: msg.ErrorMsg,
LastFailedAt: lastFailedAt,
})
}
return tasks, nil
2019-12-02 07:01:26 +08:00
}
2019-12-04 13:01:26 +08:00
func (r *RDB) rangeList(key string) ([]*TaskMessage, error) {
2019-12-02 07:01:26 +08:00
data, err := r.client.LRange(key, 0, -1).Result()
if err != nil {
return nil, err
}
return r.toMessageSlice(data), nil
}
2019-12-04 13:01:26 +08:00
func (r *RDB) rangeZSet(key string) ([]*TaskMessage, error) {
2019-12-02 07:01:26 +08:00
data, err := r.client.ZRange(key, 0, -1).Result()
if err != nil {
return nil, err
}
return r.toMessageSlice(data), nil
}
// toMessageSlice convers json strings to a slice of task messages.
2019-12-04 13:01:26 +08:00
func (r *RDB) toMessageSlice(data []string) []*TaskMessage {
var msgs []*TaskMessage
2019-12-02 07:01:26 +08:00
for _, s := range data {
2019-12-04 13:01:26 +08:00
var msg TaskMessage
2019-12-02 07:01:26 +08:00
err := json.Unmarshal([]byte(s), &msg)
if err != nil {
// bad data; ignore and continue
continue
}
msgs = append(msgs, &msg)
}
return msgs
}