From fbd401b461784b04422a59c31981ae4b6f837bb7 Mon Sep 17 00:00:00 2001 From: Cassius0924 <2670226747@qq.com> Date: Sat, 24 Feb 2024 16:18:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E5=91=BD=E4=BB=A4=E6=97=B6=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E4=B8=8D=E6=AD=A3=E7=A1=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/parsers/task_cron_list_parser.py | 16 ++++++++-------- wechatter/models/scheduler/cron_task.py | 4 ++-- wechatter/scheduler/scheduler.py | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/wechatter/config/parsers/task_cron_list_parser.py b/wechatter/config/parsers/task_cron_list_parser.py index 8cbe106b..66f4a0f4 100644 --- a/wechatter/config/parsers/task_cron_list_parser.py +++ b/wechatter/config/parsers/task_cron_list_parser.py @@ -87,22 +87,22 @@ def parse_task_cron_list(task_cron_list: List) -> List: logger.error(f"[{desc}] 任务的命令不存在: {cmd}") raise ValueError(f"[{desc}] 任务的命令不存在: {cmd}") - def _func(): + def _func(_cmd: str, *_args): # 如果命令支持引用回复 - if COMMANDS[cmd]["is_quotable"]: + if COMMANDS[_cmd]["is_quotable"]: try: - message, q_response = COMMANDS[cmd]["mainfunc"](*args) + message, q_response = COMMANDS[_cmd]["mainfunc"](*_args) except TypeError as e: # 如果用户配置的参数不正确 logger.error(f"[{desc}] 任务的命令参数不正确: {str(e)}") raise TypeError(f"[{desc}] 任务的命令参数不正确: {str(e)}") quoted_response = QuotedResponse( - command=cmd, + command=_cmd, response=q_response, ) else: # 不支持引用回复的命令 try: - message = COMMANDS[cmd]["mainfunc"](*args) + message = COMMANDS[_cmd]["mainfunc"](*_args) except TypeError as e: logger.error(f"[{desc}] 任务的命令参数不正确: {str(e)}") raise TypeError(f"[{desc}] 任务的命令参数不正确: {str(e)}") @@ -129,12 +129,12 @@ def _func(): quoted_response=quoted_response, ) # 删除发送的文件 - if cmd in SEND_FILE_COMMANDS: + if _cmd in SEND_FILE_COMMANDS: if os.path.exists(message): os.remove(message) - logger.info(f"[{desc}] 任务的命令执行成功: {cmd}") + logger.info(f"[{desc}] 任务的命令执行成功: {_cmd}") - funcs.append(_func) + funcs.append((_func, (cmd, *args))) except KeyError as e: if task_cron.get("task"): logger.error(f"[{task_cron['task']}] 定时任务规则缺少 {e.args[0]} 字段") diff --git a/wechatter/models/scheduler/cron_task.py b/wechatter/models/scheduler/cron_task.py index a57f4946..20ac9d69 100644 --- a/wechatter/models/scheduler/cron_task.py +++ b/wechatter/models/scheduler/cron_task.py @@ -1,4 +1,4 @@ -from typing import Callable, List +from typing import Callable, List, Tuple from apscheduler.triggers.cron import CronTrigger from pydantic import BaseModel, ConfigDict @@ -10,4 +10,4 @@ class CronTask(BaseModel): enabled: bool desc: str cron_trigger: CronTrigger - funcs: List[Callable] + funcs: List[Tuple[Callable, Tuple]] diff --git a/wechatter/scheduler/scheduler.py b/wechatter/scheduler/scheduler.py index 99cb2e5b..0f51fd9e 100644 --- a/wechatter/scheduler/scheduler.py +++ b/wechatter/scheduler/scheduler.py @@ -20,8 +20,8 @@ def startup(self): return for cron_task in self.cron_task_list: if cron_task.enabled: - for func in cron_task.funcs: - self.scheduler.add_job(func, cron_task.cron_trigger) + for func, args in cron_task.funcs: + self.scheduler.add_job(func, cron_task.cron_trigger, args=args) logger.info(f"定时任务已添加: {cron_task.desc}") self.scheduler.start() logger.info("定时任务已启动")