task_kill.py 1.8 KB
# coding=utf-8
#author:        4N
#createtime:    2020/9/4
#email:         nheweijun@sina.com

from ..models import db,Task,Table


from app.util.component.ApiTemplate import ApiTemplate
import os
import signal
import platform


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()
            pid = task.task_pid
            if platform.system().lower().__contains__("windows"):
                os.popen('taskkill.exe /pid:' + str(pid))
            else:
                os.kill(pid,signal.SIGILL)
            #处理kill任务后的事情
            self.fix_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:
            pass
        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})
        if task.task_type==3:
            pass
        if task.task_type==3:
            pass

        db.session.commit()

    api_doc = {
        "tags": ["任务接口"],
        "parameters": [
            {"name": "task_guid",
             "in": "formData",
             "type": "string"},
        ],
        "responses": {
            200: {
                "schema": {
                    "properties": {
                    }
                }
            }
        }
    }