1
1
from typing import List , Dict , Set
2
2
3
- import redis
4
- import valkey
5
-
6
- from .connection_types import RedisSentinel , BrokerConnectionClass
3
+ from .broker_types import ConnectionErrorTypes , BrokerMetaData
7
4
from .rq_classes import JobExecution , DjangoQueue , DjangoWorker
8
5
from .settings import SCHEDULER_CONFIG
9
6
from .settings import logger , Broker
@@ -28,31 +25,32 @@ class QueueNotFoundError(Exception):
28
25
pass
29
26
30
27
31
- def _get_redis_connection (config , use_strict_redis = False ):
28
+ def _get_broker_connection (config , use_strict_broker = False ):
32
29
"""
33
30
Returns a redis connection from a connection config
34
31
"""
35
32
if SCHEDULER_CONFIG .BROKER == Broker .FAKEREDIS :
36
33
import fakeredis
37
34
38
- redis_cls = fakeredis .FakeRedis if use_strict_redis else fakeredis .FakeStrictRedis
35
+ broker_cls = fakeredis .FakeRedis if not use_strict_broker else fakeredis .FakeStrictRedis
39
36
else :
40
- redis_cls = BrokerConnectionClass [(SCHEDULER_CONFIG .BROKER , use_strict_redis ) ]
37
+ broker_cls = BrokerMetaData [(SCHEDULER_CONFIG .BROKER , use_strict_broker )][ 0 ]
41
38
logger .debug (f"Getting connection for { config } " )
42
39
if "URL" in config :
43
- if config .get ("SSL" ) or config .get ("URL" ).startswith ("rediss://" ):
44
- return redis_cls .from_url (
40
+ ssl_url_protocol = BrokerMetaData [(SCHEDULER_CONFIG .BROKER , use_strict_broker )][2 ]
41
+ if config .get ("SSL" ) or config .get ("URL" ).startswith (f"{ ssl_url_protocol } ://" ):
42
+ return broker_cls .from_url (
45
43
config ["URL" ],
46
44
db = config .get ("DB" ),
47
45
ssl_cert_reqs = config .get ("SSL_CERT_REQS" , "required" ),
48
46
)
49
47
else :
50
- return redis_cls .from_url (
48
+ return broker_cls .from_url (
51
49
config ["URL" ],
52
50
db = config .get ("DB" ),
53
51
)
54
52
if "UNIX_SOCKET_PATH" in config :
55
- return redis_cls (unix_socket_path = config ["UNIX_SOCKET_PATH" ], db = config ["DB" ])
53
+ return broker_cls (unix_socket_path = config ["UNIX_SOCKET_PATH" ], db = config ["DB" ])
56
54
57
55
if "SENTINELS" in config :
58
56
connection_kwargs = {
@@ -63,13 +61,14 @@ def _get_redis_connection(config, use_strict_redis=False):
63
61
}
64
62
connection_kwargs .update (config .get ("CONNECTION_KWARGS" , {}))
65
63
sentinel_kwargs = config .get ("SENTINEL_KWARGS" , {})
66
- sentinel = RedisSentinel (config ["SENTINELS" ], sentinel_kwargs = sentinel_kwargs , ** connection_kwargs )
64
+ SentinelClass = BrokerMetaData [(SCHEDULER_CONFIG .BROKER , use_strict_broker )][1 ]
65
+ sentinel = SentinelClass (config ["SENTINELS" ], sentinel_kwargs = sentinel_kwargs , ** connection_kwargs )
67
66
return sentinel .master_for (
68
67
service_name = config ["MASTER_NAME" ],
69
- redis_class = redis_cls ,
68
+ redis_class = broker_cls ,
70
69
)
71
70
72
- return redis_cls (
71
+ return broker_cls (
73
72
host = config ["HOST" ],
74
73
port = config ["PORT" ],
75
74
db = config .get ("DB" , 0 ),
@@ -82,8 +81,8 @@ def _get_redis_connection(config, use_strict_redis=False):
82
81
83
82
84
83
def get_connection (queue_settings , use_strict_redis = False ):
85
- """Returns a Redis connection to use based on parameters in SCHEDULER_QUEUES"""
86
- return _get_redis_connection (queue_settings , use_strict_redis )
84
+ """Returns a Broker connection to use based on parameters in SCHEDULER_QUEUES"""
85
+ return _get_broker_connection (queue_settings , use_strict_redis )
87
86
88
87
89
88
def get_queue (
@@ -116,7 +115,7 @@ def get_all_workers() -> Set[DjangoWorker]:
116
115
try :
117
116
curr_workers : Set [DjangoWorker ] = set (DjangoWorker .all (connection = connection ))
118
117
workers_set .update (curr_workers )
119
- except ( redis . ConnectionError , valkey . ConnectionError ) as e :
118
+ except ConnectionErrorTypes as e :
120
119
logger .error (f"Could not connect for queue { queue_name } : { e } " )
121
120
return workers_set
122
121
@@ -142,7 +141,7 @@ def get_queues(*queue_names, **kwargs) -> List[DjangoQueue]:
142
141
for name in queue_names [1 :]:
143
142
if not _queues_share_connection_params (queue_params , QUEUES [name ]):
144
143
raise ValueError (
145
- f'Queues must have the same redis connection. "{ name } " and'
144
+ f'Queues must have the same broker connection. "{ name } " and'
146
145
f' "{ queue_names [0 ]} " have different connections'
147
146
)
148
147
queue = get_queue (name , ** kwargs )
0 commit comments