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
|
|
|
/*
|
|
|
|
Package asynq provides a framework for background task processing.
|
|
|
|
|
|
|
|
The Client is used to register a task to be processed at the specified time.
|
|
|
|
|
2019-12-30 08:55:51 +08:00
|
|
|
client := asynq.NewClient(redis)
|
2019-12-06 22:12:44 +08:00
|
|
|
|
2020-01-05 05:13:46 +08:00
|
|
|
t := asynq.NewTask(
|
|
|
|
"send_email",
|
|
|
|
map[string]interface{}{"user_id": 42})
|
2019-12-06 22:12:44 +08:00
|
|
|
|
2020-01-05 05:13:46 +08:00
|
|
|
err := client.Schedule(t, time.Now().Add(time.Minute))
|
2019-12-06 22:12:44 +08:00
|
|
|
|
2020-01-03 11:47:04 +08:00
|
|
|
The Background is used to run the background task processing with a given
|
|
|
|
handler.
|
2019-12-30 08:55:51 +08:00
|
|
|
bg := asynq.NewBackground(redis, &asynq.Config{
|
2020-01-03 11:47:04 +08:00
|
|
|
Concurrency: 10,
|
2019-12-06 22:12:44 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
bg.Run(handler)
|
|
|
|
|
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.
|
|
|
|
If handler 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 {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *TaskHandler) ProcessTask(task *asynq.Task) error {
|
|
|
|
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
|
|
|
case "generate_thumbnail":
|
|
|
|
// generate thumbnail image
|
|
|
|
//...
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unepected task type %q", task.Type)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
package asynq
|