Skip to content

Commit 46bcd8e

Browse files
authored
weekly and monthly jobs should not allow zero interval (#792)
1 parent 4baf341 commit 46bcd8e

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

errors.go

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
ErrMonthlyJobAtTimesNil = fmt.Errorf("gocron: MonthlyJob: atTimes must not be nil")
2222
ErrMonthlyJobDaysNil = fmt.Errorf("gocron: MonthlyJob: daysOfTheMonth must not be nil")
2323
ErrMonthlyJobHours = fmt.Errorf("gocron: MonthlyJob: atTimes hours must be between 0 and 23 inclusive")
24+
ErrMonthlyJobZeroInterval = fmt.Errorf("gocron: MonthlyJob: interval must be greater than 0")
2425
ErrMonthlyJobMinutesSeconds = fmt.Errorf("gocron: MonthlyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
2526
ErrNewJobTaskNil = fmt.Errorf("gocron: NewJob: Task must not be nil")
2627
ErrNewJobTaskNotFunc = fmt.Errorf("gocron: NewJob: Task.Function must be of kind reflect.Func")
@@ -34,6 +35,7 @@ var (
3435
ErrWeeklyJobAtTimesNil = fmt.Errorf("gocron: WeeklyJob: atTimes must not be nil")
3536
ErrWeeklyJobDaysOfTheWeekNil = fmt.Errorf("gocron: WeeklyJob: daysOfTheWeek must not be nil")
3637
ErrWeeklyJobHours = fmt.Errorf("gocron: WeeklyJob: atTimes hours must be between 0 and 23 inclusive")
38+
ErrWeeklyJobZeroInterval = fmt.Errorf("gocron: WeeklyJob: interval must be greater than 0")
3739
ErrWeeklyJobMinutesSeconds = fmt.Errorf("gocron: WeeklyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
3840
ErrPanicRecovered = fmt.Errorf("gocron: panic recovered")
3941
ErrWithClockNil = fmt.Errorf("gocron: WithClock: clock must not be nil")

job.go

+6
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ type weeklyJobDefinition struct {
274274

275275
func (w weeklyJobDefinition) setup(j *internalJob, location *time.Location, _ time.Time) error {
276276
var ws weeklyJob
277+
if w.interval == 0 {
278+
return ErrWeeklyJobZeroInterval
279+
}
277280
ws.interval = w.interval
278281

279282
if w.daysOfTheWeek == nil {
@@ -339,6 +342,9 @@ type monthlyJobDefinition struct {
339342

340343
func (m monthlyJobDefinition) setup(j *internalJob, location *time.Location, _ time.Time) error {
341344
var ms monthlyJob
345+
if m.interval == 0 {
346+
return ErrMonthlyJobZeroInterval
347+
}
342348
ms.interval = m.interval
343349

344350
if m.daysOfTheMonth == nil {

scheduler_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,18 @@ func TestScheduler_NewJobErrors(t *testing.T) {
686686
nil,
687687
ErrWeeklyJobMinutesSeconds,
688688
},
689+
{
690+
"weekly job interval zero",
691+
WeeklyJob(
692+
0,
693+
NewWeekdays(time.Monday),
694+
NewAtTimes(
695+
NewAtTime(1, 0, 0),
696+
),
697+
),
698+
nil,
699+
ErrWeeklyJobZeroInterval,
700+
},
689701
{
690702
"monthly job at times nil",
691703
MonthlyJob(
@@ -766,6 +778,18 @@ func TestScheduler_NewJobErrors(t *testing.T) {
766778
nil,
767779
ErrMonthlyJobMinutesSeconds,
768780
},
781+
{
782+
"monthly job interval zero",
783+
MonthlyJob(
784+
0,
785+
NewDaysOfTheMonth(1),
786+
NewAtTimes(
787+
NewAtTime(1, 0, 0),
788+
),
789+
),
790+
nil,
791+
ErrMonthlyJobZeroInterval,
792+
},
769793
{
770794
"WithName no name",
771795
DurationJob(

0 commit comments

Comments
 (0)