Skip to content

Fix PR #133 so that it does not break RunAll #138

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ func (j *Job) shouldRun() bool {
}

//Run the job and immediately reschedule it
func (j *Job) run() (result []reflect.Value, err error) {
//If this is a scheduled run make sure we are not early
func (j *Job) run(scheduledRun bool) (result []reflect.Value, err error) {
if j.lock {
if locker == nil {
err = fmt.Errorf("trying to lock %s with nil locker", j.jobFunc)
Expand All @@ -123,6 +124,12 @@ func (j *Job) run() (result []reflect.Value, err error) {
}()
}

if scheduledRun {
for time.Now().Before(j.nextRun) {
time.Sleep(500 * time.Nanosecond)
}
}

j.lastRun = time.Now()
result, err = callJobFuncWithParams(j.funcs[j.jobFunc], j.fparams[j.jobFunc])
j.scheduleNextRun()
Expand Down Expand Up @@ -533,7 +540,7 @@ func (s *Scheduler) RunPending() {

if n != 0 {
for i := 0; i < n; i++ {
runnableJobs[i].run()
runnableJobs[i].run(true)
}
}
}
Expand All @@ -546,7 +553,7 @@ func (s *Scheduler) RunAll() {
// RunAllwithDelay runs all jobs with delay seconds
func (s *Scheduler) RunAllwithDelay(d int) {
for i := 0; i < s.size; i++ {
s.jobs[i].run()
s.jobs[i].run(false)
if 0 != d {
time.Sleep(time.Duration(d))
}
Expand Down