Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weekly and monthly jobs should not allow zero interval #792

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down