2
0
mirror of https://github.com/hibiken/asynq.git synced 2025-02-23 12:20:19 +08:00

Update readme

This commit is contained in:
Ken Hibino 2021-06-30 06:26:14 -07:00
parent d02b722d8a
commit d5e9f3b1bd

View File

@ -26,7 +26,6 @@ Task queues are used as a mechanism to distribute work across multiple machines.
- Guaranteed [at least one execution](https://www.cloudcomputingpatterns.org/at_least_once_delivery/) of a task - Guaranteed [at least one execution](https://www.cloudcomputingpatterns.org/at_least_once_delivery/) of a task
- Scheduling of tasks - Scheduling of tasks
- Durability since tasks are written to Redis
- [Retries](https://github.com/hibiken/asynq/wiki/Task-Retry) of failed tasks - [Retries](https://github.com/hibiken/asynq/wiki/Task-Retry) of failed tasks
- Automatic recovery of tasks in the event of a worker crash - Automatic recovery of tasks in the event of a worker crash
- [Weighted priority queues](https://github.com/hibiken/asynq/wiki/Priority-Queues#weighted-priority-queues) - [Weighted priority queues](https://github.com/hibiken/asynq/wiki/Priority-Queues#weighted-priority-queues)
@ -58,7 +57,7 @@ Initialize your project by creating a folder and then running `go mod init githu
go get -u github.com/hibiken/asynq go get -u github.com/hibiken/asynq
``` ```
Make sure you're running a Redis server locally or from a [Docker](https://hub.docker.com/_/redis) container. Version `3.0` or higher is required. Make sure you're running a Redis server locally or from a [Docker](https://hub.docker.com/_/redis) container. Version `4.0` or higher is required.
Next, write a package that encapsulates task creation and task handling. Next, write a package that encapsulates task creation and task handling.
@ -120,7 +119,7 @@ func HandleEmailDeliveryTask(ctx context.Context, t *asynq.Task) error {
if err := json.Unmarshal(t.Payload(), &p); err != nil { if err := json.Unmarshal(t.Payload(), &p); err != nil {
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry) return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
} }
log.Printf("Sending Email to User: user_id = %d, template_id = %s\n", p.UserID, p.TemplateID) log.Printf("Sending Email to User: user_id=%d, template_id=%s", p.UserID, p.TemplateID)
// Email delivery code ... // Email delivery code ...
return nil return nil
} }
@ -135,7 +134,7 @@ func (p *ImageProcessor) ProcessTask(ctx context.Context, t *asynq.Task) error {
if err := json.Unmarshal(t.Payload(), &p); err != nil { if err := json.Unmarshal(t.Payload(), &p); err != nil {
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry) return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
} }
log.Printf("Resizing image: src = %s\n", p.SourceURL) log.Printf("Resizing image: src=%s", p.SourceURL)
// Image resizing code ... // Image resizing code ...
return nil return nil
} }
@ -145,13 +144,12 @@ func NewImageProcessor() *ImageProcessor {
} }
``` ```
In your application code, import the above package and use [`Client`](https://pkg.go.dev/github.com/hibiken/asynq?tab=doc#Client) to put tasks on the queue. In your application code, import the above package and use [`Client`](https://pkg.go.dev/github.com/hibiken/asynq?tab=doc#Client) to put tasks on queues.
```go ```go
package main package main
import ( import (
"fmt"
"log" "log"
"time" "time"
@ -178,7 +176,7 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("could not enqueue task: %v", err) log.Fatalf("could not enqueue task: %v", err)
} }
fmt.Printf("enqueued task: id=%s queue=%s\n", info.ID, info.Queue) log.Printf("enqueued task: id=%s queue=%s", info.ID, info.Queue)
// ------------------------------------------------------------ // ------------------------------------------------------------
@ -190,7 +188,7 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("could not schedule task: %v", err) log.Fatalf("could not schedule task: %v", err)
} }
fmt.Printf("enqueued task: id=%s queue=%s\n", info.ID, info.Queue) log.Printf("enqueued task: id=%s queue=%s", info.ID, info.Queue)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -208,7 +206,7 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("could not enqueue task: %v", err) log.Fatalf("could not enqueue task: %v", err)
} }
fmt.Printf("enqueued task: id=%s queue=%s\n", info.ID, info.Queue) log.Printf("enqueued task: id=%s queue=%s", info.ID, info.Queue)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Example 4: Pass options to tune task processing behavior at enqueue time. // Example 4: Pass options to tune task processing behavior at enqueue time.
@ -219,7 +217,7 @@ func main() {
if err != nil { if err != nil {
log.Fatal("could not enqueue task: %v", err) log.Fatal("could not enqueue task: %v", err)
} }
fmt.Printf("enqueued task: id=%s queue=%s\n", info.ID, info.Queue) log.Printf("enqueued task: id=%s queue=%s", info.ID, info.Queue)
} }
``` ```