2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-11-10 11:31:58 +08:00

Minor tweak to readme example code

This commit is contained in:
Ken Hibino 2020-03-13 14:50:03 -07:00
parent 1a53bbf21b
commit 310d38620d

View File

@ -32,28 +32,34 @@ func main() {
Addr: "127.0.0.1:6379", Addr: "127.0.0.1:6379",
} }
client := asynq.NewClient(r) c := asynq.NewClient(r)
// Create a task with task type and payload. // Example 1: Enqueue task to be processed immediately.
t1 := asynq.NewTask("email:signup", map[string]interface{}{"user_id": 42})
t2 := asynq.NewTask("email:reminder", map[string]interface{}{"user_id": 42}) t := asynq.NewTask("email:signup", map[string]interface{}{"user_id": 42})
err := c.Enqueue(t)
if err != nil {
log.Fatal("could not enqueue task: %v", err)
}
// Enqueue immediately.
err := client.Enqueue(t1)
// Enqueue 24 hrs later. // Example 2: Schedule task to be processed in the future.
err = client.EnqueueIn(24*time.Hour, t2)
// Enqueue at specific time. t = asynq.NewTask("email:reminder", map[string]interface{}{"user_id": 42})
err = client.EnqueueAt(time.Date(2020, time.March, 6, 10, 0, 0, 0, time.UTC), t2) err = c.EnqueueIn(24*time.Hour, t)
if err != nil {
log.Fatal("could not schedule task: %v", err)
}
// Pass vararg options to specify processing behavior for the given task.
// // Example 3: Pass options to tune task processing behavior.
// MaxRetry specifies the max number of retry if the task fails (Default is 25). // Options include MaxRetry, Queue, Timeout, Deadline, etc.
// Queue specifies which queue to enqueue this task to (Default is "default" queue).
// Timeout specifies the the task timeout (Default is no timeout). t = asynq.NewTask("email:reminder", map[string]interface{}{"user_id": 42})
err = client.Enqueue(t1, asynq.MaxRetry(10), asynq.Queue("critical"), asynq.Timeout(time.Minute)) err = c.Enqueue(t, asynq.MaxRetry(10), asynq.Queue("critical"), asynq.Timeout(time.Minute))
if err != nil {
log.Fatal("could not enqueue task: %v", err)
}
} }
``` ```
@ -99,7 +105,7 @@ func main() {
bg.Run(mux) bg.Run(mux)
} }
// function with the same signature as the ProcessTask method for the Handler interface. // function with the same signature as the sole method for the Handler interface.
func signupEmailHandler(ctx context.Context, t *asynq.Task) error { func signupEmailHandler(ctx context.Context, t *asynq.Task) error {
id, err := t.Payload.GetInt("user_id") id, err := t.Payload.GetInt("user_id")
if err != nil { if err != nil {