37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
"""Async OCR processing task using background tasks.
|
|
|
|
In production this would use Redis Queue (RQ) or Celery.
|
|
For now, we use FastAPI BackgroundTasks to trigger async processing.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import uuid
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.database import async_session_factory
|
|
from app.services.ocr_service import process_ocr
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def run_ocr_processing(result_id: uuid.UUID) -> None:
|
|
"""Background task: process OCR 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_ocr(session, result_id)
|
|
await session.commit()
|
|
logger.info("OCR processing completed for result %s", result_id)
|
|
except Exception as exc:
|
|
await session.rollback()
|
|
logger.error("OCR background task failed for %s: %s", result_id, exc)
|
|
raise
|
|
finally:
|
|
await session.close()
|