1- from typing import Callable
1+ from typing import Any , Callable
22
33from hyrex import constants
4+ from hyrex .connection_hook import ConnectionHook
45from hyrex .hyrex_registry import HyrexRegistry
56
67
@@ -14,6 +15,26 @@ def __init__(
1415 ):
1516 self .queue = queue
1617 self .task_registry : HyrexRegistry = HyrexRegistry ()
18+ self ._connection_hooks : dict [str , ConnectionHook ] = {}
1719
1820 def add_registry (self , registry : HyrexRegistry ):
1921 self .task_registry .add_registry (registry )
22+
23+ def register_connection (self , hook : ConnectionHook ) -> None :
24+ """Register a connection hook with this worker"""
25+ if hook .name in self ._connection_hooks :
26+ raise ValueError (f"A connection hook is already registered with name: { hook .name } " )
27+ self ._connection_hooks [hook .name ] = hook
28+
29+ def get_connection (self , name : str ) -> Any :
30+ """Get a connection by name"""
31+ hook = self ._connection_hooks .get (name )
32+ if not hook :
33+ raise KeyError (f"No connection hook registered for: { name } " )
34+ return hook .get_connection ()
35+
36+ def cleanup_connections (self ) -> None :
37+ """Cleanup all connections"""
38+ for hook in self ._connection_hooks .values ():
39+ hook .cleanup ()
40+ self ._connection_hooks .clear ()
0 commit comments