Skip to content

Commit

Permalink
fix: panic: runtime error: integer divide by zero
Browse files Browse the repository at this point in the history
"Do" function triggers "doCommon" function that sets "inScheduleChain" to false. In "getCurrentJob", if "inScheduleChain" is false, then a new job will be added to a slice and the current job will be the last element of the slice. Therefore, after "Do" function runs, "StartAt" will try to obtain the current job. Since "inScheduleChain" is false, a new job with interval 0 will be added and returned as the current job in "StartAt" function. After this interval 0 will produce "duration" to be 0 and "panic: runtime error: integer divide by zero".
  • Loading branch information
BranislavLazic committed May 31, 2023
1 parent 604aa9b commit f07c724
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,13 @@ func ExampleScheduler_StartAt() {
s.StartBlocking()
}

func ExampleScheduler_StartAt_AfterTaskStart() {

Check failure on line 759 in example_test.go

View workflow job for this annotation

GitHub Actions / lint and test (1.20)

tests: ExampleScheduler_StartAt_AfterTaskStart has malformed example suffix: AfterTaskStart (govet)

Check failure on line 759 in example_test.go

View workflow job for this annotation

GitHub Actions / lint and test (1.16)

tests: ExampleScheduler_StartAt_AfterTaskStart has malformed example suffix: AfterTaskStart (govet)
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(5).Seconds().Do(task)
s.StartAt(time.Now().Add(10 * time.Second))
s.StartBlocking()
}

func ExampleScheduler_StartBlocking() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(3).Seconds().Do(task)
Expand Down
1 change: 1 addition & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ func (s *Scheduler) GetAllTags() []string {
// StartAt schedules the next run of the Job. If this time is in the past, the configured interval will be used
// to calculate the next future time
func (s *Scheduler) StartAt(t time.Time) *Scheduler {
s.inScheduleChain = true
job := s.getCurrentJob()
job.setStartAtTime(t)
job.startsImmediately = false
Expand Down
16 changes: 16 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,22 @@ func TestScheduler_StartAt(t *testing.T) {

assert.Equal(t, dt.Add(time.Hour*168).Truncate(time.Second), job.NextRun())
})

t.Run("StartAt() called after starting a task", func(t *testing.T) {
s := NewScheduler(time.UTC)

dt := time.Now().UTC().Add(time.Second)
job, err := s.Every(1).Week().Do(func() {})
s.StartAt(dt)
require.NoError(t, err)

s.StartAsync()
assert.Equal(t, dt, job.NextRun())
time.Sleep(time.Millisecond * 1500)
s.Stop()

assert.Equal(t, dt.Add(time.Hour*168).Truncate(time.Second), job.NextRun())
})
}

func TestScheduler_CalculateNextRun(t *testing.T) {
Expand Down

0 comments on commit f07c724

Please sign in to comment.