1
This commit is contained in:
@@ -31,21 +31,17 @@ from app.tasks.celery_app import celery_app
|
|||||||
logger = logging.getLogger("video_gen")
|
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 with async_session() as db:
|
||||||
async def _do():
|
result = await db.execute(select(ScheduledTask).where(ScheduledTask.id == task_id))
|
||||||
async with async_session() as db:
|
task = result.scalar_one_or_none()
|
||||||
result = await db.execute(select(ScheduledTask).where(ScheduledTask.id == task_id))
|
if task is None:
|
||||||
task = result.scalar_one_or_none()
|
return
|
||||||
if task is None:
|
task.last_run_at = datetime.now(timezone.utc).isoformat()
|
||||||
return
|
task.last_status = status
|
||||||
task.last_run_at = datetime.now(timezone.utc).isoformat()
|
task.last_error = error_msg
|
||||||
task.last_status = status
|
await db.commit()
|
||||||
task.last_error = error_msg
|
|
||||||
await db.commit()
|
|
||||||
|
|
||||||
run_async(_do())
|
|
||||||
|
|
||||||
|
|
||||||
def _execute_internal_method(config: str | None) -> dict:
|
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:
|
if not task.is_active:
|
||||||
logger.info("定时任务已禁用,跳过执行: %s", task_id)
|
logger.info("定时任务已禁用,跳过执行: %s", task_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
task_config = task.config
|
task_config = task.config
|
||||||
|
|
||||||
try:
|
try:
|
||||||
exec_result = _execute_internal_method(task_config)
|
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)
|
logger.info("定时任务执行成功: %s -> %s", task_id, exec_result)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_msg = str(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)
|
logger.exception("定时任务执行失败: %s", task_id)
|
||||||
|
|
||||||
run_async(_run())
|
run_async(_run())
|
||||||
|
|||||||
Reference in New Issue
Block a user