2025-08-23 10:19:58 +02:00
|
|
|
"""
|
2025-12-05 20:07:01 +01:00
|
|
|
Alert Processor Service v2.0
|
|
|
|
|
|
|
|
|
|
Main FastAPI application with RabbitMQ consumer lifecycle management.
|
2025-08-23 10:19:58 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import structlog
|
2025-11-27 15:52:40 +01:00
|
|
|
|
2025-12-05 20:07:01 +01:00
|
|
|
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
|
2026-01-09 23:14:12 +01:00
|
|
|
from shared.service_base import StandardFastAPIService
|
2025-11-27 15:52:40 +01:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
# Initialize logger
|
|
|
|
|
logger = structlog.get_logger()
|
2025-08-23 10:19:58 +02:00
|
|
|
|
2025-12-05 20:07:01 +01:00
|
|
|
# Global consumer instance
|
|
|
|
|
consumer: EventConsumer = None
|
2025-08-23 10:19:58 +02:00
|
|
|
|
|
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
class AlertProcessorService(StandardFastAPIService):
|
|
|
|
|
"""Alert Processor Service with standardized monitoring setup and RabbitMQ consumer"""
|
2025-11-27 15:52:40 +01:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
async def on_startup(self, app):
|
|
|
|
|
"""Custom startup logic for Alert Processor"""
|
|
|
|
|
global consumer
|
2025-11-27 15:52:40 +01:00
|
|
|
|
2025-12-05 20:07:01 +01:00
|
|
|
# Initialize Redis connection
|
|
|
|
|
await initialize_redis(
|
|
|
|
|
settings.REDIS_URL,
|
|
|
|
|
db=settings.REDIS_DB,
|
|
|
|
|
max_connections=settings.REDIS_MAX_CONNECTIONS
|
2025-08-23 10:19:58 +02:00
|
|
|
)
|
2025-12-05 20:07:01 +01:00
|
|
|
logger.info("redis_initialized")
|
2025-11-27 15:52:40 +01:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
# Start RabbitMQ consumer
|
2025-12-05 20:07:01 +01:00
|
|
|
consumer = EventConsumer()
|
|
|
|
|
await consumer.start()
|
2026-01-09 23:14:12 +01:00
|
|
|
logger.info("rabbitmq_consumer_started")
|
2026-01-08 12:58:00 +01:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
await super().on_startup(app)
|
2026-01-08 20:48:24 +01:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
async def on_shutdown(self, app):
|
|
|
|
|
"""Custom shutdown logic for Alert Processor"""
|
|
|
|
|
global consumer
|
2025-11-27 15:52:40 +01:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
await super().on_shutdown(app)
|
2025-11-27 15:52:40 +01:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
# Stop RabbitMQ consumer
|
2025-12-05 20:07:01 +01:00
|
|
|
if consumer:
|
|
|
|
|
await consumer.stop()
|
2026-01-09 23:14:12 +01:00
|
|
|
logger.info("rabbitmq_consumer_stopped")
|
|
|
|
|
|
|
|
|
|
# Close Redis
|
2025-12-05 20:07:01 +01:00
|
|
|
await close_redis()
|
2026-01-09 23:14:12 +01:00
|
|
|
logger.info("redis_closed")
|
2025-11-27 15:52:40 +01:00
|
|
|
|
2025-10-15 16:12:49 +02:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
# Create service instance
|
|
|
|
|
service = AlertProcessorService(
|
|
|
|
|
service_name="alert-processor",
|
|
|
|
|
app_name="Alert Processor Service",
|
2025-12-05 20:07:01 +01:00
|
|
|
description="Event processing, enrichment, and alert management system",
|
|
|
|
|
version=settings.VERSION,
|
2026-01-09 23:14:12 +01:00
|
|
|
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
|
2025-12-05 20:07:01 +01:00
|
|
|
)
|
2025-10-15 16:12:49 +02:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
# Create FastAPI app
|
|
|
|
|
app = service.create_app(debug=settings.DEBUG)
|
2025-10-15 16:12:49 +02:00
|
|
|
|
2026-01-09 23:14:12 +01:00
|
|
|
# Add service-specific routers
|
2025-12-05 20:07:01 +01:00
|
|
|
app.include_router(
|
|
|
|
|
alerts.router,
|
|
|
|
|
prefix="/api/v1/tenants/{tenant_id}",
|
|
|
|
|
tags=["alerts"]
|
|
|
|
|
)
|
2025-10-15 16:12:49 +02:00
|
|
|
|
2025-12-05 20:07:01 +01:00
|
|
|
app.include_router(
|
|
|
|
|
sse.router,
|
|
|
|
|
prefix="/api/v1",
|
|
|
|
|
tags=["sse"]
|
|
|
|
|
)
|
2025-08-23 10:19:58 +02:00
|
|
|
|
2025-11-27 15:52:40 +01:00
|
|
|
|
2025-08-23 10:19:58 +02:00
|
|
|
if __name__ == "__main__":
|
2025-12-05 20:07:01 +01:00
|
|
|
import uvicorn
|
|
|
|
|
|
|
|
|
|
uvicorn.run(
|
|
|
|
|
"app.main:app",
|
|
|
|
|
host="0.0.0.0",
|
|
|
|
|
port=8000,
|
|
|
|
|
reload=settings.DEBUG
|
|
|
|
|
)
|