mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-24 23:02:18 +08:00
Update package doc
This commit is contained in:
parent
c62833540c
commit
9256fb6023
24
doc.go
24
doc.go
@ -9,26 +9,27 @@ The Client is used to register a task to be processed at the specified time.
|
|||||||
|
|
||||||
client := asynq.NewClient(redis)
|
client := asynq.NewClient(redis)
|
||||||
|
|
||||||
t := &asynq.Task{
|
t := asynq.Task{
|
||||||
Type: "send_email",
|
Type: "send_email",
|
||||||
Payload: map[string]interface{}{"recipient_id": 123},
|
Payload: map[string]interface{}{"user_id": 42},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := client.Process(t, time.Now().Add(10 * time.Minute))
|
err := client.Process(&t, time.Now().Add(time.Minute))
|
||||||
|
|
||||||
The Background is used to run the background processing with a given
|
The Background is used to run the background task processing with a given
|
||||||
handler with the specified number of workers.
|
handler.
|
||||||
bg := asynq.NewBackground(redis, &asynq.Config{
|
bg := asynq.NewBackground(redis, &asynq.Config{
|
||||||
Concurrency: 20,
|
Concurrency: 10,
|
||||||
})
|
})
|
||||||
|
|
||||||
bg.Run(handler)
|
bg.Run(handler)
|
||||||
|
|
||||||
Handler is an interface that implements ProcessTask method that
|
Handler is an interface with one method ProcessTask which
|
||||||
takes a Task and return an error. Handler should return nil if
|
takes a task and returns an error. Handler should return nil if
|
||||||
the processing is successful, otherwise return non-nil error
|
the processing is successful, otherwise return a non-nil error.
|
||||||
so that the task will be retried after some delay.
|
If handler returns a non-nil error, the task will be retried in the future.
|
||||||
|
|
||||||
|
Example of a type that implements the Handler interface.
|
||||||
type TaskHandler struct {
|
type TaskHandler struct {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@ -36,7 +37,8 @@ so that the task will be retried after some delay.
|
|||||||
func (h *TaskHandler) ProcessTask(task *asynq.Task) error {
|
func (h *TaskHandler) ProcessTask(task *asynq.Task) error {
|
||||||
switch task.Type {
|
switch task.Type {
|
||||||
case "send_email":
|
case "send_email":
|
||||||
// send email logic
|
id, err := task.Payload.GetInt("user_id")
|
||||||
|
// send email
|
||||||
case "generate_thumbnail":
|
case "generate_thumbnail":
|
||||||
// generate thumbnail image
|
// generate thumbnail image
|
||||||
//...
|
//...
|
||||||
|
Loading…
Reference in New Issue
Block a user