From 0099be01d75f8cde9cb14d6fc5beff1cfc10bd72 Mon Sep 17 00:00:00 2001 From: Seunghyup Alex Oh Date: Mon, 22 Aug 2022 07:43:08 -0700 Subject: [PATCH] Protect job.RunCount() with mutex (#375) * Protect job.RunCount() with mutex * Minor refactor usage in job.RunCount() * Lower sleeptime in TestScheduler_WaitForSchedules --- job.go | 2 ++ scheduler.go | 10 +++++----- scheduler_test.go | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/job.go b/job.go index 8d79e846..136be1e6 100644 --- a/job.go +++ b/job.go @@ -434,6 +434,8 @@ func (j *Job) setNextRun(t time.Time) { // RunCount returns the number of time the job ran so far func (j *Job) RunCount() int { + j.mu.Lock() + defer j.mu.Unlock() return j.runCount } diff --git a/scheduler.go b/scheduler.go index 1fa8407c..75fddf9b 100644 --- a/scheduler.go +++ b/scheduler.go @@ -323,14 +323,14 @@ func (s *Scheduler) calculateWeeks(job *Job, lastRun time.Time) nextRun { } func (s *Scheduler) calculateTotalDaysDifference(lastRun time.Time, daysToWeekday int, job *Job) int { - if job.getInterval() > 1 && job.RunCount() < len(job.Weekdays()) { // just count weeks after the first jobs were done - return daysToWeekday - } - if job.getInterval() > 1 && job.RunCount() >= len(job.Weekdays()) { + if job.getInterval() > 1 { + // just count weeks after the first jobs were done + if job.RunCount() < len(job.Weekdays()) { + return daysToWeekday + } if daysToWeekday > 0 { return int(job.getInterval())*7 - (allWeekDays - daysToWeekday) } - return int(job.getInterval()) * 7 } diff --git a/scheduler_test.go b/scheduler_test.go index 833ae0bd..c8eb89d1 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -1841,7 +1841,7 @@ func TestScheduler_WaitForSchedules(t *testing.T) { require.NoError(t, err) s.StartAsync() - time.Sleep(1100 * time.Millisecond) + time.Sleep(1050 * time.Millisecond) s.Stop() counterMutex.RLock()