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