Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect job.RunCount() with mutex #375

Merged
merged 3 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions job.go
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
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
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