Skip to content

Commit

Permalink
Merge pull request #69 from Cassius0924/fix-cron
Browse files Browse the repository at this point in the history
[Fix] 定时任务多个命令时命令调用不正确问题
  • Loading branch information
Cassius0924 authored Feb 24, 2024
2 parents a6c1b1a + fbd401b commit 4258658
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions wechatter/config/parsers/task_cron_list_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
Expand All @@ -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]} 字段")
Expand Down
4 changes: 2 additions & 2 deletions wechatter/models/scheduler/cron_task.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,4 +10,4 @@ class CronTask(BaseModel):
enabled: bool
desc: str
cron_trigger: CronTrigger
funcs: List[Callable]
funcs: List[Tuple[Callable, Tuple]]
4 changes: 2 additions & 2 deletions wechatter/scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("定时任务已启动")
Expand Down

0 comments on commit 4258658

Please sign in to comment.