Skip to content

Commit 768a99f

Browse files
committed
Initial connection hook and worker integration
1 parent 84bfd1f commit 768a99f

4 files changed

Lines changed: 64 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,4 @@ cython_debug/
159159
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160160
# and can be added to the global gitignore or merged into this file. For a more nuclear
161161
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162-
#.idea/
162+
.idea/

hyrex/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .hyrex_queue import HyrexQueue
77
from .hyrex_registry import HyrexRegistry
88
from .worker.worker import HyrexWorker
9+
from .connection_hook import ConnectionHook
910

1011
# Set up null handler at library root level
1112
logging.getLogger("hyrex").addHandler(logging.NullHandler())

hyrex/connection_hook.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import Dict, Any
2+
from abc import ABC, abstractmethod
3+
4+
5+
class ConnectionHook(ABC):
6+
"""Base class for connection hooks"""
7+
8+
def __init__(self):
9+
self._connection = None
10+
11+
@property
12+
def name(self) -> str:
13+
"""Unique identifier for this connection type"""
14+
return self.__class__.__name__
15+
16+
@abstractmethod
17+
def create_connection(self) -> Any:
18+
"""Create a new connection"""
19+
pass
20+
21+
@abstractmethod
22+
def close_connection(self, connection: Any) -> None:
23+
"""Close the given connection"""
24+
pass
25+
26+
def get_connection(self) -> Any:
27+
"""Get the cached connection, creating it if necessary"""
28+
if self._connection is None:
29+
self._connection = self.create_connection()
30+
return self._connection
31+
32+
def cleanup(self) -> None:
33+
"""Internal method to clean up the current connection"""
34+
if self._connection is not None:
35+
self.close_connection(self._connection)
36+
self._connection = None
37+
38+
def health_check(self) -> bool:
39+
"""Verify the connection is still viable"""
40+
return True

hyrex/worker/worker.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from typing import Callable
1+
from typing import Any, Callable
22

33
from hyrex import constants
4+
from hyrex.connection_hook import ConnectionHook
45
from 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

Comments
 (0)