handle crontab and return error with invalid day in a month (#766)

This commit is contained in:
John Roesler 2024-08-09 10:11:54 -05:00 committed by GitHub
parent 68dba115b2
commit 3ccd68d676
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 1 deletions

View File

@ -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")

5
job.go
View File

@ -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

View File

@ -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),