From bc184f03cc0574f4d55833be1a01e0b5bc023a05 Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Wed, 26 Jun 2024 17:32:22 +0800 Subject: [PATCH] update --- plugins/sphinx/index.py | 2 + plugins/sphinx/tool_cron.py | 129 ++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 plugins/sphinx/tool_cron.py diff --git a/plugins/sphinx/index.py b/plugins/sphinx/index.py index 5e614b3e7..d57c17e14 100755 --- a/plugins/sphinx/index.py +++ b/plugins/sphinx/index.py @@ -192,6 +192,8 @@ def sphOp(method): def start(): + import tool_cron + tool_cron.createBgTask() return sphOp('start') diff --git a/plugins/sphinx/tool_cron.py b/plugins/sphinx/tool_cron.py new file mode 100644 index 000000000..7a71d5927 --- /dev/null +++ b/plugins/sphinx/tool_cron.py @@ -0,0 +1,129 @@ +# 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 'sphinx' + + +def getPluginDir(): + return mw.getPluginDir() + '/' + getPluginName() + + +def getServerDir(): + return mw.getServerDir() + '/' + getPluginName() + + +def getTaskConf(): + conf = getServerDir() + "/task_config.json" + return conf + + +def getConfigData(): + try: + return json.loads(mw.readFile(getTaskConf())) + except: + pass + return { + "task_id": -1, + "period": "day-n", + "where1": "7", + "hour": "0", + "minute": "15", + } + + +def createBgTask(): + removeBgTask() + createBgTaskByName(getPluginName()) + + +def createBgTaskByName(name): + args = getConfigData() + _name = "[勿删]SPHINX全量更新[" + name + "]" + res = mw.M("crontab").field("id, name").where("name=?", (_name,)).find() + if res: + return True + + if "task_id" in args and args["task_id"] > 0: + res = mw.M("crontab").field("id, name").where( + "id=?", (args["task_id"],)).find() + if res and res["id"] == args["task_id"]: + print("计划任务已经存在!") + return True + import crontab_api + api = crontab_api.crontab_api() + + mw_dir = mw.getRunDir() + cmd = ''' +mw_dir=%s +rname=%s +plugin_path=%s +script_path=%s +logs_file=$plugin_path/${rname}.log +''' % (mw_dir, name, getServerDir(), getPluginDir()) + cmd += 'echo "★【`date +"%Y-%m-%d %H:%M:%S"`】 STSRT★" >> $logs_file' + "\n" + cmd += 'echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" >> $logs_file' + "\n" + cmd += 'echo "python3 $script_path/index.py update_all >> $logs_file 2>&1"' + "\n" + cmd += 'cd $mw_dir && python3 $script_path/index.py update_all >> $logs_file 2>&1' + "\n" + cmd += 'echo "【`date +"%Y-%m-%d %H:%M:%S"`】 END★" >> $logs_file' + "\n" + cmd += 'echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" >> $logs_file' + "\n" + + params = { + 'name': _name, + 'type': args['period'], + 'week': "", + 'where1': args['where1'], + 'hour': args['hour'], + 'minute': args['minute'], + 'save': "", + 'backup_to': "", + 'stype': "toShell", + 'sname': '', + 'sbody': cmd, + 'urladdress': '', + } + + task_id = api.add(params) + if task_id > 0: + args["task_id"] = task_id + args["name"] = name + mw.writeFile(getTaskConf(), json.dumps(args)) + + +def removeBgTask(): + cfg = getConfigData() + if "task_id" in cfg 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]: + cfg["task_id"] = -1 + mw.writeFile(getTaskConf(), json.loads(cfg)) + return True + return False + + +if __name__ == "__main__": + if len(sys.argv) > 1: + action = sys.argv[1] + if action == "remove": + removeBgTask() + elif action == "add": + createBgTask()