This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
job_mgmt.py
73 lines (59 loc) · 2.12 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
# -*- 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:
while True:
# print("runPendingJobs")
schedule.run_pending()
time.sleep(1)