mirror of
https://github.com/hibiken/asynq.git
synced 2025-01-11 23:43:38 +08:00
Change NewBackground API to take *redis.Client
This commit is contained in:
parent
62624cb0d8
commit
fc71857c7c
17
README.md
17
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))
|
||||
|
21
asynq.go
21
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,
|
||||
})
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user