fix: correct subprocess script path and add TaskLog for trends trigger

This commit is contained in:
Yuzhiran Dev
2026-05-26 11:32:34 +08:00
parent ac8644d752
commit f94e6b0161
+13 -6
View File
@@ -235,12 +235,12 @@ def trigger_metrics_sync():
def trigger_refresh_search_cache(db: Session = Depends(get_db), current_user=Depends(get_current_user)): def trigger_refresh_search_cache(db: Session = Depends(get_db), current_user=Depends(get_current_user)):
try: try:
import sys as sys_mod import sys as sys_mod
scripts_dir = Path(__file__).parent.parent.parent.parent / "scripts" scripts_dir = PROJECT_ROOT / "scripts"
from ..database import SessionLocal as _ss from ..database import SessionLocal as _ss
proc = subprocess.Popen( proc = subprocess.Popen(
[sys_mod.executable, str(scripts_dir / "opencode_search.py"), "--refresh-cache"], [sys_mod.executable, str(scripts_dir / "opencode_search.py"), "--refresh-cache"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
cwd=scripts_dir.parent.parent cwd=str(PROJECT_ROOT)
) )
logger.info("Search cache refresh started (pid=%s)", proc.pid) logger.info("Search cache refresh started (pid=%s)", proc.pid)
log = TaskLog(module_id="scheduled_refresh_search_cache", task_name="🔍 搜索缓存", status="running", message="搜索缓存刷新已启动", triggered_by="manual", started_at=datetime.now(timezone.utc), result_data={"pid": proc.pid}) log = TaskLog(module_id="scheduled_refresh_search_cache", task_name="🔍 搜索缓存", status="running", message="搜索缓存刷新已启动", triggered_by="manual", started_at=datetime.now(timezone.utc), result_data={"pid": proc.pid})
@@ -254,17 +254,24 @@ def trigger_refresh_search_cache(db: Session = Depends(get_db), current_user=Dep
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))
@router.post("/trends/run") @router.post("/trends/run")
def trigger_trends_refresh(): def trigger_trends_refresh(db: Session = Depends(get_db), current_user=Depends(get_current_user)):
try: try:
import sys as sys_mod import sys as sys_mod
scripts_dir = Path(__file__).parent.parent.parent.parent / "scripts" scripts_dir = PROJECT_ROOT / "scripts"
from ..database import SessionLocal as _ss
proc = subprocess.Popen( proc = subprocess.Popen(
[sys_mod.executable, str(scripts_dir / "trends.py")], [sys_mod.executable, str(scripts_dir / "trends.py")],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
cwd=scripts_dir.parent.parent cwd=str(PROJECT_ROOT)
) )
logger.info("Trends refresh started (pid=%s)", proc.pid) logger.info("Trends refresh started (pid=%s)", proc.pid)
return {"message": "热点趋势刷新已后台启动", "pid": proc.pid} log = TaskLog(module_id="scheduled_fetch_trends", task_name="🔥 热点趋势", status="running", message="热点趋势刷新已启动", triggered_by="manual", started_at=datetime.now(timezone.utc), result_data={"pid": proc.pid})
db.add(log)
db.commit()
log_id = log.id
t = threading.Thread(target=_monitor_subprocess, args=(log_id, proc, "scheduled_fetch_trends", "🔥 热点趋势", _ss), daemon=True)
t.start()
return {"message": "热点趋势刷新已后台启动", "pid": proc.pid, "log_id": log_id}
except Exception as e: except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))