Skip to content

Commit

Permalink
refactor: remove excessive queue empty check (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn authored Mar 23, 2024
1 parent eb4b074 commit c63c855
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 5 additions & 7 deletions quartz/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,6 @@ func (sched *StdScheduler) calculateNextTick() time.Duration {
}

func (sched *StdScheduler) executeAndReschedule(ctx context.Context) {
// return if the job queue is empty
if sched.queue.Size() == 0 {
logger.Debug("Job queue is empty.")
return
}

// fetch a job for processing
scheduled, valid := sched.fetchAndReschedule()

Expand Down Expand Up @@ -533,7 +527,11 @@ func (sched *StdScheduler) fetchAndReschedule() (ScheduledJob, bool) {
// fetch a job for processing
job, err := sched.queue.Pop()
if err != nil {
logger.Errorf("Failed to fetch a job from the queue: %s", err)
if errors.Is(err, ErrQueueEmpty) {
logger.Debug("Queue is empty")
} else {
logger.Errorf("Failed to fetch a job from the queue: %s", err)
}
return nil, false
}
// validate the job
Expand Down
6 changes: 4 additions & 2 deletions quartz/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,11 @@ func TestScheduler_Cancel(t *testing.T) {
startingRoutines := runtime.NumGoroutine()

sched := quartz.NewStdScheduler()

sched.Start(ctx)

time.Sleep(5 * time.Millisecond)
noopRoutines := runtime.NumGoroutine()
if startingRoutines > noopRoutines {
if startingRoutines >= noopRoutines {
t.Error("should have started more threads",
startingRoutines,
noopRoutines,
Expand Down Expand Up @@ -268,6 +269,7 @@ func TestScheduler_Cancel(t *testing.T) {
t.Fatal("waiting timed out before resources were released", err)
}

time.Sleep(5 * time.Millisecond)
endingRoutines := runtime.NumGoroutine()
if endingRoutines >= runningRoutines {
t.Error("number of routines should decrease after wait",
Expand Down

0 comments on commit c63c855

Please sign in to comment.