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{
Addr: "localhost:6379",
}
bg := asynq.NewBackground(r, 10)
bg := asynq.NewBackground(r, &asynq.Config{
Concurrency: 20,
})
// Blocks until signal TERM or INT is received.
// For graceful shutdown, send signal TSTP to stop processing more tasks
@ -136,7 +138,9 @@ func main() {
r := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
}
bg := asynq.NewBackground(r, 10)
bg := asynq.NewBackground(r, &asynq.Config{
Concurrency: 20,
})
// Use asynq.HandlerFunc adapter for a handler function
bg.Run(asynq.HandlerFunc(handler))

View File

@ -33,12 +33,29 @@ type Background struct {
processor *processor
}
// NewBackground returns a new Background with the specified number of workers
// given a redis configuration .
func NewBackground(r *redis.Client, numWorkers int) *Background {
// Config specifies the background-task processing behavior.
type Config struct {
// 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)
scheduler := newScheduler(rdb, 5*time.Second)
processor := newProcessor(rdb, numWorkers, nil)
processor := newProcessor(rdb, n, nil)
return &Background{
rdb: rdb,
scheduler: scheduler,

View File

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

View File

@ -25,7 +25,7 @@ func NewClient(r *redis.Client) *Client {
return &Client{rdb}
}
// Option configures the behavior of task processing.
// Option specifies the processing behavior for the associated task.
type Option interface{}
// 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.
client := asynq.NewClient(&asynq.RedisConfig{
Addr: "localhost:6379",
})
client := asynq.NewClient(redis)
t := &asynq.Task{
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
handler with the specified number of workers.
bg := asynq.NewBackground(20, &asynq.RedisConfig{
Addr: "localhost:6379",
bg := asynq.NewBackground(redis, &asynq.Config{
Concurrency: 20,
})
bg.Run(handler)