Improved RabbitMQ handling
- Switched to async because it offers easy automatic reconnect features on connection failure - Adjusted notification path to reflect that change - Added in memory message queue if RMQ is not reachable
This commit is contained in:
1 parent
c0fcdaeb4f
commit
873e7e7854
5 files changed
+104
-120
No files matched your search
+18
-51
@@ -7,7 +7,7 @@ from pydantic import BaseModel
|
||||
from validator import verify_api_key
|
||||
from db import get_db, create_connection_pool, close_connection_pool, start_healthcheck_thread
|
||||
from logger_handler import setup_logger
|
||||
from rabbitmq_handler import send_message_to_rmq, create_connection, close_connection
|
||||
from rabbitmq_handler import RabbitMQProducer
|
||||
import uvicorn
|
||||
from uvicorn_logging_config import LOGGING_CONFIG
|
||||
from contextlib import asynccontextmanager
|
||||
@@ -15,7 +15,7 @@ from metrics_server import REQUEST_COUNTER
|
||||
import asyncio
|
||||
|
||||
logger = setup_logger(__name__)
|
||||
|
||||
producer = RabbitMQProducer()
|
||||
api_key_header_internal = APIKeyHeader(name="X-API-Key-Internal")
|
||||
|
||||
class Notification(BaseModel):
|
||||
@@ -30,24 +30,21 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
logger.info("Creating MySQL connection pool...")
|
||||
create_connection_pool()
|
||||
logger.info("Connecting to RabbitMQ...")
|
||||
app.state.rmq_connection = create_connection()
|
||||
|
||||
start_healthcheck_thread()
|
||||
logger.info("MySQL healthcheck thread started.")
|
||||
|
||||
logger.info("Starting RabbitMQ producer...")
|
||||
await producer.connect()
|
||||
app.state.rmq_producer = producer
|
||||
logger.info("[FastAPI] RabbitMQ producer initialized.")
|
||||
|
||||
yield
|
||||
logger.info("Closing RabbitMQ connection...")
|
||||
close_connection(app.state.rmq_connection)
|
||||
logger.info("Closing RabbitMQ producer...")
|
||||
await producer.close()
|
||||
|
||||
logger.info("Closing MySQL connection pool...")
|
||||
close_connection_pool()
|
||||
|
||||
def get_rmq_connection(app: FastAPI):
|
||||
connection = getattr(app.state, "rmq_connection", None)
|
||||
if not connection or not connection.is_open:
|
||||
app.state.rmq_connection = create_connection()
|
||||
return app.state.rmq_connection
|
||||
|
||||
api = FastAPI(
|
||||
title="Internal Notifier API",
|
||||
description="API to forward messages to RabbitMQ",
|
||||
@@ -87,54 +84,24 @@ async def custom_http_exception_handler(request,exc):
|
||||
content={"detail": exc.detail}
|
||||
)
|
||||
|
||||
@api.get("/health", tags=["Health"])
|
||||
def return_health(request:Request, db=Depends(get_db)):
|
||||
try:
|
||||
cursor = db.cursor()
|
||||
cursor.execute("SELECT 1")
|
||||
cursor.fetchone()
|
||||
db_status = "ok"
|
||||
except Exception as e:
|
||||
logger.error(f"Health check DB failed: {e}")
|
||||
db_status = "error"
|
||||
|
||||
try:
|
||||
rmq_conn = getattr(request.app.state, "rmq_connection", None)
|
||||
if not rmq_conn or not rmq_conn.is_open:
|
||||
logger.error("Health check RMQ failed: connection closed or missing")
|
||||
rmq_status = "error"
|
||||
except Exception as e:
|
||||
logger.error(f"Health check RMQ failed: {e}")
|
||||
rmq_status = "error"
|
||||
|
||||
overall_status = "ok" if db_status == "ok" and rmq_status == "ok" else "error"
|
||||
status_code = 200 if overall_status == "ok" else 500
|
||||
|
||||
return JSONResponse(
|
||||
status_code=status_code,
|
||||
content={"status": overall_status,
|
||||
"components": {
|
||||
"database": db_status,
|
||||
"rabbitmq": rmq_status
|
||||
},
|
||||
"message": "Service is running" if overall_status == "ok" else "One or more checks failed"}
|
||||
)
|
||||
|
||||
@api.post("/internal/receive-notifications")
|
||||
def receive_notifications(
|
||||
async def receive_notifications(
|
||||
notification_data: Notification,
|
||||
request: Request,
|
||||
db = Depends(get_db),
|
||||
program_name: str = Depends(verify_api_key_dependency_internal)
|
||||
):
|
||||
rmq_connection = get_rmq_connection(request.app)
|
||||
|
||||
logger.info(f"Received notifcation data from {program_name} for RMQ")
|
||||
send_message_to_rmq(rmq_connection,notification_data.receipent_user_id,notification_data.message)
|
||||
logger.info("Successfully delivered message to RMQ")
|
||||
await request.app.state.rmq_producer.publish(
|
||||
notification_data.receipent_user_id,
|
||||
notification_data.message
|
||||
)
|
||||
logger.info("Successfully queued for delivery to RMQ")
|
||||
return {"status": "queued"}
|
||||
|
||||
async def start_servers():
|
||||
config_main = uvicorn.Config("main:api", host="0.0.0.0", port=8101, log_config=LOGGING_CONFIG, log_level="info")
|
||||
config_main = uvicorn.Config("main:api", host="0.0.0.0", port=8101, log_level="info")
|
||||
config_metrics = uvicorn.Config("metrics_server:metrics_api", host="0.0.0.0", port=9000, log_level="info")
|
||||
|
||||
server_main = uvicorn.Server(config_main)
|
||||
|
||||
Reference in new issue
Block a user