forked from wuranxu/pity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.py
88 lines (76 loc) · 2.74 KB
/
scheduler.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
from typing import List
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from app.core.executor import Executor
from app.handler.fatcory import PityResponse
class Scheduler(object):
scheduler: AsyncIOScheduler = None
@staticmethod
def init(scheduler):
Scheduler.scheduler = scheduler
@staticmethod
def configure(**kwargs):
Scheduler.scheduler.configure(**kwargs)
@staticmethod
def start():
Scheduler.scheduler.start()
@staticmethod
def add_test_plan(plan_id, plan_name, cron):
return Scheduler.scheduler.add_job(func=Executor.run_test_plan, args=(plan_id,),
name=plan_name, id=str(plan_id),
trigger=CronTrigger.from_crontab(cron))
@staticmethod
def edit_test_plan(plan_id, plan_name, cron):
"""
通过测试计划id,更新测试计划任务的cron,name等数据
:param plan_id:
:param plan_name:
:param cron:
:return:
"""
job = Scheduler.scheduler.get_job(str(plan_id))
if job is None:
# 新增job
return Scheduler.add_test_plan(plan_id, plan_name, cron)
Scheduler.scheduler.modify_job(job_id=str(plan_id), trigger=CronTrigger.from_crontab(cron), name=plan_name)
Scheduler.scheduler.pause_job(str(plan_id))
Scheduler.scheduler.resume_job(str(plan_id))
@staticmethod
def pause_resume_test_plan(plan_id, status):
"""
暂停或恢复测试计划,会影响到next_run_at
:param plan_id:
:param status:
:return:
"""
if status:
Scheduler.scheduler.resume_job(job_id=str(plan_id))
else:
Scheduler.scheduler.pause_job(job_id=str(plan_id))
@staticmethod
def remove(plan_id):
"""
删除job,当删除测试计划时,调用此方法
:param plan_id:
:return:
"""
Scheduler.scheduler.remove_job(str(plan_id))
@staticmethod
def list_test_plan(data: List):
ans = []
for d, follow in data:
temp = PityResponse.model_to_dict(d)
temp['follow'] = follow is not None
job = Scheduler.scheduler.get_job(str(temp.get('id')))
if job is None:
# 说明job初始化失败了
temp["state"] = 2
ans.append(temp)
continue
if job.next_run_time is None:
# 说明job被暂停了
temp["state"] = 3
else:
temp["next_run"] = job.next_run_time.strftime("%Y-%m-%d %H:%M:%S")
ans.append(temp)
return ans