diff --git a/errors.go b/errors.go index 5378125..04c3ffa 100644 --- a/errors.go +++ b/errors.go @@ -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") diff --git a/job.go b/job.go index 6f09f3d..b3fbb8e 100644 --- a/job.go +++ b/job.go @@ -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 @@ -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 diff --git a/scheduler_test.go b/scheduler_test.go index 212eb70..70aed72 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -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),