From fdcd3f2dbbea96b011b413666c29de0f4f177eb2 Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Fri, 14 Mar 2025 15:42:28 -0500 Subject: [PATCH] Added error checking to global redis. --- core/utils.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/core/utils.py b/core/utils.py index 149fcc8f..e9b5f6d2 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,9 +1,28 @@ import redis +import logging from django.conf import settings -# Global Redis connection (Singleton) -redis_client = redis.Redis( - host=settings.REDIS_HOST, - port=6379, - db=settings.REDIS_DB -) +logger = logging.getLogger(__name__) + +def get_redis_client(): + """Get Redis client with connection validation""" + try: + # Create Redis client + client = redis.Redis( + host=settings.REDIS_HOST, + port=getattr(settings, 'REDIS_PORT', 6379), + db=settings.REDIS_DB, + socket_timeout=5, + socket_connect_timeout=5 + ) + + # Validate connection with ping + client.ping() + logger.info(f"Connected to Redis at {settings.REDIS_HOST}:6379/{settings.REDIS_DB}") + return client + except Exception as e: + logger.error(f"Failed to connect to Redis: {e}") + return None + +# Initialize the global client +redis_client = get_redis_client() \ No newline at end of file