2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 00:02:19 +08:00

Refactor enqueue logic for scheduled tasks

This commit is contained in:
Ken Hibino 2019-11-16 07:39:42 -08:00
parent e362e0daca
commit bda718bcaa

View File

@ -51,7 +51,6 @@ func NewClient(opt *RedisOpt) *Client {
// Enqueue pushes a given task to the specified queue. // Enqueue pushes a given task to the specified queue.
func (c *Client) Enqueue(queue string, task *Task, executeAt time.Time) error { func (c *Client) Enqueue(queue string, task *Task, executeAt time.Time) error {
if time.Now().After(executeAt) { if time.Now().After(executeAt) {
fmt.Println("going directly to queue")
bytes, err := json.Marshal(task) bytes, err := json.Marshal(task)
if err != nil { if err != nil {
return err return err
@ -156,24 +155,23 @@ func (w *Workers) pollScheduledTasks() {
continue continue
} }
// TODO(hibiken): Acquire lock for job.ID if w.rdb.ZRem(scheduled, j).Val() > 0 {
pipe := w.rdb.TxPipeline()
pipe.ZRem(scheduled, j)
// Do we need to encode this again? // Do we need to encode this again?
// Can we skip this entirely by defining Task field to be a string field? // Can we skip this entirely by defining Task field to be a string field?
bytes, err := json.Marshal(job.Task) bytes, err := json.Marshal(job.Task)
if err != nil { if err != nil {
log.Printf("could not marshal job.Task %v: %v\n", job.Task, err) log.Printf("could not marshal job.Task %v: %v\n", job.Task, err)
pipe.Discard() // TODO(hibiken): Put item that cannot marshal into dead queue.
continue continue
} }
pipe.RPush(queuePrefix+job.Queue, string(bytes)) err = w.rdb.RPush(queuePrefix+job.Queue, string(bytes)).Err()
_, err = pipe.Exec()
if err != nil { if err != nil {
log.Printf("could not execute pipeline: %v\n", err) log.Printf("command RPUSH %q %s failed: %v",
queuePrefix+job.Queue, string(bytes), err)
// TODO(hibiken): Handle this error properly. Add back to scheduled ZSET?
continue continue
} }
// TODO(hibiken): Release lock for job.ID }
} }
} }
} }