Fix discovery refresh: open fresh DB session in background task

The refresh endpoint was passing the request's db session to the
background task, which is closed before the task runs — silently
doing nothing on every refresh.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 01:01:18 +02:00
parent 1ee6edcb17
commit 4a7f1f06ac

View File

@@ -163,7 +163,17 @@ def refresh_discovery(
s = db.query(UserSettings).filter_by(user_id=current_user.id).first() s = db.query(UserSettings).filter_by(user_id=current_user.id).first()
regions_str = (s.discovery_regions if s and s.discovery_regions else "US,SE") regions_str = (s.discovery_regions if s and s.discovery_regions else "US,SE")
regions = [r.strip().upper() for r in regions_str.split(",") if r.strip()] regions = [r.strip().upper() for r in regions_str.split(",") if r.strip()]
background_tasks.add_task(run_full_discovery, db, current_user.id, regions) user_id = current_user.id
def _run_discovery():
from ..database import SessionLocal
fresh_db = SessionLocal()
try:
run_full_discovery(fresh_db, user_id, regions)
finally:
fresh_db.close()
background_tasks.add_task(_run_discovery)
from .channels import _enrich_missing_task from .channels import _enrich_missing_task
background_tasks.add_task(_enrich_missing_task, 20) background_tasks.add_task(_enrich_missing_task, 20)
return {"detail": "Discovery refresh started"} return {"detail": "Discovery refresh started"}