2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 02:55:54 +08:00

Add Config type to configure background processing behavior

This commit is contained in:
Ken Hibino 2019-12-29 16:55:51 -08:00
parent fc71857c7c
commit 9af14d9a6d
5 changed files with 34 additions and 13 deletions

View File

@ -90,7 +90,9 @@ func main() {
r := redis.NewClient(&redis.Options{ r := redis.NewClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
} }
bg := asynq.NewBackground(r, 10) bg := asynq.NewBackground(r, &asynq.Config{
Concurrency: 20,
})
// 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
@ -136,7 +138,9 @@ func main() {
r := redis.NewClient(&redis.Options{ r := redis.NewClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
} }
bg := asynq.NewBackground(r, 10) bg := asynq.NewBackground(r, &asynq.Config{
Concurrency: 20,
})
// 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

@ -33,12 +33,29 @@ type Background struct {
processor *processor processor *processor
} }
// NewBackground returns a new Background with the specified number of workers // Config specifies the background-task processing behavior.
// given a redis configuration . type Config struct {
func NewBackground(r *redis.Client, numWorkers int) *Background { // Max number of concurrent workers to process tasks.
//
// If set to zero or negative value, NewBackground will overwrite the value to one.
Concurrency int
// TODO(hibiken): Add ShutdownTimeout
// ShutdownTimeout time.Duration
// TODO(hibiken): Add RetryDelayFunc
}
// NewBackground returns a new Background instance given a redis client
// and background processing configuration.
func NewBackground(r *redis.Client, cfg *Config) *Background {
n := cfg.Concurrency
if n < 1 {
n = 1
}
rdb := rdb.NewRDB(r) rdb := rdb.NewRDB(r)
scheduler := newScheduler(rdb, 5*time.Second) scheduler := newScheduler(rdb, 5*time.Second)
processor := newProcessor(rdb, numWorkers, nil) processor := newProcessor(rdb, n, nil)
return &Background{ return &Background{
rdb: rdb, rdb: rdb,
scheduler: scheduler, scheduler: scheduler,

View File

@ -18,7 +18,9 @@ func TestBackground(t *testing.T) {
DB: 15, DB: 15,
}) })
client := NewClient(r) client := NewClient(r)
bg := NewBackground(r, 10) bg := NewBackground(r, &Config{
Concurrency: 10,
})
// no-op handler // no-op handler
h := func(task *Task) error { h := func(task *Task) error {

View File

@ -25,7 +25,7 @@ func NewClient(r *redis.Client) *Client {
return &Client{rdb} return &Client{rdb}
} }
// Option configures the behavior of task processing. // Option specifies the processing behavior for the associated task.
type Option interface{} type Option interface{}
// max number of times a task will be retried. // max number of times a task will be retried.

8
doc.go
View File

@ -3,9 +3,7 @@ Package asynq provides a framework for background task processing.
The Client is used to register a task to be processed at the specified time. The Client is used to register a task to be processed at the specified time.
client := asynq.NewClient(&asynq.RedisConfig{ client := asynq.NewClient(redis)
Addr: "localhost:6379",
})
t := &asynq.Task{ t := &asynq.Task{
Type: "send_email", Type: "send_email",
@ -16,8 +14,8 @@ The Client is used to register a task to be processed at the specified time.
The Background is used to run the background processing with a given The Background is used to run the background processing with a given
handler with the specified number of workers. handler with the specified number of workers.
bg := asynq.NewBackground(20, &asynq.RedisConfig{ bg := asynq.NewBackground(redis, &asynq.Config{
Addr: "localhost:6379", Concurrency: 20,
}) })
bg.Run(handler) bg.Run(handler)