From fafc34f8f9295ac0b8bcd6903b33e89d6ab6fb09 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Wed, 15 Nov 2023 00:17:42 +0100 Subject: [PATCH] fix: check scheduler parameter #28 --- errors.go | 1 + persistence.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/errors.go b/errors.go index f42af5e..55c2367 100644 --- a/errors.go +++ b/errors.go @@ -45,4 +45,5 @@ var ( ErrNoManager = fmt.Errorf("no manager") ErrCannotLoadStatsFromDatabase = fmt.Errorf("errors while loading stats from database") ErrInvalidTime = fmt.Errorf("invalid time") + ErrSchedulerMisconfiguration = fmt.Errorf("scheduler misconfiguration") ) diff --git a/persistence.go b/persistence.go index 72a2c48..f464268 100644 --- a/persistence.go +++ b/persistence.go @@ -3,6 +3,7 @@ package jobqueue import ( "encoding/json" "fmt" + "github.com/robfig/cron/v3" "gopkg.in/yaml.v3" "gorm.io/gorm" "io" @@ -253,9 +254,26 @@ func CreateJobAndSchedulerFromPersistence(jobImport JobPersistence, manager *Man var scheduler Scheduler switch sType { case "interval": + + if jobImport.Scheduler.Interval == 0 { + return nil, nil, fmt.Errorf("%w: interval is 0", ErrSchedulerMisconfiguration) + } + scheduler = &IntervalScheduler{Interval: jobImport.Scheduler.Interval} case "cron": + + if jobImport.Scheduler.Spec == "" { + return nil, nil, fmt.Errorf("%w: spec is empty", ErrSchedulerMisconfiguration) + } + + // check spec + parser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor) + _, err := parser.Parse(jobImport.Scheduler.Spec) + if err != nil { + return nil, nil, fmt.Errorf("%w: %v", ErrSchedulerMisconfiguration, err) + } + scheduler = &CronScheduler{ Spec: jobImport.Scheduler.Spec, } @@ -266,14 +284,28 @@ func CreateJobAndSchedulerFromPersistence(jobImport JobPersistence, manager *Man case "delay": + if jobImport.Scheduler.Delay == 0 { + return nil, nil, fmt.Errorf("%w: delay is 0", ErrSchedulerMisconfiguration) + } + scheduler = &DelayScheduler{Delay: jobImport.Scheduler.Delay} + case "event": + + if jobImport.Scheduler.Event == "" { + return nil, nil, fmt.Errorf("%w: event is empty", ErrSchedulerMisconfiguration) + } + scheduler = &EventScheduler{Event: EventName(jobImport.Scheduler.Event)} case "instant": scheduler = &InstantScheduler{} case "time": + if jobImport.Scheduler.Time == nil { + return nil, nil, fmt.Errorf("%w: time is nil", ErrSchedulerMisconfiguration) + } + scheduler = &TimeScheduler{Time: *jobImport.Scheduler.Time} default: -- GitLab