2020-01-03 10:13:16 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-12-06 22:12:44 +08:00
|
|
|
/*
|
2020-01-23 22:33:34 +08:00
|
|
|
Package asynq provides a framework for asynchronous task processing.
|
2019-12-06 22:12:44 +08:00
|
|
|
|
2020-01-15 13:19:06 +08:00
|
|
|
Asynq uses Redis as a message broker. To connect to redis server,
|
|
|
|
specify the options using one of RedisConnOpt types.
|
|
|
|
|
|
|
|
redis = &asynq.RedisClientOpt{
|
2020-02-24 07:40:04 +08:00
|
|
|
Addr: "127.0.0.1:6379",
|
|
|
|
Password: "xxxxx",
|
2020-01-15 13:19:06 +08:00
|
|
|
DB: 3,
|
|
|
|
}
|
|
|
|
|
2019-12-06 22:12:44 +08:00
|
|
|
The Client is used to register a task to be processed at the specified time.
|
|
|
|
|
2020-01-15 13:19:06 +08:00
|
|
|
Task is created with two parameters: its type and payload.
|
|
|
|
|
|
|
|
client := asynq.NewClient(redis)
|
2019-12-06 22:12:44 +08:00
|
|
|
|
2020-01-15 13:19:06 +08:00
|
|
|
t := asynq.NewTask(
|
|
|
|
"send_email",
|
|
|
|
map[string]interface{}{"user_id": 42})
|
2019-12-06 22:12:44 +08:00
|
|
|
|
2020-02-24 07:40:04 +08:00
|
|
|
// Enqueue the task to be processed immediately.
|
|
|
|
err := client.Enqueue(t)
|
|
|
|
|
|
|
|
// Schedule the task to be processed in one minute.
|
|
|
|
err = client.EnqueueIn(time.Minute, t)
|
2019-12-06 22:12:44 +08:00
|
|
|
|
2020-04-13 08:16:44 +08:00
|
|
|
The Server is used to run the background task processing with a given
|
2020-01-03 11:47:04 +08:00
|
|
|
handler.
|
2020-04-13 08:16:44 +08:00
|
|
|
srv := asynq.NewServer(redis, asynq.Config{
|
2020-01-03 11:47:04 +08:00
|
|
|
Concurrency: 10,
|
2019-12-06 22:12:44 +08:00
|
|
|
})
|
|
|
|
|
2020-04-13 08:16:44 +08:00
|
|
|
srv.Run(handler)
|
2019-12-06 22:12:44 +08:00
|
|
|
|
2020-01-03 11:47:04 +08:00
|
|
|
Handler is an interface with one method ProcessTask which
|
|
|
|
takes a task and returns an error. Handler should return nil if
|
|
|
|
the processing is successful, otherwise return a non-nil error.
|
2020-01-15 13:19:06 +08:00
|
|
|
If handler panics or returns a non-nil error, the task will be retried in the future.
|
2019-12-06 22:12:44 +08:00
|
|
|
|
2020-01-03 11:47:04 +08:00
|
|
|
Example of a type that implements the Handler interface.
|
2019-12-06 22:12:44 +08:00
|
|
|
type TaskHandler struct {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:46 +08:00
|
|
|
func (h *TaskHandler) ProcessTask(ctx context.Context, task *asynq.Task) error {
|
2019-12-06 22:12:44 +08:00
|
|
|
switch task.Type {
|
|
|
|
case "send_email":
|
2020-01-03 11:47:04 +08:00
|
|
|
id, err := task.Payload.GetInt("user_id")
|
|
|
|
// send email
|
2019-12-06 22:12:44 +08:00
|
|
|
//...
|
|
|
|
default:
|
2020-01-15 13:19:06 +08:00
|
|
|
return fmt.Errorf("unexpected task type %q", task.Type)
|
2019-12-06 22:12:44 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
package asynq
|