改进手动搜索任务投递
This commit is contained in:
+16
-4
@@ -1,8 +1,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Literal, Optional
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -45,17 +46,28 @@ def list_runs(db: Session = Depends(get_db)) -> list[AgentRun]:
|
||||
|
||||
@router.post("/agent/runs/daily", response_model=RunTriggerRead)
|
||||
def trigger_daily_run(
|
||||
background_tasks: BackgroundTasks,
|
||||
request: Request,
|
||||
settings: Settings = Depends(get_settings),
|
||||
db: Session = Depends(get_db),
|
||||
) -> RunTriggerRead:
|
||||
# 手动搜索复用定时任务入口,避免测试按钮和自动调度走出两套采集逻辑。
|
||||
# 手动搜索投递到调度器线程,避免HTTP请求承载长时间Grok搜索任务。
|
||||
running = db.scalar(
|
||||
select(AgentRun).where(AgentRun.status == "running").order_by(AgentRun.id.desc()).limit(1)
|
||||
)
|
||||
if running is not None:
|
||||
return RunTriggerRead(status="running", run_id=running.id)
|
||||
background_tasks.add_task(run_scheduled_job, settings)
|
||||
scheduler = getattr(request.app.state, "scheduler", None)
|
||||
if scheduler is not None and scheduler.running:
|
||||
scheduler.add_job(
|
||||
run_scheduled_job,
|
||||
trigger="date",
|
||||
run_date=datetime.now(),
|
||||
args=[settings],
|
||||
id=f"manual-intelligence-{datetime.now().timestamp()}",
|
||||
max_instances=1,
|
||||
)
|
||||
return RunTriggerRead(status="started")
|
||||
run_scheduled_job(settings)
|
||||
return RunTriggerRead(status="started")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user