Thumbnails generated and displayed in the front end
This commit is contained in:
@@ -4,7 +4,7 @@ MongoDB database connection management.
|
||||
This module handles MongoDB connection with fail-fast approach.
|
||||
The application will terminate if MongoDB is not accessible at startup.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
@@ -13,11 +13,14 @@ from pymongo.database import Database
|
||||
from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError
|
||||
|
||||
from app.config.settings import get_mongodb_url, get_mongodb_database_name
|
||||
from app.utils.security import safe_connection_string
|
||||
|
||||
# Global variables for singleton pattern
|
||||
_client: Optional[MongoClient] = None
|
||||
_database: Optional[Database] = None
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def create_mongodb_client() -> MongoClient:
|
||||
"""
|
||||
@@ -43,16 +46,16 @@ def create_mongodb_client() -> MongoClient:
|
||||
# Test connection by running admin command
|
||||
client.admin.command('ping')
|
||||
|
||||
print(f"Successfully connected to MongoDB at {mongodb_url}")
|
||||
logger.info(f"Successfully connected to MongoDB at {safe_connection_string(mongodb_url)}")
|
||||
return client
|
||||
|
||||
except (ConnectionFailure, ServerSelectionTimeoutError) as e:
|
||||
print(f"ERROR: Failed to connect to MongoDB at {mongodb_url}")
|
||||
print(f"Connection error: {str(e)}")
|
||||
print("MongoDB is required for this application. Please ensure MongoDB is running and accessible.")
|
||||
logger.error(f"ERROR: Failed to connect to MongoDB at {safe_connection_string(mongodb_url)}")
|
||||
logger.error(f"Connection error: {str(e)}")
|
||||
logger.error("MongoDB is required for this application. Please ensure MongoDB is running and accessible.")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"ERROR: Unexpected error connecting to MongoDB: {str(e)}")
|
||||
logger.error(f"ERROR: Unexpected error connecting to MongoDB: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
@@ -74,7 +77,7 @@ def get_database() -> Database:
|
||||
|
||||
database_name = get_mongodb_database_name()
|
||||
_database = _client[database_name]
|
||||
print(f"Connected to database: {database_name}")
|
||||
logger.info(f"Connected to database: {database_name}")
|
||||
|
||||
return _database
|
||||
|
||||
@@ -92,7 +95,7 @@ def close_database_connection():
|
||||
_client.close()
|
||||
_client = None
|
||||
_database = None
|
||||
print("MongoDB connection closed")
|
||||
logger.info("MongoDB connection closed")
|
||||
|
||||
|
||||
def get_mongodb_client() -> Optional[MongoClient]:
|
||||
|
||||
Reference in New Issue
Block a user