""" Alert Processor Service v2.0 Main FastAPI application with RabbitMQ consumer lifecycle management. """ import structlog from app.core.config import settings from app.consumer.event_consumer import EventConsumer from app.api import alerts, sse from shared.redis_utils import initialize_redis, close_redis from shared.service_base import StandardFastAPIService # Initialize logger logger = structlog.get_logger() # Global consumer instance consumer: EventConsumer = None class AlertProcessorService(StandardFastAPIService): """Alert Processor Service with standardized monitoring setup and RabbitMQ consumer""" async def on_startup(self, app): """Custom startup logic for Alert Processor""" global consumer # Initialize Redis connection await initialize_redis( settings.REDIS_URL, db=settings.REDIS_DB, max_connections=settings.REDIS_MAX_CONNECTIONS ) logger.info("redis_initialized") # Start RabbitMQ consumer consumer = EventConsumer() await consumer.start() logger.info("rabbitmq_consumer_started") await super().on_startup(app) async def on_shutdown(self, app): """Custom shutdown logic for Alert Processor""" global consumer await super().on_shutdown(app) # Stop RabbitMQ consumer if consumer: await consumer.stop() logger.info("rabbitmq_consumer_stopped") # Close Redis await close_redis() logger.info("redis_closed") # Create service instance service = AlertProcessorService( service_name="alert-processor", app_name="Alert Processor Service", description="Event processing, enrichment, and alert management system", version=settings.VERSION, log_level=getattr(settings, 'LOG_LEVEL', 'INFO'), cors_origins=["*"], # Configure appropriately for production api_prefix="/api/v1", enable_metrics=True, enable_health_checks=True, enable_tracing=True, enable_cors=True ) # Create FastAPI app app = service.create_app(debug=settings.DEBUG) # Add service-specific routers app.include_router( alerts.router, prefix="/api/v1/tenants/{tenant_id}", tags=["alerts"] ) app.include_router( sse.router, prefix="/api/v1", tags=["sse"] ) if __name__ == "__main__": import uvicorn uvicorn.run( "app.main:app", host="0.0.0.0", port=8000, reload=settings.DEBUG )