Skip to content

Commit

Permalink
Protect job.RunCount() with mutex (#375)
Browse files Browse the repository at this point in the history
* Protect job.RunCount() with mutex

* Minor refactor usage in job.RunCount()

* Lower sleeptime in TestScheduler_WaitForSchedules
  • Loading branch information
seunghyupoh3517 authored Aug 22, 2022
1 parent 772979f commit 0099be0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 2 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0099be0

Please sign in to comment.