This commit is contained in:
2026-08-14 15:39:22 +08:00
parent 049bbb0d24
commit 556ea4dbd2
+12 -17
View File
@@ -31,21 +31,17 @@ from app.tasks.celery_app import celery_app
logger = logging.getLogger("video_gen")
def _update_task_status(task_id: str, status: str, error_msg: str | None = None) -> None:
async def _update_task_status(task_id: str, status: str, error_msg: str | None = None) -> None:
"""更新任务最后执行状态。"""
async def _do():
async with async_session() as db:
result = await db.execute(select(ScheduledTask).where(ScheduledTask.id == task_id))
task = result.scalar_one_or_none()
if task is None:
return
task.last_run_at = datetime.now(timezone.utc).isoformat()
task.last_status = status
task.last_error = error_msg
await db.commit()
run_async(_do())
async with async_session() as db:
result = await db.execute(select(ScheduledTask).where(ScheduledTask.id == task_id))
task = result.scalar_one_or_none()
if task is None:
return
task.last_run_at = datetime.now(timezone.utc).isoformat()
task.last_status = status
task.last_error = error_msg
await db.commit()
def _execute_internal_method(config: str | None) -> dict:
@@ -91,16 +87,15 @@ def execute_scheduled_task(self, task_id: str):
if not task.is_active:
logger.info("定时任务已禁用,跳过执行: %s", task_id)
return
task_config = task.config
try:
exec_result = _execute_internal_method(task_config)
_update_task_status(task_id, "success")
await _update_task_status(task_id, "success")
logger.info("定时任务执行成功: %s -> %s", task_id, exec_result)
except Exception as e:
error_msg = str(e)
_update_task_status(task_id, "error", error_msg)
await _update_task_status(task_id, "error", error_msg)
logger.exception("定时任务执行失败: %s", task_id)
run_async(_run())