diff --git a/config.example.yaml b/config.example.yaml index 8148d08..f643eed 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,4 +1,4 @@ -# yaml-language-server: $schema=https://github.com/FMotalleb/crontab-go/raw/main/schema.json +# yaml-language-server: $schema=schema.json #TODO: unix/tcp socket controller #TODO: prometheus exporter @@ -46,11 +46,11 @@ jobs: # However, you can combine multiple cron expressions and intervals within the same scheduler. # The cron scheduler allows you to specify schedules down to the second level of precision using cron expressions. - - cron: "* * * * * *" + - cron: "@yearly" # Intervals can be defined using human-readable formats. # For example, '10h' represents 10 hours, '10m' represents 10 minutes, and '10m15s' represents every 10 minutes and 15 seconds. # You can use units of hours (h), minutes (m), seconds (s), milliseconds (ms), and nanoseconds (ns) to define your intervals. - # - interval: 10m10s + - interval: 10m10s hooks: # Hooks are essentially tasks like those used in jobs, but they do not support nested hooks. # Additionally, errors or completion status of hooks are not directly managed by the system. diff --git a/config/compiler/compiler.go b/config/compiler/compiler.go index acd2e26..1d5870d 100644 --- a/config/compiler/compiler.go +++ b/config/compiler/compiler.go @@ -12,13 +12,6 @@ import ( func CompileScheduler(sh *config.JobScheduler, cr *cron.Cron, logger *logrus.Entry) abstraction.Scheduler { switch { - case sh.At != nil: - scheduler := schedule.NewAt( - *sh.At, - cr, - logger, - ) - return &scheduler case sh.Cron != "": scheduler := schedule.NewCron( sh.Cron, diff --git a/config/config.go b/config/config.go index bbe5592..e0b8243 100644 --- a/config/config.go +++ b/config/config.go @@ -33,7 +33,6 @@ type ( JobScheduler struct { Cron string `mapstructure:"cron" json:"cron,omitempty"` Interval time.Duration `mapstructure:"interval" json:"interval,omitempty"` - At *time.Time `mapstructure:"at" json:"at,omitempty"` } JobHooks struct { diff --git a/config/validators.go b/config/validators.go index 437f51c..9a23336 100644 --- a/config/validators.go +++ b/config/validators.go @@ -3,7 +3,6 @@ package config import ( "encoding/json" "fmt" - "time" "github.com/FMotalleb/crontab-go/core/schedule" ) @@ -103,17 +102,12 @@ func (t *Task) Validate() error { } func (s *JobScheduler) Validate() error { - if s.At != nil { - if s.At.Before(time.Now()) { - fmt.Println("you've set the time in the scheduler that is before now, received:", s.At, "Given time will be ignored") - } - } else if s.Interval < 0 { + if s.Interval < 0 { return fmt.Errorf("received a negative time in interval: `%v`", s.Interval) } else if _, err := schedule.CronParser.Parse(s.Cron); s.Cron != "" && err != nil { return err } schedules := []bool{ - s.At != nil, s.Interval != 0, s.Cron != "", } @@ -125,10 +119,9 @@ func (s *JobScheduler) Validate() error { } if activeSchedules != 1 { return fmt.Errorf( - "a single scheduler must have one of (at,interval,cron) field, received:(cron: `%s`, interval: `%s`, at: `%s`)", + "a single scheduler must have one of (at,interval,cron) field, received:(cron: `%s`, interval: `%s`)", s.Cron, s.Interval, - s.At, ) } return nil diff --git a/core/schedule/at.go b/core/schedule/at.go deleted file mode 100644 index 23cfaef..0000000 --- a/core/schedule/at.go +++ /dev/null @@ -1,61 +0,0 @@ -package schedule - -import ( - "fmt" - "time" - - "github.com/robfig/cron/v3" - "github.com/sirupsen/logrus" -) - -func timeToCron(t time.Time) string { - return fmt.Sprintf("%d %d %d %d %d *", t.Second(), t.Minute(), t.Hour(), t.Day(), t.Month()) -} - -type At struct { - time time.Time - logger *logrus.Entry - cron *cron.Cron - notifyChan chan any - entry *cron.EntryID -} - -func NewAt(schedule time.Time, c *cron.Cron, logger *logrus.Entry) At { - return At{ - time: schedule, - cron: c, - logger: logger. - WithFields( - logrus.Fields{ - "scheduler": "at", - "time": schedule, - }, - ), - } -} - -// buildTickChannel implements abstraction.Scheduler. -func (c *At) BuildTickChannel() <-chan any { - c.Cancel() - c.notifyChan = make(chan any, 1) - entry, err := c.cron.AddFunc(timeToCron(c.time), func() { - c.logger.Debugln("cron tick received for `at` scheduler") - c.notifyChan <- false - c.Cancel() - }) - if err != nil { - c.logger.Warnln("cannot initialize cron for `at` scheduler: ", err) - } else { - c.entry = &entry - } - return c.notifyChan -} - -// cancel implements abstraction.Scheduler. -func (c *At) Cancel() { - if c.entry != nil { - c.logger.Debugln("scheduler cancel signal received for an active instance") - c.cron.Remove(*c.entry) - close(c.notifyChan) - } -}