Skip to content

Commit ee17997

Browse files
fix: NewScheduler wrongly creates a client whose sharedConnection value is always true
* This is affecting the PeriodicManager as well as the Scheduler
1 parent 106c07a commit ee17997

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

scheduler.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,36 @@ const defaultHeartbeatInterval = 10 * time.Second
5454
// NewScheduler returns a new Scheduler instance given the redis connection option.
5555
// The parameter opts is optional, defaults will be used if opts is set to nil
5656
func NewScheduler(r RedisConnOpt, opts *SchedulerOpts) *Scheduler {
57+
scheduler := newScheduler(opts)
58+
5759
redisClient, ok := r.MakeRedisClient().(redis.UniversalClient)
5860
if !ok {
5961
panic(fmt.Sprintf("asynq: unsupported RedisConnOpt type %T", r))
6062
}
61-
scheduler := NewSchedulerFromRedisClient(redisClient, opts)
63+
64+
rdb := rdb.NewRDB(redisClient)
65+
66+
scheduler.rdb = rdb
67+
scheduler.client = &Client{broker: rdb, sharedConnection: false}
6268
scheduler.sharedConnection = false
69+
6370
return scheduler
6471
}
6572

6673
// NewSchedulerFromRedisClient returns a new instance of Scheduler given a redis.UniversalClient
6774
// The parameter opts is optional, defaults will be used if opts is set to nil.
6875
// Warning: The underlying redis connection pool will not be closed by Asynq, you are responsible for closing it.
6976
func NewSchedulerFromRedisClient(c redis.UniversalClient, opts *SchedulerOpts) *Scheduler {
77+
scheduler := newScheduler(opts)
78+
79+
scheduler.rdb = rdb.NewRDB(c)
80+
scheduler.client = NewClientFromRedisClient(c)
81+
scheduler.sharedConnection = true
82+
83+
return scheduler
84+
}
85+
86+
func newScheduler(opts *SchedulerOpts) *Scheduler {
7087
if opts == nil {
7188
opts = &SchedulerOpts{}
7289
}
@@ -93,8 +110,6 @@ func NewSchedulerFromRedisClient(c redis.UniversalClient, opts *SchedulerOpts) *
93110
state: &serverState{value: srvStateNew},
94111
heartbeatInterval: heartbeatInterval,
95112
logger: logger,
96-
client: NewClientFromRedisClient(c),
97-
rdb: rdb.NewRDB(c),
98113
cron: cron.New(cron.WithLocation(loc)),
99114
location: loc,
100115
done: make(chan struct{}),

0 commit comments

Comments
 (0)