Spaces:
Running
Running
File size: 2,873 Bytes
3533fb9 cbf7ff4 3533fb9 cbf7ff4 3533fb9 cbf7ff4 3533fb9 cbf7ff4 3533fb9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import redis
import logging
from app.core.config import REDIS_URL
logger = logging.getLogger(__name__)
class RedisManager:
"""
Centralized Redis connection manager using a Singleton pattern.
Provides a thread-safe StrictRedis client with automatic reconnection and basic health checks.
"""
_instance = None
_client = None
last_error = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(RedisManager, cls).__new__(cls)
return cls._instance
@property
def client(self):
if self._client is None:
self.connect()
return self._client
def connect(self):
"""Initializes the Redis client and validates the connection."""
try:
# Use connection pooling for efficiency and stability
# Keep this low — Redis Cloud free tier allows ~30 clients total
# across backend, Celery, beat, cache, and the video service.
pool = redis.ConnectionPool.from_url(
REDIS_URL,
decode_responses=True,
socket_timeout=5,
socket_connect_timeout=5,
retry_on_timeout=True,
max_connections=5,
health_check_interval=30,
)
self._client = redis.StrictRedis(connection_pool=pool)
# Simple health check to ensure Redis is responsive
self._client.ping()
self.last_error = None
logger.info(f"Successfully connected to Redis at {REDIS_URL[:32]}...")
except Exception as e:
self.last_error = f"{type(e).__name__}: {str(e)}"
logger.error(f"FATAL: Could not connect to Redis. Error: {e}")
# We don't raise here to allow the app to start,
# but specific features requiring Redis will fail gracefully later.
self._client = None
def get_client(self):
"""Returns the active Redis client instance."""
return self.client
class RedisClientProxy:
"""
A lazy proxy that forwards attributes to the active Redis client.
Allows retrying connection dynamically if it was previously disconnected.
"""
@property
def _real_client(self):
client = redis_manager.client
if client is None:
raise redis.ConnectionError(f"Redis client is not connected. Last error: {redis_manager.last_error}")
return client
def __getattr__(self, name):
return getattr(self._real_client, name)
def __repr__(self):
try:
return repr(self._real_client)
except Exception:
return f"<RedisClientProxy (Disconnected: {redis_manager.last_error})>"
# Export a single instance for use across the application
redis_manager = RedisManager()
redis_client = RedisClientProxy()
|