2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-25 23:32:17 +08:00
asynq/client.go

40 lines
896 B
Go
Raw Normal View History

package asynq
import (
"time"
2019-11-22 22:16:43 +08:00
"github.com/google/uuid"
2019-12-04 13:01:26 +08:00
"github.com/hibiken/asynq/internal/rdb"
)
// Client is an interface for scheduling tasks.
type Client struct {
2019-12-04 13:01:26 +08:00
rdb *rdb.RDB
}
// NewClient creates and returns a new client.
2019-12-04 11:43:01 +08:00
func NewClient(config *RedisConfig) *Client {
2019-12-04 13:01:26 +08:00
r := rdb.NewRDB(newRedisClient(config))
return &Client{r}
}
// 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-12-04 13:01:26 +08:00
msg := &rdb.TaskMessage{
2019-11-22 22:16:43 +08:00
ID: uuid.New(),
Type: task.Type,
Payload: task.Payload,
Queue: "default",
Retry: defaultMaxRetry,
}
2019-11-27 23:16:16 +08:00
return c.enqueue(msg, processAt)
}
// enqueue pushes a given task to the specified queue.
2019-12-04 13:01:26 +08:00
func (c *Client) enqueue(msg *rdb.TaskMessage, processAt time.Time) error {
2019-11-27 23:16:16 +08:00
if time.Now().After(processAt) {
2019-12-04 13:01:26 +08:00
return c.rdb.Enqueue(msg)
}
return c.rdb.Schedule(msg, processAt)
}