-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
gronx.go
184 lines (150 loc) · 4.03 KB
/
gronx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package gronx
import (
"errors"
"regexp"
"strings"
"time"
)
var literals = strings.NewReplacer(
"SUN", "0", "MON", "1", "TUE", "2", "WED", "3", "THU", "4", "FRI", "5", "SAT", "6",
"JAN", "1", "FEB", "2", "MAR", "3", "APR", "4", "MAY", "5", "JUN", "6", "JUL", "7",
"AUG", "8", "SEP", "9", "OCT", "10", "NOV", "11", "DEC", "12",
)
var expressions = map[string]string{
"@yearly": "0 0 1 1 *",
"@annually": "0 0 1 1 *",
"@monthly": "0 0 1 * *",
"@weekly": "0 0 * * 0",
"@daily": "0 0 * * *",
"@hourly": "0 * * * *",
"@always": "* * * * *",
"@5minutes": "*/5 * * * *",
"@10minutes": "*/10 * * * *",
"@15minutes": "*/15 * * * *",
"@30minutes": "0,30 * * * *",
"@everysecond": "* * * * * *",
}
// AddTag adds a new custom tag representing given expr
func AddTag(tag, expr string) error {
_, ok := expressions[tag]
if ok {
return errors.New("conflict tag")
}
segs, err := Segments(expr)
if err != nil {
return err
}
expr = strings.Join(segs, " ")
expressions[tag] = expr
return nil
}
// SpaceRe is regex for whitespace.
var SpaceRe = regexp.MustCompile(`\s+`)
var yearRe = regexp.MustCompile(`\d{4}`)
func normalize(expr string) []string {
expr = strings.Trim(expr, " \t")
if e, ok := expressions[strings.ToLower(expr)]; ok {
expr = e
}
expr = SpaceRe.ReplaceAllString(expr, " ")
expr = literals.Replace(strings.ToUpper(expr))
return strings.Split(strings.ReplaceAll(expr, " ", " "), " ")
}
// Gronx is the main program.
type Gronx struct {
C Checker
}
// New initializes Gronx with factory defaults.
func New() *Gronx {
return &Gronx{&SegmentChecker{}}
}
// IsDue checks if cron expression is due for given reference time (or now).
// It returns bool or error if any.
func (g *Gronx) IsDue(expr string, ref ...time.Time) (bool, error) {
if len(ref) == 0 {
ref = append(ref, time.Now())
}
g.C.SetRef(ref[0])
segs, err := Segments(expr)
if err != nil {
return false, err
}
return g.SegmentsDue(segs)
}
func (g *Gronx) isDue(expr string, ref time.Time) bool {
due, err := g.IsDue(expr, ref)
return err == nil && due
}
// Segments splits expr into array array of cron parts.
// If expression contains 5 parts or 6th part is year like, it prepends a second.
// It returns array or error.
func Segments(expr string) ([]string, error) {
segs := normalize(expr)
slen := len(segs)
if slen < 5 || slen > 7 {
return []string{}, errors.New("expr should contain 5-7 segments separated by space")
}
// Prepend second if required
prepend := slen == 5 || (slen == 6 && yearRe.MatchString(segs[5]))
if prepend {
segs = append([]string{"0"}, segs...)
}
return segs, nil
}
// SegmentsDue checks if all cron parts are due.
// It returns bool. You should use IsDue(expr) instead.
func (g *Gronx) SegmentsDue(segs []string) (bool, error) {
skipMonthDayCheck := false
for i := 0; i < len(segs); i++ {
pos := len(segs) - 1 - i
seg := segs[pos]
isMonthDay, isWeekday := pos == 3, pos == 5
if seg == "*" || seg == "?" {
continue
}
if isMonthDay && skipMonthDayCheck {
continue
}
if isWeekday {
monthDaySeg := segs[3]
intersect := strings.Index(seg, "*/") == 0 || strings.Index(monthDaySeg, "*") == 0 || monthDaySeg == "?"
if !intersect {
due, err := g.C.CheckDue(seg, pos)
if err != nil {
return false, err
}
monthDayDue, err := g.C.CheckDue(monthDaySeg, 3)
if due || monthDayDue {
skipMonthDayCheck = true
continue
}
if err != nil {
return false, err
}
}
}
if due, err := g.C.CheckDue(seg, pos); !due {
return due, err
}
}
return true, nil
}
// IsValid checks if cron expression is valid.
// It returns bool.
func (g *Gronx) IsValid(expr string) bool { return IsValid(expr) }
// checker for validity
var checker = &SegmentChecker{ref: time.Now()}
// IsValid checks if cron expression is valid.
// It returns bool.
func IsValid(expr string) bool {
segs, err := Segments(expr)
if err != nil {
return false
}
for pos, seg := range segs {
if _, err := checker.CheckDue(seg, pos); err != nil {
return false
}
}
return true
}