File size: 1,357 Bytes
3a7eb07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Celery Tasks
Background processing tasks for document analysis
"""

import asyncio
import logging

from app.workers.celery_app import celery_app

logger = logging.getLogger(__name__)


@celery_app.task(
    bind=True,
    name="app.workers.tasks.process_document_task",
    max_retries=3,
    default_retry_delay=30,
)
def process_document_task(self, document_id: str):
    """
    Celery task for processing a document with AI agents.
    Runs the async document processing pipeline in a sync context.

    Args:
        document_id: UUID string of the document to process
    """
    logger.info(f"Starting Celery task for document: {document_id}")

    try:
        # Run the async processing pipeline in a new event loop
        from app.services.document_service import DocumentService

        async def _run():
            service = DocumentService()
            return await service.process_document(document_id)

        result = asyncio.run(_run())

        if result:
            logger.info(f"Document processed successfully: {document_id}")
        else:
            logger.error(f"Document processing returned False: {document_id}")

        return {"document_id": document_id, "success": result}

    except Exception as exc:
        logger.error(f"Celery task failed for document {document_id}: {exc}")
        raise self.retry(exc=exc)