Improved database connection and logging:
Robust ping/reconnect: get_db() now retries ping multiple times and safely recreates the pool once per request. Healthcheck thread: background daemon periodically pings connections and recreates the pool if needed. Logging improvements: early pool creation and healthcheck logs appear via proper logger setup.
This commit is contained in:
1 parent
cd759f43a8
commit
7b0ba7de10
5 files changed
+121
-24
No files matched your search
+7
-3
@@ -1,15 +1,16 @@
|
||||
from fastapi import FastAPI, Depends, HTTPException, Response, Request
|
||||
import uvicorn
|
||||
from contextlib import asynccontextmanager
|
||||
from db import get_db, create_connection_pool, close_connection_pool
|
||||
import logging
|
||||
from db import get_db, create_connection_pool, close_connection_pool, start_healthcheck_thread
|
||||
from logger_handler import setup_logger
|
||||
from feed_handler import grab_latest_chapter_information
|
||||
from send_notification import send_notification
|
||||
from metrics_server import REQUEST_COUNTER
|
||||
import asyncio
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger = setup_logger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
@@ -18,6 +19,9 @@ async def lifespan(app: FastAPI):
|
||||
logger.info("Creating MySQL connection pool...")
|
||||
create_connection_pool()
|
||||
|
||||
start_healthcheck_thread()
|
||||
logger.info("MySQL healthcheck thread started.")
|
||||
|
||||
yield
|
||||
logger.info("Closing MySQL connection pool...")
|
||||
close_connection_pool()
|
||||
|
||||
Reference in new issue
Block a user