task_kill.py
3.6 KB
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
# coding=utf-8
#author: 4N
#createtime: 2020/9/4
#email: nheweijun@sina.com
from ..models import db,Task,Table,InsertingLayerName,Process
from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.StructurePrint import StructurePrint
import os
import signal
import platform
import datetime
import uuid
from app.util.component.UserCheck import UserCheck
import requests
class Api(ApiTemplate):
api_name = "停止任务"
def para_check(self):
pass
def process(self):
res = {}
try:
task_guid = self.para.get("task_guid")
task = Task.query.filter_by(guid=task_guid).one_or_none()
if not task:
raise Exception("任务不存在!")
#验证权限
UserCheck.verify(task.creator)
pid = task.task_pid
try:
if platform.system().lower().__contains__("windows"):
exec_result = os.popen('taskkill.exe /pid:' + str(pid))
else:
if task.task_type == 5:
url = "{}/API/Image/TaskKill".format(task.parameter)
StructurePrint().print(url)
response: requests.Response = requests.post(url, {"pid":pid})
if response.status_code == 200:
if response.json()["result"]:
pass
else:
raise Exception(response.json()["msg"])
else:
raise Exception("影像服务器连接失败!")
else:
os.kill(pid,signal.SIGILL)
except Exception as e:
StructurePrint().print("Kill task 失败")
#处理kill任务后的事情
self.fix_task(task)
res["msg"] = "Kill成功!"
res["result"] = True
except Exception as e:
db.session.rollback()
raise e
return res
def fix_task(self,task):
if task.task_type==1:
ilns = InsertingLayerName.query.filter_by(task_guid=task.guid).all()
for iln in ilns:
db.session.delete(iln)
if task.task_type==2:
table = Table.query.filter_by(guid=task.table_guid).one_or_none()
if len(table.relate_table_vacuates.all())>0:
Table.query.filter_by(guid=task.table_guid).update({"is_vacuate":1})
else:
Table.query.filter_by(guid=task.table_guid).update({"is_vacuate": 0})
if task.task_type==3:
pass
if task.task_type==4:
pass
if task.task_type==5:
pass
Task.query.filter_by(guid=task.guid).update({"state":-1})
message = "{} 任务被强行中止!".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
task_process = Process(guid=uuid.uuid1().__str__(), message=message, time=datetime.datetime.now(),
task_guid=task.guid)
db.session.add(task_process)
db.session.commit()
return None
api_doc = {
"tags": ["任务接口"],
"parameters": [
{"name": "task_guid",
"in": "formData",
"type": "string"},
],
"responses": {
200: {
"schema": {
"properties": {
}
}
}
}
}