"""Async retouch processing background task. Creates its own DB session independent of the request session so the HTTP response can return immediately while processing continues. """ from __future__ import annotations import logging import uuid from typing import Any from app.database import async_session_factory from app.services.retouch_service import process_retouch logger = logging.getLogger(__name__) async def run_retouch_processing( result_id: uuid.UUID, vehicle_info: dict[str, Any] | None = None, ) -> None: """Background task: process retouch result asynchronously. Creates its own DB session (independent of the request session) so the HTTP response can return immediately. """ async with async_session_factory() as session: try: await process_retouch(session, result_id, vehicle_info) await session.commit() logger.info("Retouch processing completed for result %s", result_id) except Exception as exc: await session.rollback() logger.error("Retouch background task failed for %s: %s", result_id, exc) raise finally: await session.close()