[#3132] added common cron expression macros

This commit is contained in:
Gani Georgiev
2023-08-15 12:17:50 +03:00
parent 734f35c504
commit 5f21c4119f
3 changed files with 69 additions and 1 deletions
+16 -1
View File
@@ -62,6 +62,16 @@ func (s *Schedule) IsDue(m *Moment) bool {
return true
}
var macros = map[string]string{
"@yearly": "0 0 1 1 *",
"@annually": "0 0 1 1 *",
"@monthly": "0 0 1 * *",
"@weekly": "0 0 * * 0",
"@daily": "0 0 * * *",
"@midnight": "0 0 * * *",
"@hourly": "0 * * * *",
}
// NewSchedule creates a new Schedule from a cron expression.
//
// A cron expression is consisted of 5 segments separated by space,
@@ -72,10 +82,15 @@ func (s *Schedule) IsDue(m *Moment) bool {
// - range: 1-30
// - step: */n or 1-30/n
// - list: 1,2,3,10-20/n
// - macros: @yearly (or @annually), @monthly, @weekly, @daily (or @midnight), @hourly
func NewSchedule(cronExpr string) (*Schedule, error) {
if v, ok := macros[cronExpr]; ok {
cronExpr = v
}
segments := strings.Split(cronExpr, " ")
if len(segments) != 5 {
return nil, errors.New("invalid cron expression - must have exactly 5 space separated segments")
return nil, errors.New("invalid cron expression - must be a valid macro or to have exactly 5 space separated segments")
}
minutes, err := parseCronSegment(segments[0], 0, 59)