| 1 |
from typing import Any |
| 2 |
|
| 3 |
import redis |
| 4 |
from redis import Redis |
| 5 |
from redis.sentinel import Sentinel |
| 6 |
|
| 7 |
|
| 8 |
def get_redis_connection(config: dict[str, Any], use_strict_redis: bool = False) -> Redis: |
| 9 |
""" |
| 10 |
Returns a redis connection from a connection config |
| 11 |
""" |
| 12 |
redis_cls = redis.StrictRedis if use_strict_redis else redis.Redis |
| 13 |
|
| 14 |
if 'URL' in config: |
| 15 |
if config.get('SSL') or config['URL'].startswith('rediss://'): |
| 16 |
return redis_cls.from_url( |
| 17 |
config['URL'], |
| 18 |
db=config.get('DB'), |
| 19 |
ssl_cert_reqs=config.get('SSL_CERT_REQS', 'required'), |
| 20 |
) |
| 21 |
else: |
| 22 |
return redis_cls.from_url( |
| 23 |
config['URL'], |
| 24 |
db=config.get('DB'), |
| 25 |
) |
| 26 |
|
| 27 |
if 'USE_REDIS_CACHE' in config.keys(): |
| 28 |
try: |
| 29 |
# Assume that we're using django-redis |
| 30 |
from django_redis import get_redis_connection as get_redis |
| 31 |
|
| 32 |
return get_redis(config['USE_REDIS_CACHE']) |
| 33 |
except ImportError: |
| 34 |
pass |
| 35 |
|
| 36 |
from django.core.cache import caches |
| 37 |
|
| 38 |
cache = caches[config['USE_REDIS_CACHE']] |
| 39 |
# We're using django-redis-cache |
| 40 |
try: |
| 41 |
return cache._client # type: ignore[attr-defined] |
| 42 |
except AttributeError: |
| 43 |
# For django-redis-cache > 0.13.1 |
| 44 |
return cache.get_master_client() # type: ignore[attr-defined] |
| 45 |
|
| 46 |
if 'UNIX_SOCKET_PATH' in config: |
| 47 |
return redis_cls(unix_socket_path=config['UNIX_SOCKET_PATH'], db=config['DB']) |
| 48 |
|
| 49 |
if 'SENTINELS' in config: |
| 50 |
connection_kwargs: dict[str, Any] = { |
| 51 |
'db': config.get('DB'), |
| 52 |
'password': config.get('PASSWORD'), |
| 53 |
'username': config.get('USERNAME'), |
| 54 |
'socket_timeout': config.get('SOCKET_TIMEOUT'), |
| 55 |
} |
| 56 |
connection_kwargs.update(config.get('CONNECTION_KWARGS', {})) |
| 57 |
sentinel_kwargs = config.get('SENTINEL_KWARGS', {}) |
| 58 |
sentinel = Sentinel(config['SENTINELS'], sentinel_kwargs=sentinel_kwargs, **connection_kwargs) |
| 59 |
return sentinel.master_for( |
| 60 |
service_name=config['MASTER_NAME'], |
| 61 |
redis_class=redis_cls, |
| 62 |
) |
| 63 |
|
| 64 |
return redis_cls( |
| 65 |
host=config['HOST'], |
| 66 |
port=config['PORT'], |
| 67 |
db=config.get('DB', 0), |
| 68 |
username=config.get('USERNAME', None), |
| 69 |
password=config.get('PASSWORD'), |
| 70 |
ssl=config.get('SSL', False), |
| 71 |
ssl_cert_reqs=config.get('SSL_CERT_REQS', 'required'), |
| 72 |
**config.get('REDIS_CLIENT_KWARGS', {}), |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
def get_connection( |
| 77 |
name: str = 'default', |
| 78 |
use_strict_redis: bool = False, |
| 79 |
) -> Redis: |
| 80 |
""" |
| 81 |
Returns a Redis connection to use based on parameters in settings.RQ_QUEUES |
| 82 |
""" |
| 83 |
from .settings import QUEUES |
| 84 |
|
| 85 |
return get_redis_connection(QUEUES[name], use_strict_redis) |
| 86 |
|
| 87 |
|
| 88 |
def filter_connection_params(queue_params: dict[str, Any]) -> dict[str, Any]: |
| 89 |
""" |
| 90 |
Filters the queue params to keep only the connection related params. |
| 91 |
""" |
| 92 |
CONNECTION_PARAMS = ( |
| 93 |
'URL', |
| 94 |
'DB', |
| 95 |
'USE_REDIS_CACHE', |
| 96 |
'UNIX_SOCKET_PATH', |
| 97 |
'HOST', |
| 98 |
'PORT', |
| 99 |
'PASSWORD', |
| 100 |
'SENTINELS', |
| 101 |
'MASTER_NAME', |
| 102 |
'SOCKET_TIMEOUT', |
| 103 |
'SSL', |
| 104 |
'CONNECTION_KWARGS', |
| 105 |
'SSL_CERT_REQS', |
| 106 |
) |
| 107 |
|
| 108 |
# return {p:v for p,v in queue_params.items() if p in CONNECTION_PARAMS} |
| 109 |
# Dict comprehension compatible with python 2.6 |
| 110 |
return dict((p, v) for (p, v) in queue_params.items() if p in CONNECTION_PARAMS) |
| 111 |
|
| 112 |
|
| 113 |
def get_unique_connection_configs(config: dict[str, dict[str, Any]] | None = None) -> list[dict[str, Any]]: |
| 114 |
""" |
| 115 |
Returns a list of unique Redis connections from config in deterministic order. |
| 116 |
Configs are ordered based on sorted queue names, allowing reliable use of |
| 117 |
connection_index to access specific connections. |
| 118 |
""" |
| 119 |
if config is None: |
| 120 |
from .settings import QUEUES |
| 121 |
|
| 122 |
config = QUEUES |
| 123 |
|
| 124 |
connection_configs = [] |
| 125 |
# Sort queue names to ensure deterministic ordering |
| 126 |
for key in sorted(config.keys()): |
| 127 |
value = filter_connection_params(config[key]) |
| 128 |
if value not in connection_configs: |
| 129 |
connection_configs.append(value) |
| 130 |
return connection_configs |
| 131 |
|
| 132 |
|
| 133 |
def get_connection_by_index(index: int) -> Redis: |
| 134 |
""" |
| 135 |
Returns a Redis connection using parameters from the unique connection |
| 136 |
at the specified index in get_unique_connection_configs() |
| 137 |
|
| 138 |
Args: |
| 139 |
index: The index of the connection in the unique connection configs list |
| 140 |
|
| 141 |
Returns: |
| 142 |
A Redis connection instance |
| 143 |
""" |
| 144 |
config = get_unique_connection_configs()[index] |
| 145 |
return get_redis_connection(config) |