1
1
from typing import List , Dict , Set
2
2
3
3
import redis
4
- import valkey
5
4
6
- from .connection_types import RedisSentinel , BrokerConnectionClass
5
+ from .connection_types import RedisSentinel , ValkeySentinel , BrokerConnectionClass
7
6
from .rq_classes import JobExecution , DjangoQueue , DjangoWorker
8
7
from .settings import SCHEDULER_CONFIG
9
8
from .settings import logger , Broker
10
9
10
+ ConnectionErrors = {redis .ConnectionError }
11
+
12
+ try :
13
+ from valkey import exceptions
14
+ ConnectionErrors .add (exceptions .ConnectionError )
15
+ except ImportError :
16
+ pass
17
+
11
18
_CONNECTION_PARAMS = {
12
19
"URL" ,
13
20
"DB" ,
27
34
class QueueNotFoundError (Exception ):
28
35
pass
29
36
37
+ ssl_url_protocol = {
38
+ "valkey" : "valkeys" ,
39
+ "redis" : "rediss" ,
40
+ "fakeredis" : "rediss"
41
+ }
42
+
43
+ sentinel_broker = {
44
+ "valkey" : ValkeySentinel ,
45
+ "redis" : RedisSentinel ,
46
+ }
30
47
31
- def _get_redis_connection (config , use_strict_redis = False ):
48
+ def _get_broker_connection (config , use_strict_broker = False ):
32
49
"""
33
50
Returns a redis connection from a connection config
34
51
"""
35
52
if SCHEDULER_CONFIG .BROKER == Broker .FAKEREDIS :
36
53
import fakeredis
37
54
38
- redis_cls = fakeredis .FakeRedis if use_strict_redis else fakeredis .FakeStrictRedis
55
+ broker_cls = fakeredis .FakeRedis if not use_strict_broker else fakeredis .FakeStrictRedis
39
56
else :
40
- redis_cls = BrokerConnectionClass [(SCHEDULER_CONFIG .BROKER , use_strict_redis )]
57
+ broker_cls = BrokerConnectionClass [(SCHEDULER_CONFIG .BROKER , use_strict_broker )]
41
58
logger .debug (f"Getting connection for { config } " )
42
59
if "URL" in config :
43
- if config .get ("SSL" ) or config .get ("URL" ).startswith ("rediss ://" ):
44
- return redis_cls .from_url (
60
+ if config .get ("SSL" ) or config .get ("URL" ).startswith (f" { ssl_url_protocol [ SCHEDULER_CONFIG . BROKER . value ] } ://" ):
61
+ return broker_cls .from_url (
45
62
config ["URL" ],
46
63
db = config .get ("DB" ),
47
64
ssl_cert_reqs = config .get ("SSL_CERT_REQS" , "required" ),
48
65
)
49
66
else :
50
- return redis_cls .from_url (
67
+ return broker_cls .from_url (
51
68
config ["URL" ],
52
69
db = config .get ("DB" ),
53
70
)
54
71
if "UNIX_SOCKET_PATH" in config :
55
- return redis_cls (unix_socket_path = config ["UNIX_SOCKET_PATH" ], db = config ["DB" ])
72
+ return broker_cls (unix_socket_path = config ["UNIX_SOCKET_PATH" ], db = config ["DB" ])
56
73
57
74
if "SENTINELS" in config :
58
75
connection_kwargs = {
@@ -63,13 +80,13 @@ def _get_redis_connection(config, use_strict_redis=False):
63
80
}
64
81
connection_kwargs .update (config .get ("CONNECTION_KWARGS" , {}))
65
82
sentinel_kwargs = config .get ("SENTINEL_KWARGS" , {})
66
- sentinel = RedisSentinel (config ["SENTINELS" ], sentinel_kwargs = sentinel_kwargs , ** connection_kwargs )
83
+ sentinel = sentinel_broker [ SCHEDULER_CONFIG . BROKER . value ] (config ["SENTINELS" ], sentinel_kwargs = sentinel_kwargs , ** connection_kwargs )
67
84
return sentinel .master_for (
68
85
service_name = config ["MASTER_NAME" ],
69
- redis_class = redis_cls ,
86
+ redis_class = broker_cls ,
70
87
)
71
88
72
- return redis_cls (
89
+ return broker_cls (
73
90
host = config ["HOST" ],
74
91
port = config ["PORT" ],
75
92
db = config .get ("DB" , 0 ),
@@ -82,8 +99,8 @@ def _get_redis_connection(config, use_strict_redis=False):
82
99
83
100
84
101
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 )
102
+ """Returns a Broker connection to use based on parameters in SCHEDULER_QUEUES"""
103
+ return _get_broker_connection (queue_settings , use_strict_redis )
87
104
88
105
89
106
def get_queue (
@@ -116,7 +133,7 @@ def get_all_workers() -> Set[DjangoWorker]:
116
133
try :
117
134
curr_workers : Set [DjangoWorker ] = set (DjangoWorker .all (connection = connection ))
118
135
workers_set .update (curr_workers )
119
- except ( redis . ConnectionError , valkey . ConnectionError ) as e :
136
+ except ConnectionErrors as e :
120
137
logger .error (f"Could not connect for queue { queue_name } : { e } " )
121
138
return workers_set
122
139
@@ -142,7 +159,7 @@ def get_queues(*queue_names, **kwargs) -> List[DjangoQueue]:
142
159
for name in queue_names [1 :]:
143
160
if not _queues_share_connection_params (queue_params , QUEUES [name ]):
144
161
raise ValueError (
145
- f'Queues must have the same redis connection. "{ name } " and'
162
+ f'Queues must have the same broker connection. "{ name } " and'
146
163
f' "{ queue_names [0 ]} " have different connections'
147
164
)
148
165
queue = get_queue (name , ** kwargs )
0 commit comments