Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 19 additions & 42 deletions autobot-backend/utils/redis_management/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,40 @@
# Copyright (c) 2025 mrveiss
# Author: mrveiss
"""
Redis Management Package
Redis Management — backward-compatibility shim (Issue #2313).

This package contains the Redis connection management system for AutoBot.
It was split from the monolithic redis_client.py as part of Issue #381.
The canonical implementation has moved to autobot_shared.redis_management.
All existing imports of ``from utils.redis_management import ...``
continue to work unchanged via this re-export.

Package Structure:
- types.py: Enums (RedisDatabase, ConnectionState) and database mapping
- config.py: RedisConfig, RedisConfigLoader, PoolConfig
- statistics.py: RedisStats, PoolStatistics, ManagerStats, ConnectionMetrics
- connection_manager.py: RedisConnectionManager class

Usage:
from utils.redis_management import (
# Enums
RedisDatabase, ConnectionState,
# Configuration
RedisConfig, RedisConfigLoader, PoolConfig,
# Statistics
RedisStats, PoolStatistics, ManagerStats, ConnectionMetrics,
# Manager
RedisConnectionManager,
# Constants
DATABASE_MAPPING,
)

For backward compatibility, the original redis_client.py module
still exports all classes and functions directly.
Preferred import:
from autobot_shared.redis_management import RedisConnectionManager
"""

# Configuration classes
from .config import PoolConfig, RedisConfig, RedisConfigLoader

# Connection manager
from .connection_manager import RedisConnectionManager

# Statistics dataclasses
from .statistics import ConnectionMetrics, ManagerStats, PoolStatistics, RedisStats

# Types and constants
from .types import DATABASE_MAPPING, ConnectionState, RedisDatabase
from autobot_shared.redis_management import ( # noqa: F401
DATABASE_MAPPING,
ConnectionMetrics,
ConnectionState,
ManagerStats,
PoolConfig,
PoolStatistics,
RedisConfig,
RedisConfigLoader,
RedisConnectionManager,
RedisDatabase,
RedisStats,
)

# Re-export for convenience
__all__ = [
# Enums
"RedisDatabase",
"ConnectionState",
# Constants
"DATABASE_MAPPING",
# Configuration
"RedisConfig",
"RedisConfigLoader",
"PoolConfig",
# Statistics
"RedisStats",
"PoolStatistics",
"ManagerStats",
"ConnectionMetrics",
# Manager
"RedisConnectionManager",
]
2 changes: 1 addition & 1 deletion autobot-backend/utils/redis_thread_safety_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ async def test_cleanup_doesnt_affect_active_connections(self):
manager._max_idle_time_seconds = 300 # 5 minutes

# Issue #2211: mock datetime on the module that actually uses it.
import utils.redis_management.connection_manager as cm_module
import autobot_shared.redis_management.connection_manager as cm_module

original_datetime = cm_module.datetime

