mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-27 00:02:19 +08:00
Update example code in README
This commit is contained in:
parent
27baf6de0d
commit
bf54621196
38
README.md
38
README.md
@ -92,21 +92,19 @@ type ImageResizePayload struct {
|
|||||||
//----------------------------------------------
|
//----------------------------------------------
|
||||||
|
|
||||||
func NewEmailDeliveryTask(userID int, tmplID string) (*asynq.Task, error) {
|
func NewEmailDeliveryTask(userID int, tmplID string) (*asynq.Task, error) {
|
||||||
payload := EmailDeliveryPayload{UserID: userID, TemplateID: templID}
|
payload, err := json.Marshal(EmailDeliveryPayload{UserID: userID, TemplateID: templID})
|
||||||
bytes, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return asynq.NewTask(TypeEmailDelivery, bytes), nil
|
return asynq.NewTask(TypeEmailDelivery, payload), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewImageResizeTask(src string) (*asynq.Task, error) {
|
func NewImageResizeTask(src string) (*asynq.Task, error) {
|
||||||
payload := ImageResizePayload{SourceURL: src}
|
payload, err := json.Marshal(ImageResizePayload{SourceURL: src})
|
||||||
bytes, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return asynq.NewTask(TypeImageResize, bytes), nil
|
return asynq.NewTask(TypeImageResize, payload), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------
|
//---------------------------------------------------------------
|
||||||
@ -164,20 +162,19 @@ import (
|
|||||||
const redisAddr = "127.0.0.1:6379"
|
const redisAddr = "127.0.0.1:6379"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := asynq.RedisClientOpt{Addr: redisAddr}
|
client := asynq.NewClient(asynq.RedisClientOpt{Addr: redisAddr})
|
||||||
c := asynq.NewClient(r)
|
defer client.Close()
|
||||||
defer c.Close()
|
|
||||||
|
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
// Example 1: Enqueue task to be processed immediately.
|
// Example 1: Enqueue task to be processed immediately.
|
||||||
// Use (*Client).Enqueue method.
|
// Use (*Client).Enqueue method.
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
|
|
||||||
t, err := tasks.NewEmailDeliveryTask(42, "some:template:id")
|
task, err := tasks.NewEmailDeliveryTask(42, "some:template:id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("could not create task: %v", err)
|
log.Fatalf("could not create task: %v", err)
|
||||||
}
|
}
|
||||||
info, err := c.Enqueue(t)
|
info, err := client.Enqueue(task)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("could not enqueue task: %v", err)
|
log.Fatalf("could not enqueue task: %v", err)
|
||||||
}
|
}
|
||||||
@ -189,7 +186,7 @@ func main() {
|
|||||||
// Use ProcessIn or ProcessAt option.
|
// Use ProcessIn or ProcessAt option.
|
||||||
// ------------------------------------------------------------
|
// ------------------------------------------------------------
|
||||||
|
|
||||||
info, err = c.Enqueue(t, asynq.ProcessIn(24*time.Hour))
|
info, err = client.Enqueue(task, asynq.ProcessIn(24*time.Hour))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("could not schedule task: %v", err)
|
log.Fatalf("could not schedule task: %v", err)
|
||||||
}
|
}
|
||||||
@ -201,13 +198,13 @@ func main() {
|
|||||||
// Options include MaxRetry, Queue, Timeout, Deadline, Unique etc.
|
// Options include MaxRetry, Queue, Timeout, Deadline, Unique etc.
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
c.SetDefaultOptions(tasks.TypeImageResize, asynq.MaxRetry(10), asynq.Timeout(3*time.Minute))
|
client.SetDefaultOptions(tasks.TypeImageResize, asynq.MaxRetry(10), asynq.Timeout(3*time.Minute))
|
||||||
|
|
||||||
t, err = tasks.NewImageResizeTask("https://example.com/myassets/image.jpg")
|
task, err = tasks.NewImageResizeTask("https://example.com/myassets/image.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("could not create task: %v", err)
|
log.Fatalf("could not create task: %v", err)
|
||||||
}
|
}
|
||||||
info, err = c.Enqueue(t)
|
info, err = client.Enqueue(task)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("could not enqueue task: %v", err)
|
log.Fatalf("could not enqueue task: %v", err)
|
||||||
}
|
}
|
||||||
@ -218,7 +215,7 @@ func main() {
|
|||||||
// Options passed at enqueue time override default ones.
|
// Options passed at enqueue time override default ones.
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
info, err = c.Enqueue(t, asynq.Queue("critical"), asynq.Timeout(30*time.Second))
|
info, err = client.Enqueue(task, asynq.Queue("critical"), asynq.Timeout(30*time.Second))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("could not enqueue task: %v", err)
|
log.Fatal("could not enqueue task: %v", err)
|
||||||
}
|
}
|
||||||
@ -243,9 +240,9 @@ import (
|
|||||||
const redisAddr = "127.0.0.1:6379"
|
const redisAddr = "127.0.0.1:6379"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := asynq.RedisClientOpt{Addr: redisAddr}
|
srv := asynq.NewServer(
|
||||||
|
asynq.RedisClientOpt{Addr: redisAddr}
|
||||||
srv := asynq.NewServer(r, asynq.Config{
|
asynq.Config{
|
||||||
// Specify how many concurrent workers to use
|
// Specify how many concurrent workers to use
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
// Optionally specify multiple queues with different priority.
|
// Optionally specify multiple queues with different priority.
|
||||||
@ -255,7 +252,8 @@ func main() {
|
|||||||
"low": 1,
|
"low": 1,
|
||||||
},
|
},
|
||||||
// See the godoc for other configuration options
|
// See the godoc for other configuration options
|
||||||
})
|
},
|
||||||
|
)
|
||||||
|
|
||||||
// mux maps a type to a handler
|
// mux maps a type to a handler
|
||||||
mux := asynq.NewServeMux()
|
mux := asynq.NewServeMux()
|
||||||
|
Loading…
Reference in New Issue
Block a user