|
2 | 2 |
|
3 | 3 | This repository is demo how to use asyncq with handle complex job
|
4 | 4 |
|
| 5 | +## setup task on producer |
| 6 | + |
| 7 | +```golang |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "log" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/hibiken/asynq" |
| 15 | + "github.com/leetcode-golang-classroom/golang-sample-with-asyncq/internal/config" |
| 16 | + "github.com/leetcode-golang-classroom/golang-sample-with-asyncq/internal/tasks" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + redisClientOpt, err := asynq.ParseRedisURI(config.AppConfig.RedisURL) |
| 21 | + if err != nil { |
| 22 | + log.Fatalf("failed to parse redis url: %v", err) |
| 23 | + } |
| 24 | + client := asynq.NewClient(redisClientOpt) |
| 25 | + defer client.Close() |
| 26 | + |
| 27 | + task, err := tasks. NewEmailTask( "[email protected]", "Welcome!", "Thank you for signing up.") |
| 28 | + if err != nil { |
| 29 | + log.Fatalf("could not create task: %v\n", err) |
| 30 | + } |
| 31 | + |
| 32 | + info, err := client.Enqueue(task) |
| 33 | + if err != nil { |
| 34 | + log.Fatalf("could not enqueue task: %v", err) |
| 35 | + } |
| 36 | + log.Printf("enqueued task: id=%s queue=%s\n", info.ID, info.Queue) |
| 37 | + |
| 38 | + task, err = tasks.NewReportTask(10) |
| 39 | + if err != nil { |
| 40 | + log.Fatalf("could not create task: %v\n", err) |
| 41 | + } |
| 42 | + info, err = client.Enqueue(task, asynq.MaxRetry(10), asynq.Timeout(3*time.Minute)) |
| 43 | + if err != nil { |
| 44 | + log.Fatalf("could not enqueue task: %v\n", err) |
| 45 | + } |
| 46 | + log.Printf("enqueued task: id=%s queue=%s\n", info.ID, info.Queue) |
| 47 | + |
| 48 | + task, err = tasks.NewImageProcessingTask("http://test.com/image") |
| 49 | + if err != nil { |
| 50 | + log.Fatalf("could not create task: %v\n", err) |
| 51 | + } |
| 52 | + info, err = client.Enqueue(task, asynq.MaxRetry(10), asynq.Timeout(3*time.Minute)) |
| 53 | + if err != nil { |
| 54 | + log.Fatalf("could not enqueue task: %v\n", err) |
| 55 | + } |
| 56 | + log.Printf("enqueued task: id=%s queue=%s\n", info.ID, info.Queue) |
| 57 | + |
| 58 | + task, err = tasks. NewEmailTask( "[email protected]", "Welcome!", "Thank you for signing up.") |
| 59 | + if err != nil { |
| 60 | + log.Fatalf("could not create task: %v\n", err) |
| 61 | + } |
| 62 | + |
| 63 | + info, err = client.Enqueue(task, asynq.ProcessIn(10*time.Minute), asynq.Queue("critical")) |
| 64 | + if err != nil { |
| 65 | + log.Fatalf("could not enqueue task: %v", err) |
| 66 | + } |
| 67 | + log.Printf("enqueued task: id=%s queue=%s\n", info.ID, info.Queue) |
| 68 | +} |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +## setup consumer to consumer jobs |
| 73 | + |
| 74 | +```golang |
| 75 | +package main |
| 76 | + |
| 77 | +import ( |
| 78 | + "log" |
| 79 | + |
| 80 | + "github.com/hibiken/asynq" |
| 81 | + "github.com/leetcode-golang-classroom/golang-sample-with-asyncq/internal/config" |
| 82 | + "github.com/leetcode-golang-classroom/golang-sample-with-asyncq/internal/tasks" |
| 83 | +) |
| 84 | + |
| 85 | +func main() { |
| 86 | + redisClientOpt, err := asynq.ParseRedisURI(config.AppConfig.RedisURL) |
| 87 | + if err != nil { |
| 88 | + log.Fatalf("failed to parse redis url: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + srv := asynq.NewServer( |
| 92 | + redisClientOpt, |
| 93 | + asynq.Config{ |
| 94 | + Concurrency: 10, |
| 95 | + Queues: map[string]int{ |
| 96 | + "critical": 6, |
| 97 | + "default": 3, |
| 98 | + "low": 1, |
| 99 | + }, |
| 100 | + }, |
| 101 | + ) |
| 102 | + |
| 103 | + // mux maps a type to a handler |
| 104 | + mux := asynq.NewServeMux() |
| 105 | + mux.HandleFunc(tasks.TypeEmail, tasks.EmailTaskHandler) |
| 106 | + mux.HandleFunc(tasks.TypeReport, tasks.ReportTaskHandler) |
| 107 | + mux.Handle(tasks.TypeImageProcessing, tasks.NewImageProcessor()) |
| 108 | + if err := srv.Run(mux); err != nil { |
| 109 | + log.Fatalf("could not run server: %v", err) |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
0 commit comments