Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/queue.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions queueing/resolvers/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions queueing/resolvers/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"maps"
"slices"
"time"

"github.com/gonstruct/core/config"
"github.com/gonstruct/core/queueing/job"
Expand All @@ -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
}
Expand All @@ -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{}
Expand All @@ -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
}
15 changes: 15 additions & 0 deletions tests/queueing/resolvers/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"reflect"
"testing"
"time"

"github.com/gonstruct/core/config"
"github.com/gonstruct/core/queueing/job"
Expand Down Expand Up @@ -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 {
Expand Down