Skip to content

Commit

Permalink
fix: NewScheduler wrongly creates a client whose sharedConnection val…
Browse files Browse the repository at this point in the history
…ue is always true

* This is affecting the PeriodicManager as well as the Scheduler
  • Loading branch information
kamikazechaser committed Dec 3, 2024
1 parent 106c07a commit ee17997
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,36 @@ const defaultHeartbeatInterval = 10 * time.Second
// NewScheduler returns a new Scheduler instance given the redis connection option.
// The parameter opts is optional, defaults will be used if opts is set to nil
func NewScheduler(r RedisConnOpt, opts *SchedulerOpts) *Scheduler {
scheduler := newScheduler(opts)

redisClient, ok := r.MakeRedisClient().(redis.UniversalClient)
if !ok {
panic(fmt.Sprintf("asynq: unsupported RedisConnOpt type %T", r))
}
scheduler := NewSchedulerFromRedisClient(redisClient, opts)

rdb := rdb.NewRDB(redisClient)

scheduler.rdb = rdb
scheduler.client = &Client{broker: rdb, sharedConnection: false}
scheduler.sharedConnection = false

return scheduler
}

// NewSchedulerFromRedisClient returns a new instance of Scheduler given a redis.UniversalClient
// The parameter opts is optional, defaults will be used if opts is set to nil.
// Warning: The underlying redis connection pool will not be closed by Asynq, you are responsible for closing it.
func NewSchedulerFromRedisClient(c redis.UniversalClient, opts *SchedulerOpts) *Scheduler {
scheduler := newScheduler(opts)

scheduler.rdb = rdb.NewRDB(c)
scheduler.client = NewClientFromRedisClient(c)
scheduler.sharedConnection = true

return scheduler
}

func newScheduler(opts *SchedulerOpts) *Scheduler {
if opts == nil {
opts = &SchedulerOpts{}
}
Expand All @@ -93,8 +110,6 @@ func NewSchedulerFromRedisClient(c redis.UniversalClient, opts *SchedulerOpts) *
state: &serverState{value: srvStateNew},
heartbeatInterval: heartbeatInterval,
logger: logger,
client: NewClientFromRedisClient(c),
rdb: rdb.NewRDB(c),
cron: cron.New(cron.WithLocation(loc)),
location: loc,
done: make(chan struct{}),
Expand Down

0 comments on commit ee17997

Please sign in to comment.