Repository: go-co-op/gocron
Pull request #792 (2024-10-31 15:57:22)

weekly and monthly jobs should not allow zero interval
### What does this do?


### Which issue(s) does this PR fix/relate to?
<!--- Put `Resolves #XXX` here to auto-close the issue that your PR fixes (if such) --->


### List any changes that modify/break current functionality


### Have you included tests for your changes?


### Did you document any new/modified functionality?

- [ ] Updated `example_test.go`
- [ ] Updated `README.md`

### Notes



Patch:
diff --git a/errors.go b/errors.go
index 12eca67..d160860 100644
--- a/errors.go
+++ b/errors.go
@@ -21,6 +21,7 @@ var (
 	ErrMonthlyJobAtTimesNil          = fmt.Errorf("gocron: MonthlyJob: atTimes must not be nil")
 	ErrMonthlyJobDaysNil             = fmt.Errorf("gocron: MonthlyJob: daysOfTheMonth must not be nil")
 	ErrMonthlyJobHours               = fmt.Errorf("gocron: MonthlyJob: atTimes hours must be between 0 and 23 inclusive")
+	ErrMonthlyJobZeroInterval        = fmt.Errorf("gocron: MonthlyJob: interval must be greater than 0")
 	ErrMonthlyJobMinutesSeconds      = fmt.Errorf("gocron: MonthlyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
 	ErrNewJobTaskNil                 = fmt.Errorf("gocron: NewJob: Task must not be nil")
 	ErrNewJobTaskNotFunc             = fmt.Errorf("gocron: NewJob: Task.Function must be of kind reflect.Func")
@@ -34,6 +35,7 @@ var (
 	ErrWeeklyJobAtTimesNil           = fmt.Errorf("gocron: WeeklyJob: atTimes must not be nil")
 	ErrWeeklyJobDaysOfTheWeekNil     = fmt.Errorf("gocron: WeeklyJob: daysOfTheWeek must not be nil")
 	ErrWeeklyJobHours                = fmt.Errorf("gocron: WeeklyJob: atTimes hours must be between 0 and 23 inclusive")
+	ErrWeeklyJobZeroInterval         = fmt.Errorf("gocron: WeeklyJob: interval must be greater than 0")
 	ErrWeeklyJobMinutesSeconds       = fmt.Errorf("gocron: WeeklyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
 	ErrPanicRecovered                = fmt.Errorf("gocron: panic recovered")
 	ErrWithClockNil                  = fmt.Errorf("gocron: WithClock: clock must not be nil")
diff --git a/job.go b/job.go
index b342cd0..890889e 100644
--- a/job.go
+++ b/job.go
@@ -274,6 +274,9 @@ type weeklyJobDefinition struct {
 
 func (w weeklyJobDefinition) setup(j *internalJob, location *time.Location, _ time.Time) error {
 	var ws weeklyJob
+	if w.interval == 0 {
+		return ErrWeeklyJobZeroInterval
+	}
 	ws.interval = w.interval
 
 	if w.daysOfTheWeek == nil {
@@ -339,6 +342,9 @@ type monthlyJobDefinition struct {
 
 func (m monthlyJobDefinition) setup(j *internalJob, location *time.Location, _ time.Time) error {
 	var ms monthlyJob
+	if m.interval == 0 {
+		return ErrMonthlyJobZeroInterval
+	}
 	ms.interval = m.interval
 
 	if m.daysOfTheMonth == nil {


Test patch:
diff --git a/scheduler_test.go b/scheduler_test.go
index 609975e..d335c4a 100644
--- a/scheduler_test.go
+++ b/scheduler_test.go
@@ -686,6 +686,18 @@ func TestScheduler_NewJobErrors(t *testing.T) {
 			nil,
 			ErrWeeklyJobMinutesSeconds,
 		},
+		{
+			"weekly job interval zero",
+			WeeklyJob(
+				0,
+				NewWeekdays(time.Monday),
+				NewAtTimes(
+					NewAtTime(1, 0, 0),
+				),
+			),
+			nil,
+			ErrWeeklyJobZeroInterval,
+		},
 		{
 			"monthly job at times nil",
 			MonthlyJob(
@@ -766,6 +778,18 @@ func TestScheduler_NewJobErrors(t *testing.T) {
 			nil,
 			ErrMonthlyJobMinutesSeconds,
 		},
+		{
+			"monthly job interval zero",
+			MonthlyJob(
+				0,
+				NewDaysOfTheMonth(1),
+				NewAtTimes(
+					NewAtTime(1, 0, 0),
+				),
+			),
+			nil,
+			ErrMonthlyJobZeroInterval,
+		},
 		{
 			"WithName no name",
 			DurationJob(