46 lines
1.9 KiB
Python
46 lines
1.9 KiB
Python
"""
|
|
backend/app/jobs/handlers/file_check_run.py
|
|
|
|
用途:文件检核异步执行 handler。
|
|
依赖:schedule.scheduler_service.run_task_now、jobs.queue.finish_job。
|
|
逻辑:schedule worker 领取 FILE_CHECK_RUN 后,按 payload 中的 task_id / schedule_run_id 执行已创建的 ScheduleRun。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from app import db
|
|
from app.jobs.context import JobContext
|
|
from app.jobs.queue import finish_job
|
|
from app.jobs.registry import register_handler
|
|
from app.jobs.types import JobType
|
|
from app.models import ScheduleRun, ScheduleTask
|
|
from app.services.schedule.scheduler_service import run_task_now
|
|
|
|
|
|
@register_handler(JobType.FILE_CHECK_RUN)
|
|
def run_file_check(ctx: JobContext, payload: dict) -> None:
|
|
"""
|
|
执行已入队的文件检核 ScheduleRun。
|
|
|
|
核心逻辑:读取 task/run → 调用 run_task_now(与原 API 后台线程同一入口)→ finish_job。
|
|
参数:
|
|
ctx: Worker 上下文。
|
|
payload: 必须包含 task_id 与 schedule_run_id;可选 job_id。
|
|
异常:
|
|
RuntimeError: payload 缺失或任务/运行记录不存在。
|
|
"""
|
|
task_id = int(payload.get("task_id") or 0)
|
|
run_id = int(payload.get("schedule_run_id") or 0)
|
|
job_id = int(payload.get("job_id") or 0)
|
|
if not task_id or not run_id:
|
|
raise RuntimeError("task_id and schedule_run_id are required")
|
|
|
|
task = db.session.get(ScheduleTask, task_id)
|
|
run = db.session.get(ScheduleRun, run_id)
|
|
if not task or not run:
|
|
raise RuntimeError(f"file-check task/run not found task_id={task_id} run_id={run_id}")
|
|
|
|
ctx.logger.info("file_check_run start job_id=%s task_id=%s run_id=%s", job_id, task_id, run_id)
|
|
run_task_now(task, trigger_type="dify_chatflow", triggered_by_user_id=None, run=run)
|
|
if job_id:
|
|
finish_job(job_id, result={"task_id": task_id, "schedule_run_id": run_id}, message="文件检核执行完成")
|