Spaces:
Sleeping
Sleeping
dhruv575 commited on
Commit ·
409211c
1
Parent(s): 341d7ef
Better logging
Browse files
db.py
CHANGED
|
@@ -1,41 +1,91 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
-
from pymongo import MongoClient
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 5 |
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
# Set up logging
|
| 10 |
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class Database:
|
| 13 |
_instance = None
|
| 14 |
|
| 15 |
@classmethod
|
| 16 |
def get_instance(cls):
|
|
|
|
| 17 |
if cls._instance is None:
|
|
|
|
| 18 |
cls._instance = cls()
|
|
|
|
|
|
|
| 19 |
return cls._instance
|
| 20 |
|
| 21 |
def __init__(self):
|
|
|
|
| 22 |
if Database._instance is not None:
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
self.mongo_uri = os.environ.get('MONGO_URI')
|
|
|
|
| 26 |
if not self.mongo_uri:
|
| 27 |
-
logger.error("MONGO_URI environment variable not set")
|
| 28 |
-
raise ValueError("MONGO_URI environment variable not set.
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
self.
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
-
logger.error(f"
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
def get_db(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return self.db
|
| 40 |
|
| 41 |
def close(self):
|
|
@@ -45,7 +95,10 @@ class Database:
|
|
| 45 |
|
| 46 |
# Helper functions to get collections
|
| 47 |
def get_collection(collection_name):
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
return db[collection_name]
|
| 50 |
|
| 51 |
def get_users_collection():
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
from pymongo import MongoClient, version as pymongo_version
|
| 4 |
+
from pymongo.errors import ConnectionFailure
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
import re
|
| 7 |
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
+
# Ensure logger has handlers if running this module standalone for tests
|
| 14 |
+
if not logger.hasHandlers():
|
| 15 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 16 |
+
console_handler = logging.StreamHandler()
|
| 17 |
+
console_handler.setLevel(logging.INFO)
|
| 18 |
+
console_handler.setFormatter(formatter)
|
| 19 |
+
logger.addHandler(console_handler)
|
| 20 |
+
logger.setLevel(logging.INFO)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def mask_mongo_uri(uri):
|
| 24 |
+
"""Masks the password in a MongoDB URI for logging."""
|
| 25 |
+
if not uri:
|
| 26 |
+
return "None"
|
| 27 |
+
# Regex to find username:password@ part
|
| 28 |
+
return re.sub(r"://([^:]+):([^@]+)@", r"://\1:********@", uri)
|
| 29 |
|
| 30 |
class Database:
|
| 31 |
_instance = None
|
| 32 |
|
| 33 |
@classmethod
|
| 34 |
def get_instance(cls):
|
| 35 |
+
logger.debug("Database.get_instance called.")
|
| 36 |
if cls._instance is None:
|
| 37 |
+
logger.info("Creating new Database instance.")
|
| 38 |
cls._instance = cls()
|
| 39 |
+
else:
|
| 40 |
+
logger.debug("Returning existing Database instance.")
|
| 41 |
return cls._instance
|
| 42 |
|
| 43 |
def __init__(self):
|
| 44 |
+
logger.info("Initializing Database singleton.")
|
| 45 |
if Database._instance is not None:
|
| 46 |
+
logger.warning("Database __init__ called on existing instance.")
|
| 47 |
+
# In a strict singleton, you might raise an exception,
|
| 48 |
+
# but for robustness, maybe just return?
|
| 49 |
+
# For now, we allow re-initialization check but proceed.
|
| 50 |
+
# raise Exception("This class is a singleton!")
|
| 51 |
+
pass # Let it proceed but log it
|
| 52 |
|
| 53 |
+
logger.info(f"Using PyMongo version: {pymongo_version}")
|
| 54 |
self.mongo_uri = os.environ.get('MONGO_URI')
|
| 55 |
+
|
| 56 |
if not self.mongo_uri:
|
| 57 |
+
logger.error("CRITICAL: MONGO_URI environment variable not set.")
|
| 58 |
+
raise ValueError("MONGO_URI environment variable not set. Cannot initialize Database.")
|
| 59 |
+
|
| 60 |
+
masked_uri = mask_mongo_uri(self.mongo_uri)
|
| 61 |
+
logger.info(f"Attempting to connect to MongoDB using URI: {masked_uri}")
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
# Explicitly set serverSelectionTimeoutMS (e.g., 10 seconds)
|
| 65 |
+
self.client = MongoClient(
|
| 66 |
+
self.mongo_uri,
|
| 67 |
+
serverSelectionTimeoutMS=10000 # Timeout in milliseconds
|
| 68 |
+
)
|
| 69 |
+
# The ismaster command is cheap and does not require auth.
|
| 70 |
+
logger.info("MongoClient initialized. Pinging server...")
|
| 71 |
+
self.client.admin.command('ismaster') # More specific ping/connection check
|
| 72 |
+
self.db = self.client['enflow'] # Select the database
|
| 73 |
+
logger.info("Successfully connected to MongoDB server and selected database.")
|
| 74 |
+
except ConnectionFailure as e:
|
| 75 |
+
logger.error(f"MongoDB Connection Failure: Could not connect to server. Error: {str(e)}")
|
| 76 |
+
# Log details that might help diagnose network/firewall issues
|
| 77 |
+
logger.error(f"Mongo URI used (masked): {masked_uri}")
|
| 78 |
+
raise # Re-raise the exception to halt app startup if needed
|
| 79 |
except Exception as e:
|
| 80 |
+
logger.error(f"An unexpected error occurred during MongoDB connection: {str(e)}", exc_info=True)
|
| 81 |
+
logger.error(f"Mongo URI used (masked): {masked_uri}")
|
| 82 |
+
raise # Re-raise the exception
|
| 83 |
|
| 84 |
def get_db(self):
|
| 85 |
+
if not hasattr(self, 'db'):
|
| 86 |
+
logger.error("Attempted to get DB instance before successful connection.")
|
| 87 |
+
# Depending on how you handle errors in __init__, this might indicate a startup failure
|
| 88 |
+
return None # Or raise an exception
|
| 89 |
return self.db
|
| 90 |
|
| 91 |
def close(self):
|
|
|
|
| 95 |
|
| 96 |
# Helper functions to get collections
|
| 97 |
def get_collection(collection_name):
|
| 98 |
+
db_instance = Database.get_instance()
|
| 99 |
+
db = db_instance.get_db()
|
| 100 |
+
if db is None:
|
| 101 |
+
raise RuntimeError("Database not connected, cannot get collection.")
|
| 102 |
return db[collection_name]
|
| 103 |
|
| 104 |
def get_users_collection():
|