Expand Down
18 changes: 9 additions & 9 deletions autobot-shared/redis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@
# constants.threshold_constants, config, monitoring.prometheus_metrics).
# Moving redis_management/ to autobot-shared would require pulling in those
# backend modules too — the dependency chain is deep.
# For now this coupling is accepted and documented here.
# See issue #2220 for full analysis and discussion of moving redis_management
# to autobot-shared as the preferred long-term fix.
from utils.redis_management.config import PoolConfig, RedisConfig, RedisConfigLoader
from utils.redis_management.connection_manager import RedisConnectionManager
from utils.redis_management.statistics import (
# redis_management now lives in autobot-shared (Issue #2313).
from autobot_shared.redis_management.config import (
PoolConfig,
RedisConfig,
RedisConfigLoader,
)
from autobot_shared.redis_management.connection_manager import RedisConnectionManager
from autobot_shared.redis_management.statistics import (
ConnectionMetrics,
ManagerStats,
PoolStatistics,
RedisStats,
)

# Import all types, data models, and classes from the package (Issue #381 refactoring)
from utils.redis_management.types import (
from autobot_shared.redis_management.types import (
DATABASE_MAPPING,
ConnectionState,
RedisDatabase,
Expand Down
57 changes: 57 additions & 0 deletions autobot-shared/redis_management/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# AutoBot - AI-Powered Automation Platform
# Copyright (c) 2025 mrveiss
# Author: mrveiss
"""
Redis Management Package (#2313).

Canonical location for Redis connection management. Moved from
autobot-backend/utils/redis_management/ to autobot-shared/ so all
components (backend, SLM backend, standalone scripts) share the same code.

Package Structure:
- types.py: Enums (RedisDatabase, ConnectionState) and database mapping
- config.py: RedisConfig, RedisConfigLoader, PoolConfig
- statistics.py: RedisStats, PoolStatistics, ManagerStats, ConnectionMetrics
- connection_manager.py: RedisConnectionManager class

Usage:
from autobot_shared.redis_management import (
RedisDatabase, ConnectionState,
RedisConfig, RedisConfigLoader, PoolConfig,
RedisStats, PoolStatistics, ManagerStats, ConnectionMetrics,
RedisConnectionManager,
DATABASE_MAPPING,
)
"""

# Configuration classes
from .config import PoolConfig, RedisConfig, RedisConfigLoader

# Connection manager
from .connection_manager import RedisConnectionManager

# Statistics dataclasses
from .statistics import ConnectionMetrics, ManagerStats, PoolStatistics, RedisStats

# Types and constants
from .types import DATABASE_MAPPING, ConnectionState, RedisDatabase

# Re-export for convenience
__all__ = [
# Enums
"RedisDatabase",
"ConnectionState",
# Constants
"DATABASE_MAPPING",
# Configuration
"RedisConfig",
"RedisConfigLoader",
"PoolConfig",
# Statistics
"RedisStats",
"PoolStatistics",
"ManagerStats",
"ConnectionMetrics",
# Manager
"RedisConnectionManager",
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
# Copyright (c) 2025 mrveiss
# Author: mrveiss
"""
Redis Configuration Module
Redis Configuration Module (#2313).

Contains configuration classes for Redis connection management:
- RedisConfig: Database configuration dataclass
- RedisConfigLoader: Multi-source configuration loader
- PoolConfig: Connection pool configuration

Extracted from redis_client.py as part of Issue #381 refactoring.
Extracted from redis_client.py (Issue #381).
Moved from autobot-backend/utils/ to autobot-shared/ (Issue #2313).

Backend-specific constant imports (REDIS_CONFIG, RetryConfig) have been replaced
with their literal values so this module has no backend dependencies.
"""

import logging
Expand All @@ -18,12 +22,26 @@
from typing import Any, Dict, Optional

import yaml
from constants.network_constants import NetworkConstants
from constants.redis_constants import REDIS_CONFIG
from constants.threshold_constants import RetryConfig

from autobot_shared.network_constants import NetworkConstants

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Redis connection defaults (previously from constants.redis_constants.REDIS_CONFIG)
# ---------------------------------------------------------------------------
_SOCKET_TIMEOUT = 5
_MAX_CONNECTIONS_POOL = 20
_MIN_CONNECTIONS_POOL = 2
_HEALTH_CHECK_INTERVAL = 30
_RETRY_ON_TIMEOUT = True
_CIRCUIT_BREAKER_THRESHOLD = 5
_CIRCUIT_BREAKER_TIMEOUT = 60

# Retry defaults (previously from constants.threshold_constants.RetryConfig)
_DEFAULT_RETRIES = 3
_BACKOFF_BASE = 2.0


@dataclass
class RedisConfig:
Expand All @@ -40,14 +58,14 @@ class RedisConfig:
port: int = NetworkConstants.REDIS_PORT
password: Optional[str] = None
decode_responses: bool = True
max_connections: int = REDIS_CONFIG.MAX_CONNECTIONS_POOL
socket_timeout: float = float(REDIS_CONFIG.SOCKET_TIMEOUT)
socket_connect_timeout: float = float(REDIS_CONFIG.SOCKET_TIMEOUT)
max_connections: int = _MAX_CONNECTIONS_POOL
socket_timeout: float = float(_SOCKET_TIMEOUT)
socket_connect_timeout: float = float(_SOCKET_TIMEOUT)
socket_keepalive: bool = True
socket_keepalive_options: Optional[Dict[int, int]] = None
health_check_interval: int = REDIS_CONFIG.HEALTH_CHECK_INTERVAL
retry_on_timeout: bool = REDIS_CONFIG.RETRY_ON_TIMEOUT
max_retries: int = RetryConfig.DEFAULT_RETRIES
health_check_interval: int = _HEALTH_CHECK_INTERVAL
retry_on_timeout: bool = _RETRY_ON_TIMEOUT
max_retries: int = _DEFAULT_RETRIES
description: str = ""
# TLS configuration
ssl: bool = False
Expand Down Expand Up @@ -78,7 +96,7 @@ def __post_init__(self):
from pathlib import Path

cert_dir = os.getenv("AUTOBOT_TLS_CERT_DIR", "certs")
project_root = Path(__file__).parent.parent.parent.parent
project_root = Path(__file__).parent.parent.parent
self.ssl_ca_certs = str(project_root / cert_dir / "ca" / "ca-cert.pem")
self.ssl_certfile = str(
project_root / cert_dir / "main-host" / "server-cert.pem"
Expand Down Expand Up @@ -144,7 +162,7 @@ def _parse_database_config(db_name: str, db_config: Dict[str, Any]) -> RedisConf
socket_keepalive=db_config.get("socket_keepalive", True),
health_check_interval=db_config.get("health_check_interval", 30),
retry_on_timeout=db_config.get("retry_on_timeout", True),
max_retries=db_config.get("max_retries", RetryConfig.DEFAULT_RETRIES),
max_retries=db_config.get("max_retries", _DEFAULT_RETRIES),
description=db_config.get("description", ""),
)

Expand Down Expand Up @@ -185,7 +203,9 @@ def load_from_yaml(yaml_path: str = None) -> Dict[str, RedisConfig]:
}

logger.info(
f"Loaded {len(configs)} database configurations from {resolved_path}"
"Loaded %d database configurations from %s",
len(configs),
resolved_path,
)
return configs

Expand Down Expand Up @@ -245,7 +265,7 @@ def load_timeout_config() -> Dict[str, Any]:
"socket_timeout": 5.0,
"socket_connect_timeout": 5.0,
"retry_on_timeout": True,
"max_retries": RetryConfig.DEFAULT_RETRIES,
"max_retries": _DEFAULT_RETRIES,
}


Expand All @@ -257,16 +277,17 @@ class PoolConfig:
Controls connection pool behavior including sizing,
timeouts, retry logic, and circuit breaker settings.

Issue #611: Values now reference REDIS_CONFIG constants.
Issue #611: Values previously referenced REDIS_CONFIG constants,
now inlined after move to autobot-shared (#2313).
"""

max_connections: int = REDIS_CONFIG.MAX_CONNECTIONS_POOL
min_connections: int = REDIS_CONFIG.MIN_CONNECTIONS_POOL
socket_timeout: float = float(REDIS_CONFIG.SOCKET_TIMEOUT)
socket_connect_timeout: float = float(REDIS_CONFIG.SOCKET_TIMEOUT)
retry_on_timeout: bool = REDIS_CONFIG.RETRY_ON_TIMEOUT
max_retries: int = RetryConfig.DEFAULT_RETRIES
backoff_factor: float = RetryConfig.BACKOFF_BASE
health_check_interval: float = float(REDIS_CONFIG.HEALTH_CHECK_INTERVAL)
circuit_breaker_threshold: int = REDIS_CONFIG.CIRCUIT_BREAKER_THRESHOLD
circuit_breaker_timeout: int = REDIS_CONFIG.CIRCUIT_BREAKER_TIMEOUT
max_connections: int = _MAX_CONNECTIONS_POOL
min_connections: int = _MIN_CONNECTIONS_POOL
socket_timeout: float = float(_SOCKET_TIMEOUT)
socket_connect_timeout: float = float(_SOCKET_TIMEOUT)
retry_on_timeout: bool = _RETRY_ON_TIMEOUT
max_retries: int = _DEFAULT_RETRIES
backoff_factor: float = _BACKOFF_BASE
health_check_interval: float = float(_HEALTH_CHECK_INTERVAL)
circuit_breaker_threshold: int = _CIRCUIT_BREAKER_THRESHOLD
circuit_breaker_timeout: int = _CIRCUIT_BREAKER_TIMEOUT
Loading
Loading