2019-11-20 13:19:46 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-11-22 22:16:43 +08:00
|
|
|
"github.com/google/uuid"
|
2019-11-20 13:19:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client is an interface for scheduling tasks.
|
|
|
|
type Client struct {
|
|
|
|
rdb *rdb
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates and returns a new client.
|
|
|
|
func NewClient(opt *RedisOpt) *Client {
|
2019-11-27 22:41:54 +08:00
|
|
|
return &Client{rdb: newRDB(opt)}
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process enqueues the task to be performed at a given time.
|
|
|
|
func (c *Client) Process(task *Task, executeAt time.Time) error {
|
|
|
|
msg := &taskMessage{
|
2019-11-22 22:16:43 +08:00
|
|
|
ID: uuid.New(),
|
2019-11-20 13:19:46 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload,
|
|
|
|
Queue: "default",
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
}
|
|
|
|
return c.enqueue(msg, executeAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// enqueue pushes a given task to the specified queue.
|
|
|
|
func (c *Client) enqueue(msg *taskMessage, executeAt time.Time) error {
|
|
|
|
if time.Now().After(executeAt) {
|
2019-11-26 11:58:24 +08:00
|
|
|
return c.rdb.enqueue(msg)
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
|
|
|
return c.rdb.zadd(scheduled, float64(executeAt.Unix()), msg)
|
|
|
|
}
|