Skip to content

Commit

Permalink
handle crontab and return error with invalid day in a month (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler authored Aug 9, 2024
1 parent 68dba11 commit 3ccd68d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "fmt"

// Public error definitions
var (
ErrCronJobInvalid = fmt.Errorf("gocron: CronJob: invalid crontab")
ErrCronJobParse = fmt.Errorf("gocron: CronJob: crontab parse failure")
ErrDailyJobAtTimeNil = fmt.Errorf("gocron: DailyJob: atTime within atTimes must not be nil")
ErrDailyJobAtTimesNil = fmt.Errorf("gocron: DailyJob: atTimes must not be nil")
Expand Down
5 changes: 4 additions & 1 deletion job.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ type cronJobDefinition struct {
withSeconds bool
}

func (c cronJobDefinition) setup(j *internalJob, location *time.Location, _ time.Time) error {
func (c cronJobDefinition) setup(j *internalJob, location *time.Location, now time.Time) error {
var withLocation string
if strings.HasPrefix(c.crontab, "TZ=") || strings.HasPrefix(c.crontab, "CRON_TZ=") {
withLocation = c.crontab
Expand All @@ -140,6 +140,9 @@ func (c cronJobDefinition) setup(j *internalJob, location *time.Location, _ time
if err != nil {
return errors.Join(ErrCronJobParse, err)
}
if cronSchedule.Next(now).IsZero() {
return ErrCronJobInvalid
}

j.jobSchedule = &cronJob{cronSchedule: cronSchedule}
return nil
Expand Down
9 changes: 9 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,15 @@ func TestScheduler_NewJobErrors(t *testing.T) {
nil,
ErrCronJobParse,
},
{
"cron invalid date",
CronJob(
"* * * 31 FEB *",
true,
),
nil,
ErrCronJobInvalid,
},
{
"duration job time interval is zero",
DurationJob(0 * time.Second),
Expand Down

0 comments on commit 3ccd68d

Please sign in to comment.