-
Notifications
You must be signed in to change notification settings - Fork 256
/
job_mgmt.py
84 lines (68 loc) · 2.46 KB
/
job_mgmt.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
# -*- coding: utf-8 -*-
import time
from typing import Any, Callable
import schedule
class Job(object):
def __init__(self) -> None:
pass
def onEverySeconds(self, seconds: int, task: Callable[..., Any], *args, **kwargs) -> None:
"""
每 seconds 秒执行
:param seconds: 间隔,秒
:param task: 定时执行的方法
:return: None
"""
schedule.every(seconds).seconds.do(task, *args, **kwargs)
def onEveryMinutes(self, minutes: int, task: Callable[..., Any], *args, **kwargs) -> None:
"""
每 minutes 分钟执行
:param minutes: 间隔,分钟
:param task: 定时执行的方法
:return: None
"""
schedule.every(minutes).minutes.do(task, *args, **kwargs)
def onEveryHours(self, hours: int, task: Callable[..., Any], *args, **kwargs) -> None:
"""
每 hours 小时执行
:param hours: 间隔,小时
:param task: 定时执行的方法
:return: None
"""
schedule.every(hours).hours.do(task, *args, **kwargs)
def onEveryDays(self, days: int, task: Callable[..., Any], *args, **kwargs) -> None:
"""
每 days 天执行
:param days: 间隔,天
:param task: 定时执行的方法
:return: None
"""
schedule.every(days).days.do(task, *args, **kwargs)
def onEveryTime(self, times: int, task: Callable[..., Any], *args, **kwargs) -> None:
"""
每天定时执行
:param times: 时间字符串列表,格式:
- For daily jobs -> HH:MM:SS or HH:MM
- For hourly jobs -> MM:SS or :MM
- For minute jobs -> :SS
:param task: 定时执行的方法
:return: None
例子: times=["10:30", "10:45", "11:00"]
"""
if not isinstance(times, list):
times = [times]
for t in times:
schedule.every(1).days.at(t).do(task, *args, **kwargs)
def runPendingJobs(self) -> None:
schedule.run_pending()
if __name__ == "__main__":
def printStr(s):
print(s)
job = Job()
job.onEverySeconds(59, printStr, "onEverySeconds 59")
job.onEveryMinutes(59, printStr, "onEveryMinutes 59")
job.onEveryHours(23, printStr, "onEveryHours 23")
job.onEveryDays(1, printStr, "onEveryDays 1")
job.onEveryTime("23:59", printStr, "onEveryTime 23:59")
while True:
job.runPendingJobs()
time.sleep(1)