Refactor Redis management and enhance system health callbacks

- Replaced the `RedisManager` class with a more modular `SyncRedisManager` and `AsyncRedisManager`, improving the separation of synchronous and asynchronous operations.
- Updated the `system_health.py` callbacks to utilize the new `get_sync_redis_manager` function for Redis interactions, simplifying the connection process.
- Enhanced error handling and logging in Redis status checks, providing clearer feedback on connection issues.
- Revised the setup documentation to reflect changes in Redis connection testing, ensuring clarity for users.

These updates improve the maintainability and reliability of Redis interactions within the system, aligning with best practices for modular design.
This commit is contained in:
Vasily.onl
2025-06-07 00:27:17 +08:00
parent 1466223b85
commit fe9d8e75ed
4 changed files with 344 additions and 413 deletions

View File

@@ -12,7 +12,7 @@ from dash import Output, Input, State, html, callback_context, no_update
import dash_bootstrap_components as dbc
from utils.logger import get_logger
from database.connection import DatabaseManager
from database.redis_manager import RedisManager
from database.redis_manager import get_sync_redis_manager
logger = get_logger("system_health_callbacks")
@@ -235,13 +235,16 @@ def _get_database_quick_status() -> dbc.Badge:
def _get_redis_quick_status() -> dbc.Badge:
"""Get quick Redis status."""
try:
redis_manager = RedisManager()
redis_manager = get_sync_redis_manager()
redis_manager.initialize()
if redis_manager.test_connection():
# This check is simplified as initialize() would raise an error on failure.
# For a more explicit check, a dedicated test_connection could be added to SyncRedisManager.
if redis_manager.client.ping():
return dbc.Badge("Connected", color="success", className="me-1")
else:
return dbc.Badge("Error", color="danger", className="me-1")
except:
except Exception as e:
logger.error(f"Redis quick status check failed: {e}")
return dbc.Badge("Error", color="danger", className="me-1")
@@ -418,38 +421,52 @@ def _get_database_statistics() -> html.Div:
def _get_redis_status() -> html.Div:
"""Get Redis status."""
"""Get detailed Redis server status."""
try:
redis_manager = RedisManager()
redis_manager = get_sync_redis_manager()
redis_manager.initialize()
info = redis_manager.get_info()
if not redis_manager.client.ping():
raise ConnectionError("Redis server is not responding.")
info = redis_manager.client.info()
status_badge = dbc.Badge("Connected", color="success", className="me-1")
return html.Div([
dbc.Row([
dbc.Col(dbc.Badge("Redis Connected", color="success"), width="auto"),
dbc.Col(f"Checked: {datetime.now().strftime('%H:%M:%S')}", className="text-muted")
], align="center", className="mb-2"),
html.P(f"Host: {redis_manager.config.host}:{redis_manager.config.port}", className="mb-0")
html.H5("Redis Status"),
status_badge,
html.P(f"Version: {info.get('redis_version', 'N/A')}"),
html.P(f"Mode: {info.get('redis_mode', 'N/A')}")
])
except Exception as e:
return dbc.Alert(f"Error connecting to Redis: {e}", color="danger")
logger.error(f"Failed to get Redis status: {e}")
return html.Div([
html.H5("Redis Status"),
dbc.Badge("Error", color="danger", className="me-1"),
dbc.Alert(f"Error: {e}", color="danger", dismissable=True)
])
def _get_redis_statistics() -> html.Div:
"""Get Redis statistics."""
"""Get detailed Redis statistics."""
try:
redis_manager = RedisManager()
redis_manager = get_sync_redis_manager()
redis_manager.initialize()
info = redis_manager.get_info()
if not redis_manager.client.ping():
raise ConnectionError("Redis server is not responding.")
info = redis_manager.client.info()
return html.Div([
dbc.Row([dbc.Col("Memory Used:"), dbc.Col(info.get('used_memory_human', 'N/A'), className="text-end")]),
dbc.Row([dbc.Col("Connected Clients:"), dbc.Col(info.get('connected_clients', 'N/A'), className="text-end")]),
dbc.Row([dbc.Col("Uptime (hours):"), dbc.Col(f"{info.get('uptime_in_seconds', 0) // 3600}", className="text-end")])
html.H5("Redis Statistics"),
html.P(f"Connected Clients: {info.get('connected_clients', 'N/A')}"),
html.P(f"Memory Used: {info.get('used_memory_human', 'N/A')}"),
html.P(f"Total Commands Processed: {info.get('total_commands_processed', 'N/A')}")
])
except Exception as e:
return dbc.Alert(f"Error loading Redis stats: {e}", color="danger")
logger.error(f"Failed to get Redis statistics: {e}")
return dbc.Alert(f"Error: {e}", color="danger", dismissable=True)
def _get_system_performance_metrics() -> html.Div: