Files
bakery-ia/services/notification/app/main.py

241 lines
8.9 KiB
Python
Raw Normal View History

2025-07-21 22:44:11 +02:00
# ================================================================
2025-08-23 10:19:58 +02:00
# services/notification/app/main.py - ENHANCED WITH SSE SUPPORT
2025-07-21 22:44:11 +02:00
# ================================================================
"""
2025-07-21 22:44:11 +02:00
Notification Service Main Application
2025-08-23 10:19:58 +02:00
Handles email, WhatsApp notifications and SSE for real-time alerts/recommendations
"""
2025-07-18 14:41:39 +02:00
import structlog
2025-07-21 22:44:11 +02:00
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
2025-07-21 22:44:11 +02:00
from fastapi.responses import JSONResponse
from app.core.config import settings
2025-07-21 22:44:11 +02:00
from app.core.database import init_db
from app.api.notifications import router as notification_router
2025-08-23 10:19:58 +02:00
from app.api.sse_routes import router as sse_router
2025-07-21 22:44:11 +02:00
from app.services.messaging import setup_messaging, cleanup_messaging
2025-08-23 10:19:58 +02:00
from app.services.sse_service import SSEService
from app.services.notification_orchestrator import NotificationOrchestrator
from app.services.email_service import EmailService
from app.services.whatsapp_service import WhatsAppService
2025-07-21 22:44:11 +02:00
from shared.monitoring import setup_logging, HealthChecker
from shared.monitoring.metrics import setup_metrics_early
2025-07-21 22:44:11 +02:00
# Setup logging first
setup_logging("notification-service", settings.LOG_LEVEL)
2025-07-18 14:41:39 +02:00
logger = structlog.get_logger()
2025-07-21 22:44:11 +02:00
# Global variables for lifespan access
metrics_collector = None
health_checker = None
# Create FastAPI app FIRST
app = FastAPI(
2025-07-21 22:44:11 +02:00
title="Bakery Notification Service",
2025-08-23 10:19:58 +02:00
description="Email, WhatsApp and SSE notification service for bakery alerts and recommendations",
version="2.0.0",
2025-07-21 22:44:11 +02:00
docs_url="/docs",
redoc_url="/redoc"
)
2025-07-21 22:44:11 +02:00
# Setup metrics BEFORE any middleware and BEFORE lifespan
metrics_collector = setup_metrics_early(app, "notification-service")
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan events - NO MIDDLEWARE ADDED HERE"""
global health_checker
# Startup
logger.info("Starting Notification Service...")
try:
# Initialize database
await init_db()
logger.info("Database initialized")
# Setup messaging
await setup_messaging()
logger.info("Messaging initialized")
2025-08-23 10:19:58 +02:00
# Initialize services
email_service = EmailService()
whatsapp_service = WhatsAppService()
# Initialize SSE service
sse_service = SSEService(settings.REDIS_URL)
await sse_service.initialize()
logger.info("SSE service initialized")
# Create orchestrator
orchestrator = NotificationOrchestrator(
email_service=email_service,
whatsapp_service=whatsapp_service,
sse_service=sse_service
)
# Store services in app state
app.state.orchestrator = orchestrator
app.state.sse_service = sse_service
app.state.email_service = email_service
app.state.whatsapp_service = whatsapp_service
2025-07-21 22:44:11 +02:00
# Register custom metrics (metrics_collector already exists)
2025-08-23 10:19:58 +02:00
metrics_collector.register_counter("notifications_sent_total", "Total notifications sent", labels=["type", "status", "channel"])
2025-07-21 22:44:11 +02:00
metrics_collector.register_counter("emails_sent_total", "Total emails sent", labels=["status"])
metrics_collector.register_counter("whatsapp_sent_total", "Total WhatsApp messages sent", labels=["status"])
2025-08-23 10:19:58 +02:00
metrics_collector.register_counter("sse_events_sent_total", "Total SSE events sent", labels=["tenant", "event_type"])
2025-07-21 22:44:11 +02:00
metrics_collector.register_histogram("notification_processing_duration_seconds", "Time spent processing notifications")
metrics_collector.register_gauge("notification_queue_size", "Current notification queue size")
2025-08-23 10:19:58 +02:00
metrics_collector.register_gauge("sse_active_connections", "Number of active SSE connections")
2025-07-21 22:44:11 +02:00
# Setup health checker
health_checker = HealthChecker("notification-service")
# Add database health check
async def check_database():
try:
from app.core.database import get_db
2025-08-08 09:08:41 +02:00
from sqlalchemy import text
2025-07-21 22:44:11 +02:00
async for db in get_db():
2025-08-08 09:08:41 +02:00
await db.execute(text("SELECT 1"))
2025-07-21 22:44:11 +02:00
return True
except Exception as e:
return f"Database error: {e}"
health_checker.add_check("database", check_database, timeout=5.0, critical=True)
# Add email service health check
async def check_email_service():
try:
from app.services.email_service import EmailService
email_service = EmailService()
return await email_service.health_check()
except Exception as e:
return f"Email service error: {e}"
health_checker.add_check("email_service", check_email_service, timeout=10.0, critical=True)
# Add WhatsApp service health check
async def check_whatsapp_service():
try:
return await whatsapp_service.health_check()
except Exception as e:
return f"WhatsApp service error: {e}"
health_checker.add_check("whatsapp_service", check_whatsapp_service, timeout=10.0, critical=False)
2025-08-23 10:19:58 +02:00
# Add SSE service health check
async def check_sse_service():
try:
metrics = sse_service.get_metrics()
return "healthy" if metrics["redis_connected"] else "Redis connection failed"
except Exception as e:
return f"SSE service error: {e}"
health_checker.add_check("sse_service", check_sse_service, timeout=5.0, critical=True)
2025-07-21 22:44:11 +02:00
# Add messaging health check
def check_messaging():
try:
# Check if messaging is properly initialized
from app.services.messaging import notification_publisher
return notification_publisher.connected if notification_publisher else False
except Exception as e:
return f"Messaging error: {e}"
health_checker.add_check("messaging", check_messaging, timeout=3.0, critical=False)
# Store health checker in app state
app.state.health_checker = health_checker
2025-08-23 10:19:58 +02:00
logger.info("Notification Service with SSE support started successfully")
2025-07-21 22:44:11 +02:00
except Exception as e:
logger.error(f"Failed to start Notification Service: {e}")
raise
yield
# Shutdown
logger.info("Shutting down Notification Service...")
try:
2025-08-23 10:19:58 +02:00
# Shutdown SSE service
if hasattr(app.state, 'sse_service'):
await app.state.sse_service.shutdown()
logger.info("SSE service shutdown completed")
2025-07-21 22:44:11 +02:00
await cleanup_messaging()
logger.info("Messaging cleanup completed")
except Exception as e:
2025-08-23 10:19:58 +02:00
logger.error(f"Error during shutdown: {e}")
2025-07-21 22:44:11 +02:00
# Set lifespan AFTER metrics setup
app.router.lifespan_context = lifespan
2025-07-21 22:44:11 +02:00
# CORS middleware (added after metrics setup)
app.add_middleware(
CORSMiddleware,
2025-07-21 22:44:11 +02:00
allow_origins=getattr(settings, 'CORS_ORIGINS', ["*"]),
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
2025-07-21 22:44:11 +02:00
# Include routers
app.include_router(notification_router, prefix="/api/v1", tags=["notifications"])
2025-08-23 10:19:58 +02:00
app.include_router(sse_router, prefix="/api/v1", tags=["sse"])
2025-07-21 22:44:11 +02:00
# Health check endpoint
@app.get("/health")
async def health_check():
2025-08-23 10:19:58 +02:00
"""Comprehensive health check endpoint including SSE"""
2025-07-21 22:44:11 +02:00
if health_checker:
2025-08-23 10:19:58 +02:00
health_result = await health_checker.check_health()
# Add SSE metrics to health check
if hasattr(app.state, 'sse_service'):
try:
sse_metrics = app.state.sse_service.get_metrics()
health_result['sse_metrics'] = sse_metrics
except Exception as e:
health_result['sse_error'] = str(e)
return health_result
2025-07-21 22:44:11 +02:00
else:
return {
"service": "notification-service",
"status": "healthy",
2025-08-23 10:19:58 +02:00
"version": "2.0.0",
"features": ["email", "whatsapp", "sse", "alerts", "recommendations"]
2025-07-21 22:44:11 +02:00
}
# Metrics endpoint
@app.get("/metrics")
async def metrics():
"""Prometheus metrics endpoint"""
if metrics_collector:
2025-07-29 17:24:56 +02:00
return metrics_collector.get_metrics()
2025-07-21 22:44:11 +02:00
return {"metrics": "not_available"}
# Exception handlers
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
"""Global exception handler with metrics"""
logger.error(f"Unhandled exception: {exc}", exc_info=True)
# Record error metric if available
if metrics_collector:
metrics_collector.increment_counter("errors_total", labels={"type": "unhandled"})
return JSONResponse(
status_code=500,
content={"detail": "Internal server error"}
)
if __name__ == "__main__":
import uvicorn
2025-07-21 22:44:11 +02:00
uvicorn.run(app, host="0.0.0.0", port=8000)