🐛 fix(dashboard): offload cron profile scans

This commit is contained in:
墨綠BG 2026-06-13 17:02:35 +08:00
parent b6c7ebf028
commit 1fded709aa
2 changed files with 101 additions and 18 deletions

View file

@ -6732,8 +6732,7 @@ def _find_cron_job_profile(job_id: str) -> Optional[str]:
return None
@app.get("/api/cron/jobs")
async def list_cron_jobs(profile: str = "all"):
def _list_cron_jobs_sync(profile: str = "all"):
requested = (profile or "all").strip()
if requested.lower() != "all":
return _call_cron_for_profile(requested, "list_jobs", True)
@ -6750,8 +6749,17 @@ async def list_cron_jobs(profile: str = "all"):
return jobs
@app.get("/api/cron/jobs/{job_id}")
async def get_cron_job(job_id: str, profile: Optional[str] = None):
async def _run_cron_dashboard_io(func, *args, **kwargs):
"""Run cron dashboard profile/job I/O outside the FastAPI event loop."""
return await asyncio.to_thread(func, *args, **kwargs)
@app.get("/api/cron/jobs")
async def list_cron_jobs(profile: str = "all"):
return await _run_cron_dashboard_io(_list_cron_jobs_sync, profile)
def _get_cron_job_sync(job_id: str, profile: Optional[str] = None):
selected = profile or _find_cron_job_profile(job_id)
if not selected:
raise HTTPException(status_code=404, detail="Job not found")
@ -6761,8 +6769,12 @@ async def get_cron_job(job_id: str, profile: Optional[str] = None):
return job
@app.get("/api/cron/jobs/{job_id}/runs")
async def list_cron_job_runs(job_id: str, profile: Optional[str] = None, limit: int = 20):
@app.get("/api/cron/jobs/{job_id}")
async def get_cron_job(job_id: str, profile: Optional[str] = None):
return await _run_cron_dashboard_io(_get_cron_job_sync, job_id, profile)
def _list_cron_job_runs_sync(job_id: str, profile: Optional[str] = None, limit: int = 20):
"""Run sessions produced by a cron job, newest first.
Cron runs are stored as ordinary sessions whose id is
@ -6807,8 +6819,12 @@ async def list_cron_job_runs(job_id: str, profile: Optional[str] = None, limit:
db.close()
@app.post("/api/cron/jobs")
async def create_cron_job(body: CronJobCreate, profile: str = "default"):
@app.get("/api/cron/jobs/{job_id}/runs")
async def list_cron_job_runs(job_id: str, profile: Optional[str] = None, limit: int = 20):
return await _run_cron_dashboard_io(_list_cron_job_runs_sync, job_id, profile, limit)
def _create_cron_job_sync(body: CronJobCreate, profile: str = "default"):
try:
return _call_cron_for_profile(
profile,
@ -6824,6 +6840,11 @@ async def create_cron_job(body: CronJobCreate, profile: str = "default"):
raise HTTPException(status_code=400, detail=str(e))
@app.post("/api/cron/jobs")
async def create_cron_job(body: CronJobCreate, profile: str = "default"):
return await _run_cron_dashboard_io(_create_cron_job_sync, body, profile)
@app.get("/api/cron/delivery-targets")
async def get_cron_delivery_targets():
"""Delivery targets the cron dropdown should offer.
@ -6852,8 +6873,7 @@ async def get_cron_delivery_targets():
return {"targets": targets}
@app.put("/api/cron/jobs/{job_id}")
async def update_cron_job(job_id: str, body: CronJobUpdate, profile: Optional[str] = None):
def _update_cron_job_sync(job_id: str, body: CronJobUpdate, profile: Optional[str] = None):
selected = profile or _find_cron_job_profile(job_id)
if not selected:
raise HTTPException(status_code=404, detail="Job not found")
@ -6866,8 +6886,12 @@ async def update_cron_job(job_id: str, body: CronJobUpdate, profile: Optional[st
return job
@app.post("/api/cron/jobs/{job_id}/pause")
async def pause_cron_job(job_id: str, profile: Optional[str] = None):
@app.put("/api/cron/jobs/{job_id}")
async def update_cron_job(job_id: str, body: CronJobUpdate, profile: Optional[str] = None):
return await _run_cron_dashboard_io(_update_cron_job_sync, job_id, body, profile)
def _pause_cron_job_sync(job_id: str, profile: Optional[str] = None):
selected = profile or _find_cron_job_profile(job_id)
if not selected:
raise HTTPException(status_code=404, detail="Job not found")
@ -6877,8 +6901,12 @@ async def pause_cron_job(job_id: str, profile: Optional[str] = None):
return job
@app.post("/api/cron/jobs/{job_id}/resume")
async def resume_cron_job(job_id: str, profile: Optional[str] = None):
@app.post("/api/cron/jobs/{job_id}/pause")
async def pause_cron_job(job_id: str, profile: Optional[str] = None):
return await _run_cron_dashboard_io(_pause_cron_job_sync, job_id, profile)
def _resume_cron_job_sync(job_id: str, profile: Optional[str] = None):
selected = profile or _find_cron_job_profile(job_id)
if not selected:
raise HTTPException(status_code=404, detail="Job not found")
@ -6888,8 +6916,12 @@ async def resume_cron_job(job_id: str, profile: Optional[str] = None):
return job
@app.post("/api/cron/jobs/{job_id}/trigger")
async def trigger_cron_job(job_id: str, profile: Optional[str] = None):
@app.post("/api/cron/jobs/{job_id}/resume")
async def resume_cron_job(job_id: str, profile: Optional[str] = None):
return await _run_cron_dashboard_io(_resume_cron_job_sync, job_id, profile)
def _trigger_cron_job_sync(job_id: str, profile: Optional[str] = None):
selected = profile or _find_cron_job_profile(job_id)
if not selected:
raise HTTPException(status_code=404, detail="Job not found")
@ -6899,8 +6931,12 @@ async def trigger_cron_job(job_id: str, profile: Optional[str] = None):
return job
@app.delete("/api/cron/jobs/{job_id}")
async def delete_cron_job(job_id: str, profile: Optional[str] = None):
@app.post("/api/cron/jobs/{job_id}/trigger")
async def trigger_cron_job(job_id: str, profile: Optional[str] = None):
return await _run_cron_dashboard_io(_trigger_cron_job_sync, job_id, profile)
def _delete_cron_job_sync(job_id: str, profile: Optional[str] = None):
selected = profile or _find_cron_job_profile(job_id)
if not selected:
raise HTTPException(status_code=404, detail="Job not found")
@ -6913,6 +6949,11 @@ async def delete_cron_job(job_id: str, profile: Optional[str] = None):
return {"ok": True}
@app.delete("/api/cron/jobs/{job_id}")
async def delete_cron_job(job_id: str, profile: Optional[str] = None):
return await _run_cron_dashboard_io(_delete_cron_job_sync, job_id, profile)
# ---------------------------------------------------------------------------
# Automation Blueprints — parameterized automation blueprints. The dashboard renders the
# slot schema as a form; submitting instantiates a real cron job via the same

View file

@ -1,5 +1,7 @@
"""Regression tests for dashboard cron job profile routing."""
import threading
import pytest
from fastapi import HTTPException
@ -131,6 +133,46 @@ async def test_cron_mutation_without_profile_finds_named_profile_job(isolated_pr
assert worker_jobs[0]["enabled"] is False
@pytest.mark.asyncio
async def test_cron_profile_scan_runs_off_event_loop(isolated_profiles, monkeypatch):
from hermes_cli import web_server
worker_job = web_server._call_cron_for_profile(
"worker_alpha",
"create_job",
prompt="managed by named profile",
schedule="every 1h",
name="thread-offload-job",
)
event_loop_thread = threading.get_ident()
profile_scan_threads = []
worker_threads = []
original_profile_dicts = web_server._cron_profile_dicts
original_find = web_server._find_cron_job_profile
def tracking_profile_dicts():
profile_scan_threads.append(threading.get_ident())
return original_profile_dicts()
def tracking_find(job_id):
worker_threads.append(threading.get_ident())
return original_find(job_id)
monkeypatch.setattr(web_server, "_cron_profile_dicts", tracking_profile_dicts)
monkeypatch.setattr(web_server, "_find_cron_job_profile", tracking_find)
jobs = await web_server.list_cron_jobs(profile="all")
paused = await web_server.pause_cron_job(worker_job["id"])
assert any(job["id"] == worker_job["id"] for job in jobs)
assert paused["profile"] == "worker_alpha"
assert profile_scan_threads
assert worker_threads
assert all(thread_id != event_loop_thread for thread_id in profile_scan_threads)
assert all(thread_id != event_loop_thread for thread_id in worker_threads)
@pytest.mark.asyncio
async def test_update_cron_job_rejects_id_mutation(isolated_profiles):
"""Dashboard surfaces a 400 (not a 500 or silent rename) when an