Add comprehensive Redis connection debugging and network testing scripts
Browse files- core/redis_client.py +97 -18
- debug_redis_connection.py +54 -0
- test_network_connectivity.py +53 -0
core/redis_client.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
import redis
|
| 2 |
import logging
|
| 3 |
import time
|
|
|
|
| 4 |
from typing import Optional
|
| 5 |
|
| 6 |
logger = logging.getLogger(__name__)
|
| 7 |
|
| 8 |
class RedisClient:
|
| 9 |
-
"""Hardcoded Redis client with
|
| 10 |
|
| 11 |
_instance = None
|
| 12 |
_redis_client = None
|
|
@@ -22,35 +23,112 @@ class RedisClient:
|
|
| 22 |
self._connect()
|
| 23 |
|
| 24 |
def _connect(self):
|
| 25 |
-
"""Establish Redis connection with
|
| 26 |
-
logger.info("
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
-
logger.info("Connecting to NEW Redis Cloud with hardcoded configuration")
|
| 34 |
self._redis_client = redis.Redis(
|
| 35 |
-
host=
|
| 36 |
-
port=
|
| 37 |
-
username=
|
| 38 |
-
password=
|
| 39 |
decode_responses=True,
|
| 40 |
socket_connect_timeout=15,
|
| 41 |
socket_timeout=15,
|
| 42 |
-
ssl=True,
|
| 43 |
health_check_interval=30,
|
| 44 |
retry_on_timeout=True
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
-
self._redis_client.ping()
|
| 48 |
-
logger.info("
|
| 49 |
return
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
-
logger.error(f"
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def get_client(self) -> Optional[redis.Redis]:
|
| 56 |
"""Get Redis client instance"""
|
|
@@ -63,7 +141,8 @@ class RedisClient:
|
|
| 63 |
try:
|
| 64 |
self._redis_client.ping()
|
| 65 |
return True
|
| 66 |
-
except:
|
|
|
|
| 67 |
return False
|
| 68 |
|
| 69 |
def reconnect(self):
|
|
|
|
| 1 |
import redis
|
| 2 |
import logging
|
| 3 |
import time
|
| 4 |
+
import ssl
|
| 5 |
from typing import Optional
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
class RedisClient:
|
| 10 |
+
"""Hardcoded Redis client with comprehensive debugging"""
|
| 11 |
|
| 12 |
_instance = None
|
| 13 |
_redis_client = None
|
|
|
|
| 23 |
self._connect()
|
| 24 |
|
| 25 |
def _connect(self):
|
| 26 |
+
"""Establish Redis connection with comprehensive debugging"""
|
| 27 |
+
logger.info("=== Redis Connection Debug Information ===")
|
| 28 |
+
host = 'redis-16717.c85.us-east-1-2.ec2.redns.redis-cloud.com'
|
| 29 |
+
port = 16717
|
| 30 |
+
username = "default"
|
| 31 |
+
password = "bNQGmfkB2fRo4KrT3UXwhAUEUmgDClx7"
|
| 32 |
+
|
| 33 |
+
logger.info(f"Host: {host}")
|
| 34 |
+
logger.info(f"Port: {port}")
|
| 35 |
+
logger.info(f"Username: {username}")
|
| 36 |
+
logger.info(f"Password: {'*' * len(password) if password else 'None'}")
|
| 37 |
+
logger.info("========================================")
|
| 38 |
|
| 39 |
+
# Try different SSL configurations
|
| 40 |
+
ssl_configs = [
|
| 41 |
+
{
|
| 42 |
+
'name': 'Standard SSL with cert validation',
|
| 43 |
+
'ssl': True,
|
| 44 |
+
'ssl_cert_reqs': 'required'
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
'name': 'SSL without cert validation',
|
| 48 |
+
'ssl': True,
|
| 49 |
+
'ssl_cert_reqs': None
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
'name': 'SSL with custom context',
|
| 53 |
+
'ssl': True,
|
| 54 |
+
'ssl_cert_reqs': ssl.CERT_REQUIRED,
|
| 55 |
+
'ssl_context': ssl.create_default_context()
|
| 56 |
+
}
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
for i, config in enumerate(ssl_configs, 1):
|
| 60 |
+
logger.info(f"\n--- Trying SSL Configuration {i}: {config['name']} ---")
|
| 61 |
+
try:
|
| 62 |
+
redis_config = {
|
| 63 |
+
'host': host,
|
| 64 |
+
'port': port,
|
| 65 |
+
'username': username,
|
| 66 |
+
'password': password,
|
| 67 |
+
'decode_responses': True,
|
| 68 |
+
'socket_connect_timeout': 15,
|
| 69 |
+
'socket_timeout': 15,
|
| 70 |
+
'health_check_interval': 30,
|
| 71 |
+
'retry_on_timeout': True
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
# Add SSL configuration
|
| 75 |
+
redis_config.update({k: v for k, v in config.items() if k != 'name'})
|
| 76 |
+
|
| 77 |
+
logger.info(f"Creating Redis client with config: {redis_config}")
|
| 78 |
+
self._redis_client = redis.Redis(**redis_config)
|
| 79 |
+
|
| 80 |
+
logger.info("Attempting to ping Redis...")
|
| 81 |
+
result = self._redis_client.ping()
|
| 82 |
+
logger.info(f"Ping result: {result}")
|
| 83 |
+
|
| 84 |
+
# Test set/get
|
| 85 |
+
logger.info("Testing set/get operations...")
|
| 86 |
+
self._redis_client.set('debug_test_key', 'debug_test_value')
|
| 87 |
+
value = self._redis_client.get('debug_test_key')
|
| 88 |
+
self._redis_client.delete('debug_test_key')
|
| 89 |
+
|
| 90 |
+
if value == 'debug_test_value':
|
| 91 |
+
logger.info("β
Set/Get test successful!")
|
| 92 |
+
logger.info(f"β
Successfully connected with SSL configuration: {config['name']}")
|
| 93 |
+
return
|
| 94 |
+
else:
|
| 95 |
+
logger.warning("β Set/Get test failed")
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
logger.error(f"β Failed with SSL configuration {config['name']}: {e}")
|
| 99 |
+
logger.error(f"Error type: {type(e).__name__}")
|
| 100 |
+
import traceback
|
| 101 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 102 |
+
|
| 103 |
+
# If all SSL configurations fail, try without SSL
|
| 104 |
+
logger.info("\n--- Trying without SSL as last resort ---")
|
| 105 |
try:
|
|
|
|
| 106 |
self._redis_client = redis.Redis(
|
| 107 |
+
host=host,
|
| 108 |
+
port=port,
|
| 109 |
+
username=username,
|
| 110 |
+
password=password,
|
| 111 |
decode_responses=True,
|
| 112 |
socket_connect_timeout=15,
|
| 113 |
socket_timeout=15,
|
|
|
|
| 114 |
health_check_interval=30,
|
| 115 |
retry_on_timeout=True
|
| 116 |
+
# No SSL parameters
|
| 117 |
)
|
| 118 |
|
| 119 |
+
result = self._redis_client.ping()
|
| 120 |
+
logger.info(f"β
Non-SSL connection successful! Ping result: {result}")
|
| 121 |
return
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
+
logger.error(f"β Non-SSL connection also failed: {e}")
|
| 125 |
+
logger.error(f"Error type: {type(e).__name__}")
|
| 126 |
+
import traceback
|
| 127 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 128 |
+
|
| 129 |
+
# If we get here, all connection attempts failed
|
| 130 |
+
logger.error("π₯ All Redis connection attempts failed!")
|
| 131 |
+
self._redis_client = None
|
| 132 |
|
| 133 |
def get_client(self) -> Optional[redis.Redis]:
|
| 134 |
"""Get Redis client instance"""
|
|
|
|
| 141 |
try:
|
| 142 |
self._redis_client.ping()
|
| 143 |
return True
|
| 144 |
+
except Exception as e:
|
| 145 |
+
logger.error(f"Redis health check failed: {e}")
|
| 146 |
return False
|
| 147 |
|
| 148 |
def reconnect(self):
|
debug_redis_connection.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import logging
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
# Add project root to path
|
| 6 |
+
project_root = Path(__file__).parent
|
| 7 |
+
sys.path.append(str(project_root))
|
| 8 |
+
|
| 9 |
+
# Set up logging to see debug information
|
| 10 |
+
logging.basicConfig(
|
| 11 |
+
level=logging.DEBUG,
|
| 12 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
from core.redis_client import redis_client
|
| 16 |
+
|
| 17 |
+
def main():
|
| 18 |
+
"""Debug Redis connection issues"""
|
| 19 |
+
print("=== Redis Connection Debug Test ===")
|
| 20 |
+
|
| 21 |
+
# Test the connection
|
| 22 |
+
client = redis_client.get_client()
|
| 23 |
+
|
| 24 |
+
if client is None:
|
| 25 |
+
print("β Redis client is None - connection failed")
|
| 26 |
+
return 1
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
print("Testing ping...")
|
| 30 |
+
result = client.ping()
|
| 31 |
+
print(f"β
Ping successful: {result}")
|
| 32 |
+
|
| 33 |
+
print("Testing set/get...")
|
| 34 |
+
client.set('debug_test', 'success')
|
| 35 |
+
value = client.get('debug_test')
|
| 36 |
+
client.delete('debug_test')
|
| 37 |
+
|
| 38 |
+
if value == 'success':
|
| 39 |
+
print("β
Set/Get test successful!")
|
| 40 |
+
print("π Redis connection is working!")
|
| 41 |
+
return 0
|
| 42 |
+
else:
|
| 43 |
+
print("β Set/Get test failed")
|
| 44 |
+
return 1
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"β Redis operation failed: {e}")
|
| 48 |
+
import traceback
|
| 49 |
+
print(f"Traceback: {traceback.format_exc()}")
|
| 50 |
+
return 1
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
exit_code = main()
|
| 54 |
+
sys.exit(exit_code)
|
test_network_connectivity.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import socket
|
| 2 |
+
import ssl
|
| 3 |
+
|
| 4 |
+
def test_tcp_connection(host, port):
|
| 5 |
+
"""Test basic TCP connection to host:port"""
|
| 6 |
+
print(f"Testing TCP connection to {host}:{port}...")
|
| 7 |
+
try:
|
| 8 |
+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| 9 |
+
sock.settimeout(10)
|
| 10 |
+
result = sock.connect_ex((host, port))
|
| 11 |
+
sock.close()
|
| 12 |
+
|
| 13 |
+
if result == 0:
|
| 14 |
+
print("β
TCP connection successful")
|
| 15 |
+
return True
|
| 16 |
+
else:
|
| 17 |
+
print(f"β TCP connection failed with error code: {result}")
|
| 18 |
+
return False
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"β TCP connection failed: {e}")
|
| 21 |
+
return False
|
| 22 |
+
|
| 23 |
+
def test_ssl_connection(host, port):
|
| 24 |
+
"""Test SSL connection to host:port"""
|
| 25 |
+
print(f"Testing SSL connection to {host}:{port}...")
|
| 26 |
+
try:
|
| 27 |
+
context = ssl.create_default_context()
|
| 28 |
+
with socket.create_connection((host, port), timeout=10) as sock:
|
| 29 |
+
with context.wrap_socket(sock, server_hostname=host) as ssock:
|
| 30 |
+
print(f"β
SSL connection successful")
|
| 31 |
+
print(f"SSL version: {ssock.version()}")
|
| 32 |
+
return True
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"β SSL connection failed: {e}")
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
host = 'redis-16717.c85.us-east-1-2.ec2.redns.redis-cloud.com'
|
| 39 |
+
port = 16717
|
| 40 |
+
|
| 41 |
+
print("=== Network Connectivity Tests ===")
|
| 42 |
+
|
| 43 |
+
tcp_result = test_tcp_connection(host, port)
|
| 44 |
+
print()
|
| 45 |
+
ssl_result = test_ssl_connection(host, port)
|
| 46 |
+
|
| 47 |
+
print("\n=== Summary ===")
|
| 48 |
+
if tcp_result and ssl_result:
|
| 49 |
+
print("β
All network tests passed")
|
| 50 |
+
elif tcp_result:
|
| 51 |
+
print("β οΈ TCP connection works but SSL failed")
|
| 52 |
+
else:
|
| 53 |
+
print("β Network connectivity issues detected")
|