/
rab_crontab.py
360 lines (337 loc) · 10.7 KB
/
rab_crontab.py
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
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
#
# @AUTHOR: Rabbir
# @FILE: /root/GitHub/rab_python_packages/rab_crontab.py
# @DATE: 2021/08/12 Thu
# @TIME: 09:30:35
#
# @DESCRIPTION: 定时任务模块
import sys
import time
import calendar
import datetime
sys.path.append("..") if (".." not in sys.path) else True
from rab_python_packages import rab_logging
# 日志记录
r_logger = rab_logging.r_logger()
"""
@description: 获取上一个整分、整时、整天的时间戳
-------
@param:
-------
@return:
"""
def get_last_whole_timestamp(timestamp=int(time.time()), method="minute"):
if (method == "minute"):
return ((timestamp//60)) * 60
elif(method == "hour"):
return ((timestamp//3600)) * 3600
elif(method == "day"):
return ((timestamp//86400)) * 86400
elif(method == "month"):
return int(time.mktime(datetime.date(
datetime.date.today().year, \
datetime.date.today().month, 1).timetuple()))
else:
return None
"""
@description: 获取下一个整分、整时、整天的时间戳
-------
@param:
-------
@return:
"""
def get_next_whole_timestamp(timestamp=int(time.time()), method="minute"):
if (method == "minute"):
return ((timestamp//60)+1) * 60
elif(method == "hour"):
return ((timestamp//3600)+1) * 3600
elif(method == "day"):
return ((timestamp//86400)+1) * 86400
elif(method == "month"):
return int(time.mktime(datetime.date(
datetime.date.today().year, \
datetime.date.today().month,1).timetuple()))
else:
return None
"""
@description: 解析 crontab 时间规则以获取分、时、日
-------
@param:
-------
@return:
"""
def parse_crontab_time_setting(crontab_time_setting):
minute = crontab_time_setting[0]
hour = crontab_time_setting[1]
day_of_month = crontab_time_setting[2]
month = crontab_time_setting[3]
day_of_week = crontab_time_setting[4]
# 暂不支持每周和每月固定日期执行
if (month != "*" and day_of_week != "*"):
r_logger.error("定时任务模块暂不支持指定月和指定星期执行!")
return None
# 获取需要定时执行的分钟数
minutes = []
if (minute != "*"):
# 单个分钟
if (minute.isdigit()):
minutes = [int(minute)]
# 手动设定多个
elif("," in minute):
for m in minute.split(","):
minutes.append(int(m))
# 每隔固定时间执行
elif("/" in minute):
for m in range(0, 60):
if (m%int(minute.split("/")[1]) == 0):
minutes.append(int(m))
else:
minutes = list(range(0, 60))
# 获取需要定时执行的小时数
hours = []
if (hour != "*"):
# 单个小时
if (hour.isdigit()):
hours = [int(hour)]
# 手动设定多个
elif("," in hour):
for h in hour.split(","):
hours.append(int(h))
# 每隔固定时间执行
elif("/" in hour):
for h in range(0, 24):
if (h%int(hour.split("/")[1]) == 0):
hours.append(int(h))
else:
hours = list(range(0, 24))
# 获取需要定时执行的天数(以月为单位,并且会拓展到下个月 1 日 24 点)
day = day_of_month
days = []
week, days_num = calendar.monthrange(
datetime.datetime.now().year, datetime.datetime.now().month)
if (day != "*"):
# 单个日期
if (day.isdigit()):
days = [int(day)]
# 手动设定多个
elif("," in day):
for d in day.split(","):
days.append(int(d))
# 每隔固定时间执行
elif("/" in day):
# 每月 1 日必定符合
days.append(0)
# 循环剩余日期
for d in range(1, days_num+1):
if (d%int(day.split("/")[1]) == 0):
days.append(int(d))
else:
days = list(range(0, days_num+1))
return minutes, hours, days
"""
@description: 获取当月所有满足要求的时间点时间戳
-------
@param:
-------
@return:
"""
def get_run_timestamps(crontab_time_setting):
# 先生成所有符合 crontab 要求的时间戳
run_timestamps = []
minutes, hours, days = parse_crontab_time_setting(
crontab_time_setting)
for day in days:
for hour in hours:
for minute in minutes:
this_timestamp = get_last_whole_timestamp(method="month") \
+ day*86400 + hour*3600 + minute*60
run_timestamps.append(this_timestamp)
return run_timestamps
"""
@description: r_crontab 类
-------
@param:
-------
@return:
"""
class r_crontab():
"""
@description: 初始化
-------
@param:
-------
@return:
"""
def __init__(self):
self.task = {}
"""
@description: 新增任务,默认周期为 30 天
-------
@param:
-------
@return:
"""
def add(self, task_name, crontab_time_setting):
self.task[task_name] = {
"crontab_time_setting": crontab_time_setting,
"run_timestamps": get_run_timestamps(crontab_time_setting),
"no_stop": False}
"""
@description: 续约任务
-------
@param:
-------
@return:
"""
def renew(self, task_name):
self.task[task_name]["run_timestamps"] = get_run_timestamps(
self.task[task_name]["crontab_time_setting"])
"""
@description: 运行一次任务以记录时间戳
-------
@param:
-------
@return:
"""
def run(self, task_name):
self.task[task_name]["last_run_over_timestamp"] = None
self.task[task_name]["last_run_start_timestamp"] = int(time.time())
"""
@description: 记录运行结束
-------
@param:
-------
@return:
"""
def over(self, task_name):
self.task[task_name]["last_run_over_timestamp"] = int(time.time())
# 如果接下来剩下少于一个可用的时间戳则进行续约
available_run_timestamps = []
for run_timestamp in self.task[task_name]["run_timestamps"]:
if (run_timestamp >= int(time.time())):
available_run_timestamps.append(run_timestamp)
if (len(available_run_timestamps) <= 1):
self.renew(task_name)
"""
@description: 获取下一次运行的时间戳
-------
@param:
-------
@return:
"""
def get_next_run_timestamp(self, task_name):
for run_timestamp in self.task[task_name]["run_timestamps"]:
if (run_timestamp > int(time.time())):
return run_timestamp
return None
"""
@description: 到了运行的时间点
-------
@param:
-------
@return:
"""
def is_time_2_run(self, task_name):
# 如果是不间断运行则直接返回 True
if (self.task[task_name]["no_stop"]):
return True
# 如果曾经开始并结束过
if ("last_run_over_timestamp" in self.task[task_name].keys()
and self.task[task_name]["last_run_over_timestamp"]):
# 遍历所有需要运行的时间戳
for run_timestamp_index, run_timestamp in enumerate(
self.task[task_name]["run_timestamps"]):
# 判断当前时间戳是否比上次运行时间点的下一次时间戳还要大
if ((self.task[task_name]["last_run_over_timestamp"] \
>= run_timestamp)
and (self.task[task_name]["last_run_over_timestamp"] \
< self.task[task_name]["run_timestamps"][(
run_timestamp_index+1)])
and (int(time.time()) >= self.task[task_name][
"run_timestamps"][(run_timestamp_index+1)])):
return True
# 时间戳循环结束返回 False
return False
# 未开始过则直接开始
else:
return True
"""
@description: 等待下一次运行时间的到来
-------
@param:
-------
@return:
"""
def wait(self, task_name):
# 如果是不间断运行则直接返回 True
if (self.task[task_name]["no_stop"]):
return True
next_run_timestamp = self.get_next_run_timestamp(task_name)
r_logger.info("下一次 {task_name} 任务执行的时间:{time}".format(
task_name=task_name, time=time.strftime(
"%Y-%m-%d %H:%M:%S",time.localtime(next_run_timestamp))))
r_logger.info("等待中......")
while True:
if (int(time.time()) > next_run_timestamp):
return True
else:
time.sleep(1)
"""
@description: 距上次运行结束是否是新的分钟、小时、天
-------
@param:
-------
@return:
"""
def is_new(self, task_name, method="day"):
# 如果没有开始过
if ("last_run_start_timestamp" not in self.task[task_name].keys()):
return True
# 如果还没结束
elif(not "last_run_over_timestamp"):
return False
# 如果开始过则判定是否到了新的时间点
else:
if (method=="minute"):
if ((int(time.time())-get_last_whole_timestamp(self.task[
task_name]["last_run_start_timestamp"], "minute")) \
>= 60):
return True
else:
return False
elif (method=="hour"):
if ((int(time.time())-get_last_whole_timestamp(self.task[
task_name]["last_run_start_timestamp"], "hour")) \
>= 3600):
return True
else:
return False
elif (method=="day"):
if ((int(time.time())-get_last_whole_timestamp(self.task[
task_name]["last_run_start_timestamp"], "day")) \
>= 86400):
return True
else:
return False
"""
@description: 单体测试
-------
@param:
-------
@return:
"""
if __name__ == "__main__":
r_crontab = r_crontab()
r_crontab.add("test", ["*/3", "*", "*", "*", "*"])
print(len(r_crontab.task["test"]["run_timestamps"]))
# r_crontab.wait("test")
# 测试到点执行
# while True:
# time.sleep(10)
# if (r_crontab.is_time_2_run("test")):
# r_crontab.run("test")
# print("run")
# r_crontab.over("test")
# print("wait next: {}".format(r_crontab.get_next_run_timestamp("test")))