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("定时任务已启动")