2020-01-03 10:13:16 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-11-20 13:19:46 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
2020-03-18 21:49:39 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
2020-01-06 22:53:40 +08:00
|
|
|
"strings"
|
2020-04-26 22:48:38 +08:00
|
|
|
"sync"
|
2019-11-20 13:19:46 +08:00
|
|
|
"time"
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2019-12-04 13:01:26 +08:00
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
2019-12-21 23:42:32 +08:00
|
|
|
"github.com/rs/xid"
|
2019-11-20 13:19:46 +08:00
|
|
|
)
|
|
|
|
|
2019-12-07 14:00:09 +08:00
|
|
|
// A Client is responsible for scheduling tasks.
|
|
|
|
//
|
2019-12-09 22:52:43 +08:00
|
|
|
// A Client is used to register tasks that should be processed
|
2019-12-07 14:00:09 +08:00
|
|
|
// immediately or some time in the future.
|
|
|
|
//
|
|
|
|
// Clients are safe for concurrent use by multiple goroutines.
|
2019-11-20 13:19:46 +08:00
|
|
|
type Client struct {
|
2020-04-26 22:48:38 +08:00
|
|
|
mu sync.Mutex
|
|
|
|
opts map[string][]Option
|
|
|
|
rdb *rdb.RDB
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
|
|
|
|
2020-01-17 11:50:45 +08:00
|
|
|
// NewClient and returns a new Client given a redis connection option.
|
2020-01-15 13:19:06 +08:00
|
|
|
func NewClient(r RedisConnOpt) *Client {
|
|
|
|
rdb := rdb.NewRDB(createRedisClient(r))
|
2020-04-26 22:48:38 +08:00
|
|
|
return &Client{
|
|
|
|
opts: make(map[string][]Option),
|
|
|
|
rdb: rdb,
|
|
|
|
}
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
|
|
|
|
2020-01-17 11:50:45 +08:00
|
|
|
// Option specifies the task processing behavior.
|
2019-12-21 23:42:32 +08:00
|
|
|
type Option interface{}
|
|
|
|
|
2020-01-06 22:53:40 +08:00
|
|
|
// Internal option representations.
|
|
|
|
type (
|
2020-03-08 12:24:03 +08:00
|
|
|
retryOption int
|
|
|
|
queueOption string
|
|
|
|
timeoutOption time.Duration
|
|
|
|
deadlineOption time.Time
|
2020-03-18 21:49:39 +08:00
|
|
|
uniqueOption time.Duration
|
2020-01-06 22:53:40 +08:00
|
|
|
)
|
2019-12-21 23:42:32 +08:00
|
|
|
|
|
|
|
// MaxRetry returns an option to specify the max number of times
|
2020-01-06 23:21:14 +08:00
|
|
|
// the task will be retried.
|
2019-12-21 23:42:32 +08:00
|
|
|
//
|
|
|
|
// Negative retry count is treated as zero retry.
|
|
|
|
func MaxRetry(n int) Option {
|
|
|
|
if n < 0 {
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
return retryOption(n)
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:50:45 +08:00
|
|
|
// Queue returns an option to specify the queue to enqueue the task into.
|
2020-01-06 22:53:40 +08:00
|
|
|
//
|
|
|
|
// Queue name is case-insensitive and the lowercased version is used.
|
2020-01-06 23:21:14 +08:00
|
|
|
func Queue(name string) Option {
|
|
|
|
return queueOption(strings.ToLower(name))
|
2020-01-06 22:53:40 +08:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:53:59 +08:00
|
|
|
// Timeout returns an option to specify how long a task may run.
|
|
|
|
//
|
|
|
|
// Zero duration means no limit.
|
|
|
|
func Timeout(d time.Duration) Option {
|
|
|
|
return timeoutOption(d)
|
|
|
|
}
|
|
|
|
|
2020-03-08 12:24:03 +08:00
|
|
|
// Deadline returns an option to specify the deadline for the given task.
|
|
|
|
func Deadline(t time.Time) Option {
|
|
|
|
return deadlineOption(t)
|
|
|
|
}
|
|
|
|
|
2020-03-18 21:49:39 +08:00
|
|
|
// Unique returns an option to enqueue a task only if the given task is unique.
|
|
|
|
// Task enqueued with this option is guaranteed to be unique within the given ttl.
|
|
|
|
// Once the task gets processed successfully or once the TTL has expired, another task with the same uniqueness may be enqueued.
|
|
|
|
// ErrDuplicateTask error is returned when enqueueing a duplicate task.
|
|
|
|
//
|
|
|
|
// Uniqueness of a task is based on the following properties:
|
|
|
|
// - Task Type
|
|
|
|
// - Task Payload
|
|
|
|
// - Queue Name
|
|
|
|
func Unique(ttl time.Duration) Option {
|
|
|
|
return uniqueOption(ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrDuplicateTask indicates that the given task could not be enqueued since it's a duplicate of another task.
|
|
|
|
//
|
|
|
|
// ErrDuplicateTask error only applies to tasks enqueued with a Unique option.
|
|
|
|
var ErrDuplicateTask = errors.New("task already exists")
|
|
|
|
|
2019-12-21 23:42:32 +08:00
|
|
|
type option struct {
|
2020-03-18 21:49:39 +08:00
|
|
|
retry int
|
|
|
|
queue string
|
|
|
|
timeout time.Duration
|
|
|
|
deadline time.Time
|
|
|
|
uniqueTTL time.Duration
|
2019-12-21 23:42:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func composeOptions(opts ...Option) option {
|
|
|
|
res := option{
|
2020-03-08 12:24:03 +08:00
|
|
|
retry: defaultMaxRetry,
|
|
|
|
queue: base.DefaultQueueName,
|
|
|
|
timeout: 0,
|
|
|
|
deadline: time.Time{},
|
2019-12-21 23:42:32 +08:00
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt := opt.(type) {
|
|
|
|
case retryOption:
|
|
|
|
res.retry = int(opt)
|
2020-01-06 22:53:40 +08:00
|
|
|
case queueOption:
|
|
|
|
res.queue = string(opt)
|
2020-02-12 13:53:59 +08:00
|
|
|
case timeoutOption:
|
|
|
|
res.timeout = time.Duration(opt)
|
2020-03-08 12:24:03 +08:00
|
|
|
case deadlineOption:
|
|
|
|
res.deadline = time.Time(opt)
|
2020-03-18 21:49:39 +08:00
|
|
|
case uniqueOption:
|
|
|
|
res.uniqueTTL = time.Duration(opt)
|
2019-12-21 23:42:32 +08:00
|
|
|
default:
|
|
|
|
// ignore unexpected option
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-03-18 21:49:39 +08:00
|
|
|
// uniqueKey computes the redis key used for the given task.
|
|
|
|
// It returns an empty string if ttl is zero.
|
|
|
|
func uniqueKey(t *Task, ttl time.Duration, qname string) string {
|
|
|
|
if ttl == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%s:%s", t.Type, serializePayload(t.Payload.data), qname)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serializePayload(payload map[string]interface{}) string {
|
|
|
|
if payload == nil {
|
|
|
|
return "nil"
|
|
|
|
}
|
|
|
|
type entry struct {
|
|
|
|
k string
|
|
|
|
v interface{}
|
|
|
|
}
|
|
|
|
var es []entry
|
|
|
|
for k, v := range payload {
|
|
|
|
es = append(es, entry{k, v})
|
|
|
|
}
|
|
|
|
// sort entries by key
|
|
|
|
sort.Slice(es, func(i, j int) bool { return es[i].k < es[j].k })
|
|
|
|
var b strings.Builder
|
|
|
|
for _, e := range es {
|
|
|
|
if b.Len() > 0 {
|
|
|
|
b.WriteString(",")
|
|
|
|
}
|
|
|
|
b.WriteString(fmt.Sprintf("%s=%v", e.k, e.v))
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:48:38 +08:00
|
|
|
// Default max retry count used if nothing is specified.
|
|
|
|
const defaultMaxRetry = 25
|
|
|
|
|
|
|
|
// SetDefaultOptions sets options to be used for a given task type.
|
|
|
|
// The argument opts specifies the behavior of task processing.
|
|
|
|
// If there are conflicting Option values the last one overrides others.
|
|
|
|
//
|
|
|
|
// Default options can be overridden by options passed at enqueue time.
|
|
|
|
func (c *Client) SetDefaultOptions(taskType string, opts ...Option) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
c.opts[taskType] = opts
|
|
|
|
}
|
2019-12-21 23:42:32 +08:00
|
|
|
|
2020-02-24 07:40:04 +08:00
|
|
|
// EnqueueAt schedules task to be enqueued at the specified time.
|
2019-12-07 14:00:09 +08:00
|
|
|
//
|
2020-02-24 07:40:04 +08:00
|
|
|
// EnqueueAt returns nil if the task is scheduled successfully, otherwise returns a non-nil error.
|
2019-12-22 02:02:03 +08:00
|
|
|
//
|
2020-02-24 07:40:04 +08:00
|
|
|
// The argument opts specifies the behavior of task processing.
|
|
|
|
// If there are conflicting Option values the last one overrides others.
|
|
|
|
func (c *Client) EnqueueAt(t time.Time, task *Task, opts ...Option) error {
|
2020-04-26 22:48:38 +08:00
|
|
|
return c.enqueueAt(t, task, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue enqueues task to be processed immediately.
|
|
|
|
//
|
|
|
|
// Enqueue returns nil if the task is enqueued successfully, otherwise returns a non-nil error.
|
|
|
|
//
|
|
|
|
// The argument opts specifies the behavior of task processing.
|
|
|
|
// If there are conflicting Option values the last one overrides others.
|
|
|
|
func (c *Client) Enqueue(task *Task, opts ...Option) error {
|
|
|
|
return c.enqueueAt(time.Now(), task, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnqueueIn schedules task to be enqueued after the specified delay.
|
|
|
|
//
|
|
|
|
// EnqueueIn returns nil if the task is scheduled successfully, otherwise returns a non-nil error.
|
|
|
|
//
|
|
|
|
// The argument opts specifies the behavior of task processing.
|
|
|
|
// If there are conflicting Option values the last one overrides others.
|
|
|
|
func (c *Client) EnqueueIn(d time.Duration, task *Task, opts ...Option) error {
|
|
|
|
return c.enqueueAt(time.Now().Add(d), task, opts...)
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:25:50 +08:00
|
|
|
// Close closes the connection with redis server.
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
return c.rdb.Close()
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:48:38 +08:00
|
|
|
func (c *Client) enqueueAt(t time.Time, task *Task, opts ...Option) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
if defaults, ok := c.opts[task.Type]; ok {
|
|
|
|
opts = append(defaults, opts...)
|
|
|
|
}
|
2019-12-21 23:42:32 +08:00
|
|
|
opt := composeOptions(opts...)
|
2019-12-22 23:15:45 +08:00
|
|
|
msg := &base.TaskMessage{
|
2020-03-18 21:49:39 +08:00
|
|
|
ID: xid.New(),
|
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Queue: opt.queue,
|
|
|
|
Retry: opt.retry,
|
|
|
|
Timeout: opt.timeout.String(),
|
|
|
|
Deadline: opt.deadline.Format(time.RFC3339),
|
|
|
|
UniqueKey: uniqueKey(task, opt.uniqueTTL, opt.queue),
|
|
|
|
}
|
|
|
|
var err error
|
2020-06-14 20:31:24 +08:00
|
|
|
now := time.Now()
|
|
|
|
if t.Before(now) || t.Equal(now) {
|
2020-03-18 21:49:39 +08:00
|
|
|
err = c.enqueue(msg, opt.uniqueTTL)
|
|
|
|
} else {
|
|
|
|
err = c.schedule(msg, t, opt.uniqueTTL)
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
2020-03-18 21:49:39 +08:00
|
|
|
if err == rdb.ErrDuplicateTask {
|
|
|
|
return fmt.Errorf("%w", ErrDuplicateTask)
|
|
|
|
}
|
|
|
|
return err
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
|
|
|
|
2020-03-18 21:49:39 +08:00
|
|
|
func (c *Client) enqueue(msg *base.TaskMessage, uniqueTTL time.Duration) error {
|
|
|
|
if uniqueTTL > 0 {
|
|
|
|
return c.rdb.EnqueueUnique(msg, uniqueTTL)
|
|
|
|
}
|
|
|
|
return c.rdb.Enqueue(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) schedule(msg *base.TaskMessage, t time.Time, uniqueTTL time.Duration) error {
|
|
|
|
if uniqueTTL > 0 {
|
|
|
|
ttl := t.Add(uniqueTTL).Sub(time.Now())
|
|
|
|
return c.rdb.ScheduleUnique(msg, t, ttl)
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
2020-02-24 07:40:04 +08:00
|
|
|
return c.rdb.Schedule(msg, t)
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|