diff --git a/config/queue.go b/config/queue.go index 2b025f3..3249cf8 100644 --- a/config/queue.go +++ b/config/queue.go @@ -1,5 +1,7 @@ package config +import "time" + // queueDriver is how jobs run: sync where they are dispatched, redis on // the worker. type queueDriver string @@ -27,6 +29,9 @@ type Queue struct { // Concurrency is how many jobs of the default queue run at once: every // job registered outside a group. Concurrency int + // CheckInterval is how long an idle default worker waits before it + // looks for work again; 0 keeps asynq's second, with jitter. + CheckInterval time.Duration // Groups is each queue group's workers of its own, by the name // engine.Group registers its jobs under. A group registered without one // stops the worker from starting. @@ -41,6 +46,10 @@ type QueueGroup struct { // Concurrency is how many of its jobs run at once, on workers of its // own: the other queues never take them. Concurrency int + // CheckInterval is how long its idle workers wait before they look for + // work again; 0 keeps asynq's second, with jitter. A job someone waits + // on wants it short: on an idle queue it can sit up to 1.5s otherwise. + CheckInterval time.Duration } type RedisQueueConnection struct { diff --git a/queueing/resolvers/redis.go b/queueing/resolvers/redis.go index ec07fb5..98a271c 100644 --- a/queueing/resolvers/redis.go +++ b/queueing/resolvers/redis.go @@ -182,8 +182,9 @@ func serve(servers []*asynq.Server, handler asynq.Handler) error { // job is retried and failed, the same on every server. func (e *Redis) config(jobs map[string]job.Job, server Server) asynq.Config { return asynq.Config{ - Concurrency: server.Concurrency, - Queues: server.Queues, + Concurrency: server.Concurrency, + TaskCheckInterval: server.CheckInterval, + Queues: server.Queues, RetryDelayFunc: func(retries int, err error, task *asynq.Task) time.Duration { if release, ok := job.AsRelease(err); ok { return release.Delay diff --git a/queueing/resolvers/servers.go b/queueing/resolvers/servers.go index bc6d2e8..e1a6448 100644 --- a/queueing/resolvers/servers.go +++ b/queueing/resolvers/servers.go @@ -4,6 +4,7 @@ import ( "fmt" "maps" "slices" + "time" "github.com/gonstruct/core/config" "github.com/gonstruct/core/queueing/job" @@ -18,8 +19,9 @@ const DefaultQueue = "default" // group's queue or, with no Group, the default workers on the queues of // every job outside a group. type Server struct { - Group string - Concurrency int + Group string + Concurrency int + CheckInterval time.Duration // Queues is the queues it takes jobs from, by weight. Queues map[string]int } @@ -46,7 +48,12 @@ func Servers(jobs map[string]job.Job, groupOf map[string]string, queue config.Qu if workers < 1 { return nil, fmt.Errorf("[queueing] queue group %s has no workers: give it a Concurrency in the config's Groups", group) } - servers = append(servers, Server{Group: group, Concurrency: workers, Queues: map[string]int{group: 1}}) + servers = append(servers, Server{ + Group: group, + Concurrency: workers, + CheckInterval: queue.Groups[group].CheckInterval, + Queues: map[string]int{group: 1}, + }) } queues := map[string]int{} @@ -73,5 +80,5 @@ func Servers(jobs map[string]job.Job, groupOf map[string]string, queue config.Qu return nil, fmt.Errorf("[queueing] the default queue has no workers: give the config a Concurrency") } - return append(servers, Server{Concurrency: queue.Concurrency, Queues: queues}), nil + return append(servers, Server{Concurrency: queue.Concurrency, CheckInterval: queue.CheckInterval, Queues: queues}), nil } diff --git a/tests/queueing/resolvers/servers_test.go b/tests/queueing/resolvers/servers_test.go index 50acac3..6b690ba 100644 --- a/tests/queueing/resolvers/servers_test.go +++ b/tests/queueing/resolvers/servers_test.go @@ -4,6 +4,7 @@ import ( "context" "reflect" "testing" + "time" "github.com/gonstruct/core/config" "github.com/gonstruct/core/queueing/job" @@ -86,6 +87,20 @@ func TestServers(t *testing.T) { config: config.Queue{Groups: map[string]config.QueueGroup{"ci": {Concurrency: 150}}}, want: []resolvers.Server{{Group: "ci", Concurrency: 150, Queues: map[string]int{"ci": 1}}}, }, + { + name: "each server looks for work as often as its config says", + jobs: jobs(&queued{name: "launch"}, &plain{}), + groups: map[string]string{"launch": "ci"}, + config: config.Queue{ + Concurrency: 100, + CheckInterval: time.Second, + Groups: map[string]config.QueueGroup{"ci": {Concurrency: 150, CheckInterval: 50 * time.Millisecond}}, + }, + want: []resolvers.Server{ + {Group: "ci", Concurrency: 150, CheckInterval: 50 * time.Millisecond, Queues: map[string]int{"ci": 1}}, + {Concurrency: 100, CheckInterval: time.Second, Queues: map[string]int{"default": 1}}, + }, + }, } for _, each := range cases {