refactor(fc2c-i): sweep blueprints + ml tasks onto shared get_session (covers tasks/ml.py, a survey gap)
This commit is contained in:
@@ -5,22 +5,11 @@ from datetime import UTC, datetime, timedelta
|
||||
from quart import Blueprint, jsonify, request
|
||||
from sqlalchemy import delete, select, update
|
||||
|
||||
from ..extensions import make_engine, make_session_factory
|
||||
from ..extensions import get_session
|
||||
from ..models import ImportBatch, ImportTask
|
||||
|
||||
import_admin_bp = Blueprint("import_admin", __name__, url_prefix="/api/import")
|
||||
|
||||
_engine = None
|
||||
_Session = None
|
||||
|
||||
|
||||
def _session_factory():
|
||||
global _engine, _Session
|
||||
if _engine is None:
|
||||
_engine = make_engine()
|
||||
_Session = make_session_factory(_engine)
|
||||
return _Session
|
||||
|
||||
|
||||
@import_admin_bp.route("/trigger", methods=["POST"])
|
||||
async def trigger_scan():
|
||||
@@ -39,8 +28,7 @@ async def trigger_scan():
|
||||
|
||||
@import_admin_bp.route("/status", methods=["GET"])
|
||||
async def status():
|
||||
Session = _session_factory()
|
||||
async with Session() as session:
|
||||
async with get_session() as session:
|
||||
active = (
|
||||
await session.execute(
|
||||
select(ImportBatch)
|
||||
@@ -72,8 +60,7 @@ async def list_tasks():
|
||||
cursor_raw = request.args.get("cursor")
|
||||
cursor_id = int(cursor_raw) if cursor_raw else None
|
||||
|
||||
Session = _session_factory()
|
||||
async with Session() as session:
|
||||
async with get_session() as session:
|
||||
stmt = select(ImportTask).order_by(ImportTask.created_at.desc(), ImportTask.id.desc())
|
||||
if status_filter:
|
||||
stmt = stmt.where(ImportTask.status == status_filter)
|
||||
@@ -107,8 +94,7 @@ async def list_tasks():
|
||||
|
||||
@import_admin_bp.route("/retry-failed", methods=["POST"])
|
||||
async def retry_failed():
|
||||
Session = _session_factory()
|
||||
async with Session() as session:
|
||||
async with get_session() as session:
|
||||
failed_ids = (
|
||||
await session.execute(select(ImportTask.id).where(ImportTask.status == "failed"))
|
||||
).scalars().all()
|
||||
@@ -140,8 +126,7 @@ async def clear_completed():
|
||||
datetime.now(UTC) - timedelta(days=int(age_days)) if age_days else None
|
||||
)
|
||||
|
||||
Session = _session_factory()
|
||||
async with Session() as session:
|
||||
async with get_session() as session:
|
||||
stmt = delete(ImportTask).where(ImportTask.status.in_(status_filter))
|
||||
if cutoff is not None:
|
||||
stmt = stmt.where(ImportTask.finished_at < cutoff)
|
||||
|
||||
Reference in New Issue
Block a user