Skip to content

Commit 3e02985

Browse files
authored
fix(scheduler): prevent nil dereference when attaching JobMetadata (#163)
1 parent d05d8b5 commit 3e02985

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

quartz/scheduler.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ func WithLogger(logger logger.Logger) SchedulerOpt {
264264
// The following options are available for configuring the scheduler:
265265
//
266266
// - WithBlockingExecution()
267+
// - WithJobMetadata()
267268
// - WithWorkerLimit(workerLimit int)
268269
// - WithOutdatedThreshold(outdatedThreshold time.Duration)
269270
// - WithRetryInterval(retryInterval time.Duration)
@@ -626,6 +627,11 @@ func (sched *StdScheduler) calculateNextTick() time.Duration {
626627
func (sched *StdScheduler) executeAndReschedule(ctx context.Context) {
627628
// fetch a job for processing
628629
scheduled, valid := sched.fetchAndReschedule()
630+
// return if the job is not valid
631+
if !valid {
632+
return
633+
}
634+
629635
// attach job metadata to the context
630636
if sched.opts.JobMetadata {
631637
ctx = context.WithValue(
@@ -635,29 +641,26 @@ func (sched *StdScheduler) executeAndReschedule(ctx context.Context) {
635641
)
636642
}
637643

638-
// execute the job
639-
if valid {
640-
sched.logger.Debug("Job is about to be executed",
641-
"key", scheduled.JobDetail().jobKey.String())
642-
switch {
643-
case sched.opts.BlockingExecution:
644-
sched.executeWithRetries(ctx, scheduled.JobDetail())
645-
case sched.opts.WorkerLimit > 0:
646-
select {
647-
case sched.dispatch <- dispatchedJob{
648-
ctx: ctx,
649-
jobDetail: scheduled.JobDetail(),
650-
}:
651-
case <-ctx.Done():
652-
return
653-
}
654-
default:
655-
sched.wg.Add(1)
656-
go func() {
657-
defer sched.wg.Done()
658-
sched.executeWithRetries(ctx, scheduled.JobDetail())
659-
}()
644+
sched.logger.Debug("Job is about to be executed",
645+
"key", scheduled.JobDetail().jobKey.String())
646+
switch {
647+
case sched.opts.BlockingExecution:
648+
sched.executeWithRetries(ctx, scheduled.JobDetail())
649+
case sched.opts.WorkerLimit > 0:
650+
select {
651+
case sched.dispatch <- dispatchedJob{
652+
ctx: ctx,
653+
jobDetail: scheduled.JobDetail(),
654+
}:
655+
case <-ctx.Done():
656+
return
660657
}
658+
default:
659+
sched.wg.Add(1)
660+
go func() {
661+
defer sched.wg.Done()
662+
sched.executeWithRetries(ctx, scheduled.JobDetail())
663+
}()
661664
}
662665
}
663666

0 commit comments

Comments
 (0)