From fc71857c7c928668d45973e661ce670dc33a1fec Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 29 Dec 2019 14:50:46 -0800 Subject: [PATCH] Change NewBackground API to take *redis.Client --- README.md | 17 +++++++++++------ asynq.go | 21 --------------------- background.go | 11 ++++++----- background_test.go | 6 +----- 4 files changed, 18 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 767cf5a..fa5d2d0 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,11 @@ Asynq provides: ## Requirements -- Redis: 2.6+ -- Go: 1.12+ +| Dependency | Version | +| -------------------------------------------------------------- | ------- | +| [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 @@ -84,9 +87,10 @@ func main() { ```go func main() { - bg := asynq.NewBackground(10, &asynq.RedisOpt{ + r := redis.NewClient(&redis.Options{ Addr: "localhost:6379", - }) + } + bg := asynq.NewBackground(r, 10) // Blocks until signal TERM or INT is received. // For graceful shutdown, send signal TSTP to stop processing more tasks @@ -129,9 +133,10 @@ func handler(t *asynq.Task) error { } func main() { - bg := asynq.NewBackground(10, &asynq.RedisOpt{ + r := redis.NewClient(&redis.Options{ Addr: "localhost:6379", - }) + } + bg := asynq.NewBackground(r, 10) // Use asynq.HandlerFunc adapter for a handler function bg.Run(asynq.HandlerFunc(handler)) diff --git a/asynq.go b/asynq.go index febf174..3830e0f 100644 --- a/asynq.go +++ b/asynq.go @@ -1,13 +1,10 @@ package asynq -import "github.com/go-redis/redis/v7" - /* TODOs: - [P0] Pagination for `asynqmon ls` command - [P0] Show elapsed time for InProgress tasks (asynqmon ls inprogress) - [P0] Go docs + CONTRIBUTION.md + Github issue template + License comment -- [P0] Redis Sentinel support - [P1] Add Support for multiple queues and priority */ @@ -19,21 +16,3 @@ type Task struct { // Payload holds data needed to process the task. 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, - }) -} diff --git a/background.go b/background.go index 6c87d0d..9d5b719 100644 --- a/background.go +++ b/background.go @@ -9,6 +9,7 @@ import ( "syscall" "time" + "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" ) @@ -34,12 +35,12 @@ type Background struct { // NewBackground returns a new Background with the specified number of workers // given a redis configuration . -func NewBackground(numWorkers int, cfg *RedisConfig) *Background { - r := rdb.NewRDB(newRedisClient(cfg)) - scheduler := newScheduler(r, 5*time.Second) - processor := newProcessor(r, numWorkers, nil) +func NewBackground(r *redis.Client, numWorkers int) *Background { + rdb := rdb.NewRDB(r) + scheduler := newScheduler(rdb, 5*time.Second) + processor := newProcessor(rdb, numWorkers, nil) return &Background{ - rdb: r, + rdb: rdb, scheduler: scheduler, processor: processor, } diff --git a/background_test.go b/background_test.go index 263627f..489bebc 100644 --- a/background_test.go +++ b/background_test.go @@ -13,16 +13,12 @@ func TestBackground(t *testing.T) { ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper") defer goleak.VerifyNoLeaks(t, ignoreOpt) - bg := NewBackground(10, &RedisConfig{ - Addr: "localhost:6379", - DB: 15, - }) - r := redis.NewClient(&redis.Options{ Addr: "localhost:6379", DB: 15, }) client := NewClient(r) + bg := NewBackground(r, 10) // no-op handler h := func(task *Task) error {