-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.lua
323 lines (284 loc) · 8.52 KB
/
cron.lua
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
CronFields = {
SECOND = 1,
MINUTE = 2,
HOUR = 3,
DAY_OF_MONTH = 4,
MONTH = 5,
DAY_OF_WEEK = 6,
YEAR = 7
}
CronConstants = {
MAX_SECONDS = 60,
MAX_MINUTES = 60,
MAX_HOURS = 24,
MAX_DAYS_OF_WEEK = 8, -- This is because sunday can be either 0 or 7
MAX_DAYS_OF_MONTH = 31,
MAX_MONTHS = 13, -- This is because months are 1-indexed
MAX_DAYS_IN_YEAR = 365,
DAYS = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" },
MONTHS = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }
}
CronExp = {}
function CronExp.pushToFields(fields, field)
if (not fields) or field == -1 then return end
if table.HasValue(fields, field) then return end
for i=0,7 do
if fields[i] == -1 then
fields[i] = field
return
end
end
end
function CronExp.addToField(field, date, value)
if field == -1 then return -1 end
if field == CronFields.SECOND then
date.sec = date.sec + value
elseif field == CronFields.MINUTE then
date.min = date.min + value
elseif field == CronFields.HOUR then
date.hour = date.hour + value
elseif field == CronFields.DAY_OF_MONTH then
date.day = date.day + value
elseif field == CronFields.MONTH then
date.month = date.month + value
elseif field == CronFields.DAY_OF_WEEK then
date.wday = date.wday + value
elseif field == CronFields.YEARS then
date.year = date.year + value
end
end
function CronExp.resetMin(field, date)
if field == -1 then return -1 end
if field == CronFields.SECOND then
date.sec = 0
elseif field == CronFields.MINUTE then
date.min = 0
elseif field == CronFields.HOUR then
date.hour = 0
elseif field == CronFields.DAY_OF_MONTH then
date.day = 1
elseif field == CronFields.MONTH then
date.month = 0
elseif field == CronFields.DAY_OF_WEEK then
date.wday = 1
elseif field == CronFields.DAY_OF_WEEK then
date.year = 0
end
end
function CronExp.resetAllMin(date, lowerOrders)
for k,v in ipairs(lowerOrders) do
if not v == -1 then
CronExp.resetMin(v, date)
end
end
end
function CronExp.setField(field, date, value)
if field == CronFields.SECOND then
date.sec = value
elseif field == CronFields.MINUTE then
date.min = value
elseif field == CronFields.HOUR then
date.hour = value
elseif field == CronFields.DAY_OF_MONTH then
date.day = value
elseif field == CronFields.MONTH then
date.month = value
elseif field == CronFields.DAY_OF_WEEK then
date.wday = value
elseif field == CronFields.DAY_OF_WEEK then
date.year = value
end
end
function CronExp.getRange(value, min, max)
local range = {}
if #value == 1 and value == "*" then
range[1] = min
range[2] = max - 1
elseif select(1, string.find(value, '-')) == nil then
range[1] = tonumber(value)
range[2] = tonumber(value)
else
local parts = string.Split(value, "-")
if not parts == 2 then
error("Ranges require two values: " .. value)
end
range[1] = tonumber(parts[1])
range[2] = tonumber(parts[2])
end
if range[1] >= max or range[2] >= max or range[1] < min or range[2] < min then
error("Range is outside limits: " .. value .. " (min=" .. min .. ",max=" .. max - 1 .. ")")
end
if range[1] > range[2] then
error("Start of range is larger than end of range: " .. value)
end
return range
end
function CronExp.getNumberBits(value, min, max)
local values = string.Split(value, ",")
local res = {}
for i=min,max-1 do
res[i] = false
end
for _,v in ipairs(values) do
if select(1, string.find(v, "/")) == nil then
local range = CronExp.getRange(v, min, max)
for i=range[1],range[2] do
res[i] = true
end
else
local split = string.Split(v, "/")
local range = CronExp.getRange(split[1], min, max)
if select(1, string.find(split[1], "-")) == nil then
range[2] = max - 1
end
if tonumber(split[2]) == 0 then
error("Increment cannot be zero: " .. v)
end
for i=range[1],range[2],tonumber(split[2]) do
res[i] = true
end
end
end
return res
end
function CronExp.replaceOrdinals(input, replacement)
local res = input
for i=1,#replacement do
res = string.Replace(res, replacement[i], i)
end
return res
end
function CronExp.getMonth(value)
local res = CronExp.getNumberBits(CronExp.replaceOrdinals(value, CronConstants.MONTHS), 1, CronConstants.MAX_MONTHS)
-- Shift everything back to 0-indexed so it's consistent
for i=1,CronConstants.MAX_MONTHS do
if res[i] then
res[i - 1] = true
res[i] = nil
end
end
return res
end
function CronExp.getDaysOfWeek(value)
local res = CronExp.getNumberBits(CronExp.replaceOrdinals(value, CronConstants.DAYS), 0, CronConstants.MAX_DAYS_OF_WEEK)
-- Make sunday 0
if res[7] == true then
res[0] = true
res[7] = nil
end
return res
end
function CronExp.fromString(exp)
local res = {}
local parts = string.Split(exp, " ")
res.seconds = CronExp.getNumberBits(parts[1], 0, CronConstants.MAX_SECONDS)
res.minutes = CronExp.getNumberBits(parts[2], 0, CronConstants.MAX_MINUTES)
res.hours = CronExp.getNumberBits(parts[3], 0, CronConstants.MAX_HOURS)
if #parts[4] == 1 and parts[4][1] == "?" then
parts[4] = "*" .. string.sub(parts[4], 2)
end
res.daysMonth = CronExp.getNumberBits(parts[4], 0, CronConstants.MAX_DAYS_OF_MONTH)
res.month = CronExp.getMonth(parts[5])
if #parts[6] == 1 and parts[6][1] == "?" then
parts[6] = "*" .. string.sub(parts[6], 2)
end
res.daysWeek = CronExp.getDaysOfWeek(parts[6])
return res
end
function CronExp.findNextBit(bits, from, to)
for i=from,to-1 do
if bits[i] == true then return i end
end
return -1
end
function CronExp.findNext(bits, value, max, date, field, nextField, lowerOrders)
local nextValue = CronExp.findNextBit(bits, value, max)
local notFound = (nextValue == -1)
if notFound then
CronExp.addToField(nextField, date, 1)
local res = CronExp.resetMin(field, date)
if res == -1 then
return -1
end
nextValue = CronExp.findNextBit(bits, 0, value)
notFound = (nextValue == -1)
end
if notFound or value != nextValue then
CronExp.setField(field, date, nextValue)
CronExp.resetAllMin(date, lowerOrders)
end
return nextValue
end
function CronExp.findNextDay(date, daysMonth, daysWeek, resets)
local count = 0
local dayOfMonth = date.day - 1
local dayOfWeek = date.wday - 1
while (daysMonth[dayOfMonth] == false or daysWeek[dayOfWeek] == false) and count < 10 do
count = count + 1
local res = CronExp.addToField(CronFields.DAY_OF_MONTH, date, 1)
if res == -1 then return -1 end
dayOfMonth = date.day - 1
dayOfWeek = date.wday - 1
CronExp.resetAllMin(date, resets)
end
return dayOfMonth
end
function CronExp.doNext(exp, date)
local second, updateSecond, minute, updateMinute, hour, updateHour, dayOfWeek, dayOfMonth, updateDayOfMonth, month, updateMonth = 0
local res = 0
local resets = {}
local emptyList = {}
for i=0,7 do
resets[i] = -1
emptyList[i] = -1
end
second = date.sec
updateSecond = CronExp.findNext(exp.seconds, second, CronConstants.MAX_SECONDS, date, CronFields.SECOND, CronFields.MINUTE, emptyList)
if updateSecond == -1 then goto returnResult end
if second == updateSecond then
CronExp.pushToFields(resets, CronFields.SECOND)
end
minute = date.min
updateMinute = CronExp.findNext(exp.minutes, minute, CronConstants.MAX_MINUTES, date, CronFields.MINUTE, CronFields.HOUR, resets)
if updateMinute == -1 then goto returnResult end
if minute == updateMinute then
CronExp.pushToFields(resets, CronFields.MINUTE)
else
res = CronExp.doNext(exp, date)
end
hour = date.hour
updateHour = CronExp.findNext(exp.hours, hour, CronConstants.MAX_HOURS, date, CronFields.HOUR, CronFields.DAY_OF_WEEK, resets)
if updateHour == -1 then goto returnResult end
if hour == updateHour then
CronExp.pushToFields(resets, CronFields.HOUR)
else
res = CronExp.doNext(exp, date)
end
-- Subtract 1 because they are 1-indexed
dayOfWeek = date.wday - 1
dayOfMonth = date.day - 1
updateDayOfMonth = CronExp.findNextDay(date, exp.daysMonth, exp.daysWeek, resets)
if updateDayOfMonth == -1 then goto returnResult end
if dayOfMonth == updateDayOfMonth then
CronExp.pushToFields(resets, CronFields.DAY_OF_MONTH)
else
res = CronExp.doNext(exp, date)
end
month = date.month
updateMonth = CronExp.findNext(exp.month, month, CronConstants.MAX_MONTHS, date, CronFields.MONTH, CronFields.YEAR, resets)
if updateMonth == -1 then goto returnResult end
if not month == updateMonth then
res = CronExp.doNext(exp, date)
end
::returnResult::
return date
end
function CronExp.getNext(exp, date)
local calculated = CronExp.doNext(exp, date)
-- Prevent it from giving the current time back
if date == calculated then
CronExp.addToField(CronFields.SECOND, date, 1)
calculated = CronExp.doNext(exp, date)
end
return calculated
end