This commit is contained in:
2026-08-14 15:47:51 +08:00
parent 7ab5c9a5c9
commit 4a2355a902
+4 -5
View File
@@ -44,13 +44,12 @@ async def _update_task_status(task_id: str, status: str, error_msg: str | None =
await db.commit()
def _execute_internal_method(config: str | None) -> dict:
async def _execute_internal_method(config: str | None) -> dict:
"""执行内部方法调用。
配置 JSON 中 module 和 function 指定要调用的函数,其余字段作为 kwargs 传入。
支持同步函数和异步函数(async def)。
"""
import asyncio
import importlib
from inspect import iscoroutinefunction
@@ -69,8 +68,8 @@ def _execute_internal_method(config: str | None) -> dict:
# 剩余字段作为 kwargs 传给函数
start = time.monotonic()
if iscoroutinefunction(func):
# 异步函数:在当前事件循环中 await
result = asyncio.get_event_loop().run_until_complete(func(**cfg))
# 异步函数:直接 await
result = await func(**cfg)
else:
result = func(**cfg)
duration_ms = int((time.monotonic() - start) * 1000)
@@ -97,7 +96,7 @@ def execute_scheduled_task(self, task_id: str):
task_config = task.config
try:
exec_result = _execute_internal_method(task_config)
exec_result = await _execute_internal_method(task_config)
await _update_task_status(task_id, "success")
logger.info("定时任务执行成功: %s -> %s", task_id, exec_result)
except Exception as e: