Skip to content

Commit

Permalink
fix(scheduler): synchronize the started flag read (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn authored Feb 1, 2025
1 parent 4bb9433 commit 9dff931
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 8 additions & 8 deletions quartz/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Scheduler interface {

// StdScheduler implements the [Scheduler] interface.
type StdScheduler struct {
mtx sync.Mutex
mtx sync.RWMutex
wg sync.WaitGroup

interrupt chan struct{}
Expand Down Expand Up @@ -300,7 +300,7 @@ func (sched *StdScheduler) ScheduleJob(

if err = sched.queue.Push(toSchedule); err == nil {
sched.logger.Debug("Successfully added job", "key", jobDetail.jobKey.String())
if sched.started {
if sched.IsStarted() {
sched.Reset()
}
}
Expand Down Expand Up @@ -342,8 +342,8 @@ func (sched *StdScheduler) Wait(ctx context.Context) {

// IsStarted determines whether the scheduler has been started.
func (sched *StdScheduler) IsStarted() bool {
sched.mtx.Lock()
defer sched.mtx.Unlock()
sched.mtx.RLock()
defer sched.mtx.RUnlock()

return sched.started
}
Expand Down Expand Up @@ -391,7 +391,7 @@ func (sched *StdScheduler) DeleteJob(jobKey *JobKey) error {
_, err := sched.queue.Remove(jobKey)
if err == nil {
sched.logger.Debug("Successfully deleted job", "key", jobKey.String())
if sched.started {
if sched.IsStarted() {
sched.Reset()
}
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func (sched *StdScheduler) PauseJob(jobKey *JobKey) error {
}
if err = sched.queue.Push(paused); err == nil {
sched.logger.Debug("Successfully paused job", "key", jobKey.String())
if sched.started {
if sched.IsStarted() {
sched.Reset()
}
}
Expand Down Expand Up @@ -466,7 +466,7 @@ func (sched *StdScheduler) ResumeJob(jobKey *JobKey) error {
}
if err = sched.queue.Push(resumed); err == nil {
sched.logger.Debug("Successfully resumed job", "key", jobKey.String())
if sched.started {
if sched.IsStarted() {
sched.Reset()
}
}
Expand All @@ -483,7 +483,7 @@ func (sched *StdScheduler) Clear() error {
err := sched.queue.Clear()
if err == nil {
sched.logger.Debug("Successfully cleared job queue")
if sched.started {
if sched.IsStarted() {
sched.Reset()
}
}
Expand Down
4 changes: 4 additions & 0 deletions quartz/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ func TestScheduler(t *testing.T) {
}

func TestScheduler_BlockingSemantics(t *testing.T) {
t.Parallel()

for _, tt := range []string{"Blocking", "NonBlocking", "WorkerSmall", "WorkerLarge"} {
tt := tt
t.Run(tt, func(t *testing.T) {
t.Parallel()
var (
workerLimit int
opt quartz.SchedulerOpt
Expand Down

0 comments on commit 9dff931

Please sign in to comment.