From c8b50de3e72a2a0423373ef25f93cc4a58a8cbee Mon Sep 17 00:00:00 2001 From: midoks Date: Tue, 26 Jul 2022 11:48:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=AB=99=E7=BB=9F=E8=AE=A1=20x17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- class/core/crontab_api.py | 10 ++- plugins/webstats/tool_task.py | 131 ++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 plugins/webstats/tool_task.py diff --git a/class/core/crontab_api.py b/class/core/crontab_api.py index 4df870793..385b04815 100755 --- a/class/core/crontab_api.py +++ b/class/core/crontab_api.py @@ -263,9 +263,9 @@ class crontab_api: except Exception as e: return mw.returnJson(False, '删除失败:' + str(e)) - def delete(self, task_id): + def delete(self, tid): - find = mw.M('crontab').where("id=?", (sid,)).field('name,echo').find() + find = mw.M('crontab').where("id=?", (tid,)).field('name,echo').find() if not self.removeForCrond(find['echo']): return (False, '无法写入文件,请检查是否开启了系统加固功能!') @@ -278,7 +278,7 @@ class crontab_api: if os.path.exists(sfile): os.remove(sfile) - mw.M('crontab').where("id=?", (sid,)).delete() + mw.M('crontab').where("id=?", (tid,)).delete() mw.writeLog('计划任务', mw.getInfo('删除计划任务[{1}]成功!', (find['name'],))) return (True, "OK") @@ -509,6 +509,10 @@ echo "-------------------------------------------------------------------------- return False else: file = u_file + + if mw.isAppleSystem(): + return True + conf = mw.readFile(file) rep = ".+" + str(echo) + ".+\n" conf = re.sub(rep, "", conf) diff --git a/plugins/webstats/tool_task.py b/plugins/webstats/tool_task.py new file mode 100644 index 000000000..ff753e1b3 --- /dev/null +++ b/plugins/webstats/tool_task.py @@ -0,0 +1,131 @@ +# coding:utf-8 + +import sys +import io +import os +import time +import json + +sys.path.append(os.getcwd() + "/class/core") +import mw + + +app_debug = False +if mw.isAppleSystem(): + app_debug = True + + +def getPluginName(): + return 'webstats' + + +def getPluginDir(): + return mw.getPluginDir() + '/' + getPluginName() + + +sys.path.append(getPluginDir() + "/class") +from LuaMaker import LuaMaker + + +def listToLuaFile(path, lists): + content = LuaMaker.makeLuaTable(lists) + content = "return " + content + mw.writeFile(path, content) + + +def getServerDir(): + return mw.getServerDir() + '/' + getPluginName() + + +def getConf(): + conf = getServerDir() + "/lua/config.json" + return conf + + +def getTaskConf(): + conf = getServerDir() + "/task_config.json" + return conf + + +def getConfigData(): + try: + return json.loads(mw.readFile(getTaskConf())) + except: + pass + return { + "task_id": -1, + "task_list": ["migrate_hot_logs"], + "default_execute_hour": 3, + "default_execute_minute": 10, + } + + +def createBgTask(): + cfg = getConfigData() + name = "[勿删]网站统计插件定时任务" + res = mw.M("crontab").field("id, name").where("name=?", (name,)).find() + if res: + return True + + if "task_id" in cfg.keys() and cfg["task_id"] > 0: + res = mw.M("crontab").field("id, name").where( + "id=?", (cfg["task_id"],)).find() + if res and res["id"] == cfg["task_id"]: + print("计划任务已经存在!") + return True + import crontab_api + api = crontab_api.crontab_api() + + cmd = "nice -n 10 python3 " + getPluginDir() + "/tool_task.py execute" + params = { + 'name': name, + 'type': 'day', + 'week': "", + 'where1': "", + 'hour': cfg['default_execute_hour'], + 'minute': cfg['default_execute_hour'], + 'save': "", + 'backup_to': "", + 'stype': "toShell", + 'sname': '', + 'sbody': cmd, + 'urladdress': '', + } + + task_id = api.add(params) + if task_id > 0: + cfg["task_id"] = task_id + mw.writeFile(getTaskConf(), json.dumps(cfg)) + + +def removeBgTask(): + cfg = getConfigData() + if "task_id" in cfg.keys() and cfg["task_id"] > 0: + res = mw.M("crontab").field("id, name").where( + "id=?", (cfg["task_id"],)).find() + if res and res["id"] == cfg["task_id"]: + import crontab_api + api = crontab_api.crontab_api() + + data = api.delete(cfg["task_id"]) + if data[0]: + print(data[1]) + cfg["task_id"] = -1 + mw.writeFile(getTaskConf(), json.dumps(cfg)) + return True + return False + + +def execute(): + print("helo") + + +if __name__ == "__main__": + if len(sys.argv) > 1: + action = sys.argv[1] + if action == "execute": + execute() + elif action == "remove": + removeBgTask() + elif action == "add": + createBgTask()