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.
|
2019-12-04 11:43:01 +08:00
|
|
|
func NewClient(config *RedisConfig) *Client {
|
|
|
|
return &Client{rdb: newRDB(config)}
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process enqueues the task to be performed at a given time.
|
2019-11-27 23:16:16 +08:00
|
|
|
func (c *Client) Process(task *Task, processAt time.Time) error {
|
2019-11-20 13:19:46 +08:00
|
|
|
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,
|
|
|
|
}
|
2019-11-27 23:16:16 +08:00
|
|
|
return c.enqueue(msg, processAt)
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// enqueue pushes a given task to the specified queue.
|
2019-11-27 23:16:16 +08:00
|
|
|
func (c *Client) enqueue(msg *taskMessage, processAt time.Time) error {
|
|
|
|
if time.Now().After(processAt) {
|
2019-11-26 11:58:24 +08:00
|
|
|
return c.rdb.enqueue(msg)
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|
2019-11-27 23:16:16 +08:00
|
|
|
return c.rdb.schedule(scheduled, processAt, msg)
|
2019-11-20 13:19:46 +08:00
|
|
|
}
|