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