Spaces:
Runtime error
Runtime error
| import logging | |
| from typing import Optional, Any, Dict | |
| import json | |
| from app.core.config import get_settings | |
| logger = logging.getLogger(__name__) | |
| class QueueService: | |
| """ | |
| Queue service for managing asynchronous analysis tasks. | |
| Currently uses in-memory queue, can be extended to use Redis. | |
| """ | |
| def __init__(self): | |
| """Initialize the queue service.""" | |
| self.settings = get_settings() | |
| self.redis_client = None | |
| if self.settings.REDIS_ENABLED: | |
| self._initialize_redis() | |
| def _initialize_redis(self): | |
| """Initialize Redis connection (future implementation).""" | |
| # This will be implemented when Redis support is added | |
| logger.info( | |
| f"Redis queue service initialized: {self.settings.REDIS_URL}" | |
| ) | |
| async def enqueue_analysis( | |
| self, | |
| file_url: str, | |
| model: str, | |
| task_id: str, | |
| ) -> bool: | |
| """ | |
| Enqueue an analysis task. | |
| Args: | |
| file_url: URL of the file to analyze | |
| model: Detector model to use | |
| task_id: Unique task identifier | |
| Returns: | |
| True if successful, False otherwise | |
| """ | |
| task_data = { | |
| "task_id": task_id, | |
| "file_url": file_url, | |
| "model": model, | |
| } | |
| logger.info(f"Enqueuing analysis task: {task_id}") | |
| if self.settings.REDIS_ENABLED: | |
| # Future: Push to Redis queue | |
| # await self.redis_client.lpush( | |
| # self.settings.REDIS_QUEUE_NAME, | |
| # json.dumps(task_data) | |
| # ) | |
| pass | |
| return True | |
| async def get_task_result(self, task_id: str) -> Optional[Dict[str, Any]]: | |
| """ | |
| Get analysis result for a task. | |
| Args: | |
| task_id: Task identifier | |
| Returns: | |
| Analysis result or None if not found | |
| """ | |
| logger.info(f"Retrieving result for task: {task_id}") | |
| if self.settings.REDIS_ENABLED: | |
| # Future: Get from Redis | |
| # result = await self.redis_client.get(f"result:{task_id}") | |
| # return json.loads(result) if result else None | |
| pass | |
| return None | |
| # Singleton instance | |
| _queue_service: Optional[QueueService] = None | |
| def get_queue_service() -> QueueService: | |
| """Get or create the queue service singleton.""" | |
| global _queue_service | |
| if _queue_service is None: | |
| _queue_service = QueueService() | |
| return _queue_service | |