2019-11-15 13:07:19 +08:00
|
|
|
package asynq
|
|
|
|
|
2019-11-17 06:45:51 +08:00
|
|
|
/*
|
|
|
|
TODOs:
|
2019-11-18 13:13:41 +08:00
|
|
|
- [P0] Write tests
|
2019-11-17 06:45:51 +08:00
|
|
|
- [P1] Add Support for multiple queues
|
2019-11-18 07:36:33 +08:00
|
|
|
- [P1] User defined max-retry count
|
|
|
|
- [P2] Web UI
|
2019-11-17 06:45:51 +08:00
|
|
|
*/
|
|
|
|
|
2019-11-15 13:07:19 +08:00
|
|
|
import (
|
2019-11-19 23:46:04 +08:00
|
|
|
"sync"
|
2019-11-15 13:07:19 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v7"
|
|
|
|
)
|
|
|
|
|
2019-11-18 05:25:01 +08:00
|
|
|
// Max retry count by default
|
|
|
|
const defaultMaxRetry = 25
|
|
|
|
|
2019-11-15 13:07:19 +08:00
|
|
|
// Client is an interface for scheduling tasks.
|
|
|
|
type Client struct {
|
2019-11-20 11:44:41 +08:00
|
|
|
rdb *rdb
|
2019-11-15 13:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Task represents a task to be performed.
|
|
|
|
type Task struct {
|
2019-11-17 06:45:51 +08:00
|
|
|
// Type indicates the kind of the task to be performed.
|
|
|
|
Type string
|
|
|
|
|
|
|
|
// Payload is an arbitrary data needed for task execution.
|
|
|
|
// The value has to be serializable.
|
|
|
|
Payload map[string]interface{}
|
2019-11-15 13:07:19 +08:00
|
|
|
}
|
|
|
|
|
2019-11-18 05:25:01 +08:00
|
|
|
// taskMessage is an internal representation of a task with additional metadata fields.
|
|
|
|
// This data gets written in redis.
|
|
|
|
type taskMessage struct {
|
|
|
|
// fields from type Task
|
|
|
|
Type string
|
|
|
|
Payload map[string]interface{}
|
|
|
|
|
|
|
|
//------- metadata fields ----------
|
|
|
|
// queue name this message should be enqueued to
|
2019-11-15 13:07:19 +08:00
|
|
|
Queue string
|
2019-11-18 05:25:01 +08:00
|
|
|
|
2019-11-18 10:44:40 +08:00
|
|
|
// max number of retry for this task.
|
2019-11-18 05:25:01 +08:00
|
|
|
Retry int
|
|
|
|
|
|
|
|
// number of times we've retried so far
|
|
|
|
Retried int
|
|
|
|
|
|
|
|
// error message from the last failure
|
|
|
|
ErrorMsg string
|
2019-11-15 13:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RedisOpt specifies redis options.
|
|
|
|
type RedisOpt struct {
|
|
|
|
Addr string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates and returns a new client.
|
|
|
|
func NewClient(opt *RedisOpt) *Client {
|
2019-11-20 11:44:41 +08:00
|
|
|
client := redis.NewClient(&redis.Options{Addr: opt.Addr, Password: opt.Password})
|
|
|
|
return &Client{rdb: newRDB(client)}
|
2019-11-15 13:07:19 +08:00
|
|
|
}
|
|
|
|
|
2019-11-17 06:45:51 +08:00
|
|
|
// Process enqueues the task to be performed at a given time.
|
|
|
|
func (c *Client) Process(task *Task, executeAt time.Time) error {
|
2019-11-18 05:25:01 +08:00
|
|
|
msg := &taskMessage{
|
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload,
|
2019-11-18 07:36:33 +08:00
|
|
|
Queue: "default",
|
2019-11-18 05:25:01 +08:00
|
|
|
Retry: defaultMaxRetry,
|
2019-11-15 13:07:19 +08:00
|
|
|
}
|
2019-11-18 07:36:33 +08:00
|
|
|
return c.enqueue(msg, executeAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// enqueue pushes a given task to the specified queue.
|
|
|
|
func (c *Client) enqueue(msg *taskMessage, executeAt time.Time) error {
|
2019-11-18 05:25:01 +08:00
|
|
|
if time.Now().After(executeAt) {
|
2019-11-20 11:44:41 +08:00
|
|
|
return c.rdb.push(msg)
|
2019-11-15 13:07:19 +08:00
|
|
|
}
|
2019-11-20 11:44:41 +08:00
|
|
|
return c.rdb.zadd(scheduled, float64(executeAt.Unix()), msg)
|
2019-11-15 13:07:19 +08:00
|
|
|
}
|
2019-11-15 23:21:25 +08:00
|
|
|
|
2019-11-19 22:48:54 +08:00
|
|
|
//-------------------- Launcher --------------------
|
2019-11-15 23:21:25 +08:00
|
|
|
|
2019-11-19 22:19:22 +08:00
|
|
|
// Launcher starts the manager and poller.
|
|
|
|
type Launcher struct {
|
2019-11-19 23:46:04 +08:00
|
|
|
// running indicates whether manager and poller are both running.
|
2019-11-18 13:21:32 +08:00
|
|
|
running bool
|
2019-11-19 23:46:04 +08:00
|
|
|
mu sync.Mutex
|
2019-11-19 13:23:49 +08:00
|
|
|
|
|
|
|
poller *poller
|
2019-11-19 22:48:54 +08:00
|
|
|
|
|
|
|
manager *manager
|
2019-11-15 23:21:25 +08:00
|
|
|
}
|
|
|
|
|
2019-11-19 22:19:22 +08:00
|
|
|
// NewLauncher creates and returns a new Launcher.
|
|
|
|
func NewLauncher(poolSize int, opt *RedisOpt) *Launcher {
|
2019-11-20 11:44:41 +08:00
|
|
|
client := redis.NewClient(&redis.Options{Addr: opt.Addr, Password: opt.Password})
|
|
|
|
rdb := newRDB(client)
|
|
|
|
poller := newPoller(rdb, 5*time.Second, []string{scheduled, retry})
|
2019-11-19 22:48:54 +08:00
|
|
|
manager := newManager(rdb, poolSize, nil)
|
2019-11-19 22:19:22 +08:00
|
|
|
return &Launcher{
|
2019-11-19 22:48:54 +08:00
|
|
|
poller: poller,
|
|
|
|
manager: manager,
|
2019-11-15 23:21:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 06:45:51 +08:00
|
|
|
// TaskHandler handles a given task and report any error.
|
|
|
|
type TaskHandler func(*Task) error
|
2019-11-15 23:21:25 +08:00
|
|
|
|
2019-11-19 22:48:54 +08:00
|
|
|
// Start starts the manager and poller.
|
|
|
|
func (l *Launcher) Start(handler TaskHandler) {
|
2019-11-19 23:46:04 +08:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2019-11-19 22:48:54 +08:00
|
|
|
if l.running {
|
2019-11-18 13:21:32 +08:00
|
|
|
return
|
|
|
|
}
|
2019-11-19 22:48:54 +08:00
|
|
|
l.running = true
|
|
|
|
l.manager.handler = handler
|
|
|
|
|
|
|
|
l.poller.start()
|
|
|
|
l.manager.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops both manager and poller.
|
|
|
|
func (l *Launcher) Stop() {
|
2019-11-19 23:46:04 +08:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2019-11-19 22:48:54 +08:00
|
|
|
if !l.running {
|
|
|
|
return
|
2019-11-15 23:21:25 +08:00
|
|
|
}
|
2019-11-19 22:48:54 +08:00
|
|
|
l.running = false
|
|
|
|
|
|
|
|
l.poller.terminate()
|
|
|
|
l.manager.terminate()
|
2019-11-15 23:21:25 +08:00
|
|
|
}
|