Refactor and enhance logging in webhook handlers and Redis client

- Translated comments and docstrings from German to English for better clarity.
- Improved logging consistency across various webhook handlers for create, delete, and update operations.
- Centralized logging functionality by utilizing a dedicated logger utility.
- Added new enums for file and XAI sync statuses in models.
- Updated Redis client factory to use a centralized logger and improved error handling.
- Enhanced API responses to include more descriptive messages and status codes.
This commit is contained in:
bsiggel
2026-03-08 21:50:34 +00:00
parent f392ec0f06
commit a0cf845877
17 changed files with 300 additions and 276 deletions

View File

@@ -1,51 +1,58 @@
"""
Redis Client Factory
Zentralisierte Redis-Client-Verwaltung mit:
- Singleton Pattern
- Connection Pooling
- Automatic Reconnection
- Health Checks
Centralized Redis client management with:
- Singleton pattern
- Connection pooling
- Automatic reconnection
- Health checks
"""
import redis
import os
import logging
from typing import Optional
from services.exceptions import RedisConnectionError
logger = logging.getLogger(__name__)
from services.logging_utils import get_service_logger
class RedisClientFactory:
"""
Singleton Factory für Redis Clients.
Singleton factory for Redis clients.
Vorteile:
- Eine zentrale Konfiguration
- Connection Pooling
- Lazy Initialization
- Besseres Error Handling
Benefits:
- Centralized configuration
- Connection pooling
- Lazy initialization
- Better error handling
"""
_instance: Optional[redis.Redis] = None
_connection_pool: Optional[redis.ConnectionPool] = None
_logger = None
@classmethod
def _get_logger(cls):
"""Get logger instance (lazy initialization)"""
if cls._logger is None:
cls._logger = get_service_logger('redis_factory', None)
return cls._logger
@classmethod
def get_client(cls, strict: bool = False) -> Optional[redis.Redis]:
"""
Gibt Redis Client zurück (erstellt wenn nötig).
Return Redis client (creates if needed).
Args:
strict: Wenn True, wirft Exception bei Verbindungsfehlern.
Wenn False, gibt None zurück (für optionale Redis-Nutzung).
strict: If True, raises exception on connection failures.
If False, returns None (for optional Redis usage).
Returns:
Redis client oder None (wenn strict=False und Verbindung fehlschlägt)
Redis client or None (if strict=False and connection fails)
Raises:
RedisConnectionError: Wenn strict=True und Verbindung fehlschlägt
RedisConnectionError: If strict=True and connection fails
"""
logger = cls._get_logger()
if cls._instance is None:
try:
cls._instance = cls._create_client()
@@ -65,14 +72,15 @@ class RedisClientFactory:
@classmethod
def _create_client(cls) -> redis.Redis:
"""
Erstellt neuen Redis Client mit Connection Pool.
Create new Redis client with connection pool.
Returns:
Configured Redis client
Raises:
redis.ConnectionError: Bei Verbindungsproblemen
redis.ConnectionError: On connection problems
"""
logger = cls._get_logger()
# Load configuration from environment
redis_host = os.getenv('REDIS_HOST', 'localhost')
redis_port = int(os.getenv('REDIS_PORT', '6379'))
@@ -94,7 +102,7 @@ class RedisClientFactory:
socket_timeout=redis_timeout,
socket_connect_timeout=redis_timeout,
max_connections=redis_max_connections,
decode_responses=True # Auto-decode bytes zu strings
decode_responses=True # Auto-decode bytes to strings
)
# Create client from pool
@@ -108,10 +116,11 @@ class RedisClientFactory:
@classmethod
def reset(cls) -> None:
"""
Reset factory state (hauptsächlich für Tests).
Reset factory state (mainly for tests).
Schließt bestehende Verbindungen und setzt Singleton zurück.
Closes existing connections and resets singleton.
"""
logger = cls._get_logger()
if cls._instance:
try:
cls._instance.close()
@@ -131,11 +140,12 @@ class RedisClientFactory:
@classmethod
def health_check(cls) -> bool:
"""
Prüft Redis-Verbindung.
Check Redis connection.
Returns:
True wenn Redis erreichbar, False sonst
True if Redis is reachable, False otherwise
"""
logger = cls._get_logger()
try:
client = cls.get_client(strict=False)
if client is None:
@@ -150,11 +160,12 @@ class RedisClientFactory:
@classmethod
def get_info(cls) -> Optional[dict]:
"""
Gibt Redis Server Info zurück (für Monitoring).
Return Redis server info (for monitoring).
Returns:
Redis info dict oder None bei Fehler
Redis info dict or None on error
"""
logger = cls._get_logger()
try:
client = cls.get_client(strict=False)
if client is None:
@@ -170,22 +181,22 @@ class RedisClientFactory:
def get_redis_client(strict: bool = False) -> Optional[redis.Redis]:
"""
Convenience function für Redis Client.
Convenience function for Redis client.
Args:
strict: Wenn True, wirft Exception bei Fehler
strict: If True, raises exception on error
Returns:
Redis client oder None
Redis client or None
"""
return RedisClientFactory.get_client(strict=strict)
def is_redis_available() -> bool:
"""
Prüft ob Redis verfügbar ist.
Check if Redis is available.
Returns:
True wenn Redis erreichbar
True if Redis is reachable
"""
return RedisClientFactory.health_check()