Skip to content

Commit

Permalink
Merge pull request #156 from jasonlvhit/bugfix/142_53
Browse files Browse the repository at this point in the history
fixes #142 by removing the j.nextRun.After(now) check and fixes #53 b…
  • Loading branch information
Streppel authored Apr 23, 2020
2 parents 1a413f9 + 2aef708 commit ab84337
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
7 changes: 7 additions & 0 deletions gocron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ func testJobWithInterval(t *testing.T, sched *Scheduler, job *Job, expectedTimeB
func TestSafeExecution(t *testing.T) {
sched := NewScheduler()
success := false
var wc sync.WaitGroup
wc.Add(1)

sched.Every(1).Second().Do(func(mutableValue *bool) {
*mutableValue = !*mutableValue
wc.Done()
}, &success)

sched.RunAll()
wc.Wait()

assert.Equal(t, true, success, "Task did not get called")
}

Expand Down
15 changes: 5 additions & 10 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ func (j *Job) run() ([]reflect.Value, error) {
locker.Lock(key)
defer locker.Unlock(key)
}

j.lastRun = time.Now()
if err := j.scheduleNextRun(); err != nil {
return nil, err
}
result, err := callJobFuncWithParams(j.funcs[j.jobFunc], j.fparams[j.jobFunc])
if err != nil {
return nil, err
Expand All @@ -94,7 +89,11 @@ func (j *Job) Do(jobFun interface{}, params ...interface{}) error {
j.funcs[fname] = jobFun
j.fparams[fname] = params
j.jobFunc = fname
j.scheduleNextRun()

now := time.Now().In(j.loc)
if !j.nextRun.After(now) {
j.scheduleNextRun()
}

return nil
}
Expand Down Expand Up @@ -201,10 +200,6 @@ func (j *Job) scheduleNextRun() error {
j.lastRun = now
}

if j.nextRun.After(now) {
return nil
}

periodDuration, err := j.periodDuration()
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func (s *Scheduler) RunPending() {

if n != 0 {
for i := 0; i < n; i++ {
runnableJobs[i].run()
go runnableJobs[i].run()
runnableJobs[i].lastRun = time.Now()
runnableJobs[i].scheduleNextRun()
}
}
}
Expand All @@ -100,7 +102,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()
go s.jobs[i].run()
if 0 != d {
time.Sleep(time.Duration(d))
}
Expand Down

0 comments on commit ab84337

Please sign in to comment.