Repository: go-co-op/gocron
Pull request #61 (2020-09-20 23:52:35)

fix: scheduler schedules jobs registration when already running
### What does this do?
scheduler schedules jobs registration when already running

### Which issue(s) does this PR fix/relate to?
#60 #58 



Patch:
diff --git a/scheduler.go b/scheduler.go
index c25d2db..e4fd769 100644
--- a/scheduler.go
+++ b/scheduler.go
@@ -392,6 +392,11 @@ func (s *Scheduler) Do(jobFun interface{}, params ...interface{}) (*Job, error)
 	j.fparams[fname] = params
 	j.jobFunc = fname
 
+	// we should not schedule if not running since we cant foresee how long it will take for the scheduler to start
+	if s.running {
+		s.scheduleNextRun(j)
+	}
+
 	return j, nil
 }
 


Test patch:
diff --git a/scheduler_test.go b/scheduler_test.go
index 4e779c9..c67fd70 100644
--- a/scheduler_test.go
+++ b/scheduler_test.go
@@ -108,47 +108,6 @@ func TestAtFuture(t *testing.T) {
 	assert.Equal(t, false, shouldBeFalse, "Day job was not expected to run as it was in the future")
 }
 
-func TestDay(t *testing.T) {
-	now := time.Now().UTC()
-
-	// Create new scheduler to have clean test env
-	s := NewScheduler(time.UTC)
-
-	// schedule next run 1 day
-	dayJob, _ := s.Every(1).Day().Do(task)
-	s.scheduleNextRun(dayJob)
-	tomorrow := now.AddDate(0, 0, 1)
-	expectedTime := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 0, 0, 0, 0, time.UTC)
-	assert.Equal(t, expectedTime, dayJob.nextRun)
-
-	// schedule next run 2 days
-	dayJob, _ = s.Every(2).Days().Do(task)
-	s.scheduleNextRun(dayJob)
-	twoDaysFromNow := now.AddDate(0, 0, 2)
-	expectedTime = time.Date(twoDaysFromNow.Year(), twoDaysFromNow.Month(), twoDaysFromNow.Day(), 0, 0, 0, 0, time.UTC)
-	assert.Equal(t, expectedTime, dayJob.nextRun)
-
-	// Job running longer than next schedule 1day 2 hours
-	dayJob, _ = s.Every(1).Day().Do(task)
-	twoHoursFromNow := now.Add(2 * time.Hour)
-	dayJob.lastRun = time.Date(twoHoursFromNow.Year(), twoHoursFromNow.Month(), twoHoursFromNow.Day(), twoHoursFromNow.Hour(), 0, 0, 0, time.UTC)
-	s.scheduleNextRun(dayJob)
-	expectedTime = time.Date(now.Year(), now.Month(), now.AddDate(0, 0, 1).Day(), 0, 0, 0, 0, time.UTC)
-	assert.Equal(t, expectedTime, dayJob.nextRun)
-
-	// At() 2 hours before now
-	twoHoursBefore := now.Add(time.Duration(-2 * time.Hour))
-	startAt := fmt.Sprintf("%02d:%02d", twoHoursBefore.Hour(), twoHoursBefore.Minute())
-	dayJob, _ = s.Every(1).Day().At(startAt).Do(task)
-	s.scheduleNextRun(dayJob)
-
-	expectedTime = time.Date(twoHoursBefore.Year(), twoHoursBefore.Month(),
-		twoHoursBefore.AddDate(0, 0, 1).Day(),
-		twoHoursBefore.Hour(), twoHoursBefore.Minute(), 0, 0, time.UTC)
-
-	assert.Equal(t, expectedTime, dayJob.nextRun)
-}
-
 func schedulerForNextWeekdayEveryNTimes(weekday time.Weekday, n uint64, s *Scheduler) *Scheduler {
 	switch weekday {
 	case time.Monday:
@@ -631,3 +590,21 @@ func (f fakeTime) Sleep(duration time.Duration) {
 func (f fakeTime) NewTicker(duration time.Duration) *time.Ticker {
 	panic("implement me")
 }
+
+func TestScheduler_Do(t *testing.T) {
+	t.Run("adding a new job before scheduler starts does not schedule job", func(t *testing.T) {
+		s := NewScheduler(time.UTC)
+		s.running = false
+		job, err := s.Every(1).Second().Do(func() {})
+		assert.Equal(t, nil, err)
+		assert.True(t, job.nextRun.IsZero())
+	})
+
+	t.Run("adding a new job when scheduler is running schedules job", func(t *testing.T) {
+		s := NewScheduler(time.UTC)
+		s.running = true
+		job, err := s.Every(1).Second().Do(func() {})
+		assert.Equal(t, nil, err)
+		assert.False(t, job.nextRun.IsZero())
+	})
+}