-
Notifications
You must be signed in to change notification settings - Fork 0
/
crontab.go
429 lines (404 loc) · 9.74 KB
/
crontab.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package crontab
import (
"context"
"errors"
"fmt"
"strings"
"time"
)
type cType int
const (
second = cType(iota)
minute
hour
day
month
week
errorStr = "规则错误"
errorNull = "不存在可执行时间"
errorNullYear = "%v年以前不存在可执行时间"
errorStrRange = "%v的规则错误,范围在%v-%v之间"
errorStrCompare = "%v的规则错误,%v-%v开始比结束小"
)
var (
maxYearLen = 1 // 最长年限多久,今年年份+参数为最大年,0表示限制为今年,-1为不限制(如果周月日不一致可能导致查询几十年以后的)
)
func (c cType) text() string {
m := map[cType]string{
second: "秒",
minute: "分",
hour: "时",
day: "日",
month: "月",
week: "周",
}
return m[c]
}
type Context struct {
context.Context
c *Cron
}
// Stop 结束定时器
func (c *Context) Stop() {
c.c.die <- struct{}{}
}
// NewCron 创建定时器
func NewCron(rules string) *Cron {
return &Cron{rules: rules}
}
// SetMaxYearLen 设置最长年限多久,今年年份+参数为最大年,0表示限制为今年,-1为不限制(如果周月日不一致可能导致查询几十年以后的)
func SetMaxYearLen(l int) {
maxYearLen = l
}
type Cron struct {
rules string // 任务规则
task func(res *Context) // 任务
beforeTimePtr *time.Time // 上次执行时间
die chan struct{} // 关闭定时器
t *time.Timer // 定时器
year int // 执行年
times [6][]int // 可执行时间
timesLen [6]int // 可执行的数组长度
timesIdx [6]int // 执行时间索引
}
// SetFun 设置任务方法
func (c *Cron) SetFun(task func(ctx *Context)) *Cron {
c.task = task
return c
}
// SetBeforeTime 设置上一次执行时间,没有则不设置
func (c *Cron) SetBeforeTime(beforeTime time.Time) *Cron {
c.beforeTimePtr = &beforeTime
return c
}
func (c *Cron) Run() {
// 解析规则
c.parseRules()
// 验证月份和日期是否可组成正确的时间(月份闰月,大月小月问题)
c.validMonthDay()
// 初始化第一次可执行时间
c.init()
// 判断当前时间是否时已经执行
if c.beforeTimePtr != nil && c.beforeTimePtr.Year() == c.year &&
int(c.beforeTimePtr.Month()) == c.times[month][c.timesIdx[month]] &&
c.beforeTimePtr.Day() == c.times[day][c.timesIdx[day]] &&
c.beforeTimePtr.Hour() == c.times[hour][c.timesIdx[hour]] &&
c.beforeTimePtr.Minute() == c.times[minute][c.timesIdx[minute]] &&
c.beforeTimePtr.Second() == c.times[second][c.timesIdx[second]] {
// 已经执行则跳到下一个时间
c.nextTime()
}
c.t = time.AfterFunc(time.Duration(c.getNextTime().UnixNano()-time.Now().UnixNano()), func() {
c.run()
})
defer c.t.Stop()
// 阻塞
select {
case <-c.die:
c.t.Stop()
}
}
func (c *Cron) run() {
go func() {
defer func() {
if err := recover(); err != nil {
c.die <- struct{}{}
}
}()
c.task(&Context{
c: c,
})
}()
// 下一次执行时间
c.nextTime()
c.t = time.AfterFunc(time.Duration(c.getNextTime().UnixNano()-time.Now().UnixNano()), func() {
c.run()
})
}
func (c *Cron) validMonthDay() {
isExists := false
for _, v := range c.times[month] {
switch v {
case 1, 3, 5, 7, 8, 10, 12:
isExists = true
// 不验证,天可为31天
case 2:
// 2月最大可为29
if c.times[day][0] <= 29 {
isExists = true
}
default:
// 其他月份都为30天
if c.times[day][0] <= 30 {
isExists = true
}
}
}
if !isExists {
panic(errorNull)
}
}
func (c *Cron) init() {
c.die = make(chan struct{})
nowTime := time.Now()
c.year = nowTime.Year()
nowTimes := []int{nowTime.Second(), nowTime.Minute(), nowTime.Hour(), nowTime.Day(), int(nowTime.Month()),
nowTime.Year()}
// 秒到月可进制
isNext := false
for i := second; i <= month; i++ {
c.timesIdx[i] = inSliceByRun(nowTimes[i], c.times[i])
if isNext {
c.timesIdx[i] = (c.timesIdx[i] + 1) % c.timesLen[i]
isNext = false
}
// 小于证明数字经过最大,高位进1
if c.times[i][c.timesIdx[i]] < nowTimes[i] {
isNext = true
}
// 不等于证明变化,变化后低位至0
if c.times[i][c.timesIdx[i]] != nowTimes[i] {
for j := second; j < i; j++ {
c.timesIdx[j] = 0
}
}
}
if isNext {
c.year++
for j := second; j < month; j++ {
c.timesIdx[j] = 0
}
}
if !c.isTrueNextTime() {
c.nextTime()
}
}
func (c *Cron) nextTime() {
isNext := true
for i := second; i <= month; i++ {
if isNext {
c.timesIdx[i] = (c.timesIdx[i] + 1) % c.timesLen[i]
}
if c.timesIdx[i] != 0 {
isNext = false
break
}
}
if isNext {
c.year++
}
if c.isTrueNextTime() {
return
}
// 不满足为日月周不匹配,循环日,将时分秒置零
c.timesIdx[second] = 0
c.timesIdx[minute] = 0
c.timesIdx[hour] = 0
for {
isNext = true
for i := day; i <= month; i++ {
if isNext {
c.timesIdx[i] = (c.timesIdx[i] + 1) % c.timesLen[i]
}
if c.timesIdx[i] != 0 {
isNext = false
break
}
}
if isNext {
c.year++
}
if c.isTrueNextTime() {
return
}
}
}
func (c *Cron) getNextTime() time.Time {
nextTime := time.Date(c.year, time.Month(c.times[month][c.timesIdx[month]]), c.times[day][c.timesIdx[day]],
c.times[hour][c.timesIdx[hour]], c.times[minute][c.timesIdx[minute]], c.times[second][c.timesIdx[second]], 0, time.Local)
return nextTime
}
func (c *Cron) isTrueNextTime() bool {
nextTime := c.getNextTime()
// 两年内时间为正确时间
if maxYearLen != -1 && time.Now().Year()+1+maxYearLen <= nextTime.Year() {
panic(fmt.Errorf(errorNullYear, time.Now().Year()+1+maxYearLen))
}
if nextTime.Year() != c.year || nextTime.Day() != c.times[day][c.timesIdx[day]] {
return false
}
// 判断周是否在数组中
if !isInArray(int(nextTime.Weekday()), c.times[week]) {
return false
}
return true
}
func (c *Cron) parseRules() {
var err error
ruleList := strings.Split(c.rules, " ")
if len(ruleList) != 6 {
panic("cron格式为* * * * * *六位,分别代表秒 分 时 日 月 周")
}
// 秒
seconds := make([]bool, 60)
if c.times[second], err = c.parseSingle(ruleList[second], second, seconds); err != nil {
panic(err)
}
// 分
minutes := make([]bool, 60)
if c.times[minute], err = c.parseSingle(ruleList[minute], minute, minutes); err != nil {
panic(err)
}
// 时
hours := make([]bool, 24)
if c.times[hour], err = c.parseSingle(ruleList[hour], hour, hours); err != nil {
panic(err)
}
// 日
days := make([]bool, 32)
if c.times[day], err = c.parseSingle(ruleList[day], day, days); err != nil {
panic(err)
}
// 月
months := make([]bool, 13)
if c.times[month], err = c.parseSingle(ruleList[month], month, months); err != nil {
panic(err)
}
// 周
weeks := make([]bool, 7)
if c.times[week], err = c.parseSingle(ruleList[week], week, weeks); err != nil {
panic(err)
}
for k, v := range c.times {
c.timesLen[k] = len(v)
}
}
func (c *Cron) parseSingle(rule string, ct cType, runList []bool) (rs []int, err error) {
for _, r := range strings.Split(rule, ",") {
if strings.Contains(r, "/") {
// 解析每多少时间times
rList := strings.Split(r, "/")
if len(rList) != 2 {
err = errors.New(errorStr)
return
}
var start, end, interval int
if interval, err = parseInt(rList[1]); err != nil {
return
}
if err = c.validNum(interval, ct, true); err != nil {
return
}
start, end = c.getMaxBetween(ct)
if rList[0] != "*" {
charList := strings.Split(rList[0], "-")
if len(charList) > 2 {
err = errors.New(errorStr)
return
}
if start, err = parseInt(charList[0]); err != nil {
return
}
if err = c.validNum(start, ct, false); err != nil {
return
}
if len(charList) == 2 {
if end, err = parseInt(charList[1]); err != nil {
return
}
if err = c.validNum(end, ct, false); err != nil {
return
}
}
}
if start > end {
err = fmt.Errorf(errorStrCompare, ct.text(), start, end)
return
}
c.getTimes(start, end, interval, runList)
} else if strings.Contains(r, "-") {
// 解释时间段between
rList := strings.Split(r, "-")
if len(rList) != 2 {
err = errors.New(errorStr)
return
}
var start, end, interval int
interval = 1
if start, err = parseInt(rList[0]); err != nil {
return
}
if err = c.validNum(start, ct, false); err != nil {
return
}
if end, err = parseInt(rList[1]); err != nil {
return
}
if err = c.validNum(end, ct, false); err != nil {
return
}
if start > end {
err = fmt.Errorf(errorStrCompare, ct.text(), start, end)
return
}
c.getTimes(start, end, interval, runList)
} else {
var start, end, interval int
start, end = c.getMaxBetween(ct)
if r == "*" {
interval = 1
c.getTimes(start, end, interval, runList)
continue
}
var charNum int
if charNum, err = parseInt(r); err != nil {
return
}
if err = c.validNum(charNum, ct, false); err != nil {
return
}
runList[charNum] = true
}
}
for k, v := range runList {
if v {
rs = append(rs, k)
}
}
return
}
func (c *Cron) getTimes(start, end, interval int, runList []bool) {
for i := start; i <= end; i += interval {
runList[i] = true
}
}
func (c *Cron) validNum(n int, ct cType, isInterval bool) error {
min, max := c.getMaxBetween(ct)
if isInterval && (n < 1 || n > max+1) {
return fmt.Errorf(errorStrRange, ct.text()+"间隔", 1, max+1)
}
if !isInterval && (n < min || n > max) {
return fmt.Errorf(errorStrRange, ct.text(), min, max)
}
return nil
}
func (c *Cron) getMaxBetween(ct cType) (start, end int) {
switch ct {
case second, minute:
start = 0
end = 59
case hour:
start = 0
end = 23
case day:
start = 1
end = 31
case week:
start = 0
end = 6
case month:
start = 1
end = 12
}
return
}