2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 11:05:58 +08:00

Change NewBackground API to take *redis.Client

This commit is contained in:
Ken Hibino 2019-12-29 14:50:46 -08:00
parent 62624cb0d8
commit fc71857c7c
4 changed files with 18 additions and 37 deletions

View File

@ -29,8 +29,11 @@ Asynq provides:
## Requirements ## Requirements
- Redis: 2.6+ | Dependency | Version |
- Go: 1.12+ | -------------------------------------------------------------- | ------- |
| [Redis](https://redis.io/) | v2.6+ |
| [Go](https://golang.org/) | v1.12+ |
| [github.com/go-redis/redis](https://github.com/go-redis/redis) | v.7.0+ |
## Installation ## Installation
@ -84,9 +87,10 @@ func main() {
```go ```go
func main() { func main() {
bg := asynq.NewBackground(10, &asynq.RedisOpt{ r := redis.NewClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
}) }
bg := asynq.NewBackground(r, 10)
// Blocks until signal TERM or INT is received. // Blocks until signal TERM or INT is received.
// For graceful shutdown, send signal TSTP to stop processing more tasks // For graceful shutdown, send signal TSTP to stop processing more tasks
@ -129,9 +133,10 @@ func handler(t *asynq.Task) error {
} }
func main() { func main() {
bg := asynq.NewBackground(10, &asynq.RedisOpt{ r := redis.NewClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
}) }
bg := asynq.NewBackground(r, 10)
// Use asynq.HandlerFunc adapter for a handler function // Use asynq.HandlerFunc adapter for a handler function
bg.Run(asynq.HandlerFunc(handler)) bg.Run(asynq.HandlerFunc(handler))

View File

@ -1,13 +1,10 @@
package asynq package asynq
import "github.com/go-redis/redis/v7"
/* /*
TODOs: TODOs:
- [P0] Pagination for `asynqmon ls` command - [P0] Pagination for `asynqmon ls` command
- [P0] Show elapsed time for InProgress tasks (asynqmon ls inprogress) - [P0] Show elapsed time for InProgress tasks (asynqmon ls inprogress)
- [P0] Go docs + CONTRIBUTION.md + Github issue template + License comment - [P0] Go docs + CONTRIBUTION.md + Github issue template + License comment
- [P0] Redis Sentinel support
- [P1] Add Support for multiple queues and priority - [P1] Add Support for multiple queues and priority
*/ */
@ -19,21 +16,3 @@ type Task struct {
// Payload holds data needed to process the task. // Payload holds data needed to process the task.
Payload Payload Payload Payload
} }
// RedisConfig specifies redis configurations.
// TODO(hibiken): Support more configuration.
type RedisConfig struct {
Addr string
Password string
// DB specifies which redis database to select.
DB int
}
func newRedisClient(cfg *RedisConfig) *redis.Client {
return redis.NewClient(&redis.Options{
Addr: cfg.Addr,
Password: cfg.Password,
DB: cfg.DB,
})
}

View File

@ -9,6 +9,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/go-redis/redis/v7"
"github.com/hibiken/asynq/internal/rdb" "github.com/hibiken/asynq/internal/rdb"
) )
@ -34,12 +35,12 @@ type Background struct {
// NewBackground returns a new Background with the specified number of workers // NewBackground returns a new Background with the specified number of workers
// given a redis configuration . // given a redis configuration .
func NewBackground(numWorkers int, cfg *RedisConfig) *Background { func NewBackground(r *redis.Client, numWorkers int) *Background {
r := rdb.NewRDB(newRedisClient(cfg)) rdb := rdb.NewRDB(r)
scheduler := newScheduler(r, 5*time.Second) scheduler := newScheduler(rdb, 5*time.Second)
processor := newProcessor(r, numWorkers, nil) processor := newProcessor(rdb, numWorkers, nil)
return &Background{ return &Background{
rdb: r, rdb: rdb,
scheduler: scheduler, scheduler: scheduler,
processor: processor, processor: processor,
} }

View File

@ -13,16 +13,12 @@ func TestBackground(t *testing.T) {
ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper") ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper")
defer goleak.VerifyNoLeaks(t, ignoreOpt) defer goleak.VerifyNoLeaks(t, ignoreOpt)
bg := NewBackground(10, &RedisConfig{
Addr: "localhost:6379",
DB: 15,
})
r := redis.NewClient(&redis.Options{ r := redis.NewClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
DB: 15, DB: 15,
}) })
client := NewClient(r) client := NewClient(r)
bg := NewBackground(r, 10)
// no-op handler // no-op handler
h := func(task *Task) error { h := func(task *Task) error {