11from typing import List , Dict , Set
22
3- import redis
4-
5- from .connection_types import RedisSentinel , ValkeySentinel , BrokerConnectionClass
3+ from .broker_types import ConnectionErrorTypes , BrokerMetaData
64from .rq_classes import JobExecution , DjangoQueue , DjangoWorker
75from .settings import SCHEDULER_CONFIG
86from .settings import logger , Broker
97
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-
198_CONNECTION_PARAMS = {
209 "URL" ,
2110 "DB" ,
@@ -36,18 +25,6 @@ class QueueNotFoundError(Exception):
3625 pass
3726
3827
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-
5128def _get_broker_connection (config , use_strict_broker = False ):
5229 """
5330 Returns a redis connection from a connection config
@@ -57,10 +34,11 @@ def _get_broker_connection(config, use_strict_broker=False):
5734
5835 broker_cls = fakeredis .FakeRedis if not use_strict_broker else fakeredis .FakeStrictRedis
5936 else :
60- broker_cls = BrokerConnectionClass [(SCHEDULER_CONFIG .BROKER , use_strict_broker )]
37+ broker_cls = BrokerMetaData [(SCHEDULER_CONFIG .BROKER , use_strict_broker )][ 0 ]
6138 logger .debug (f"Getting connection for { config } " )
6239 if "URL" in config :
63- if config .get ("SSL" ) or config .get ("URL" ).startswith (f"{ ssl_url_protocol [SCHEDULER_CONFIG .BROKER .value ]} ://" ):
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 } ://" ):
6442 return broker_cls .from_url (
6543 config ["URL" ],
6644 db = config .get ("DB" ),
@@ -83,9 +61,8 @@ def _get_broker_connection(config, use_strict_broker=False):
8361 }
8462 connection_kwargs .update (config .get ("CONNECTION_KWARGS" , {}))
8563 sentinel_kwargs = config .get ("SENTINEL_KWARGS" , {})
86- sentinel = sentinel_broker [SCHEDULER_CONFIG .BROKER .value ](
87- config ["SENTINELS" ], sentinel_kwargs = sentinel_kwargs , ** connection_kwargs
88- )
64+ SentinelClass = BrokerMetaData [(SCHEDULER_CONFIG .BROKER , use_strict_broker )][1 ]
65+ sentinel = SentinelClass (config ["SENTINELS" ], sentinel_kwargs = sentinel_kwargs , ** connection_kwargs )
8966 return sentinel .master_for (
9067 service_name = config ["MASTER_NAME" ],
9168 redis_class = broker_cls ,
@@ -138,7 +115,7 @@ def get_all_workers() -> Set[DjangoWorker]:
138115 try :
139116 curr_workers : Set [DjangoWorker ] = set (DjangoWorker .all (connection = connection ))
140117 workers_set .update (curr_workers )
141- except ConnectionErrors as e :
118+ except ConnectionErrorTypes as e :
142119 logger .error (f"Could not connect for queue { queue_name } : { e } " )
143120 return workers_set
144121
0 commit comments