Skip to content

Commit

Permalink
Add polling decorator and handle port edge cases
Browse files Browse the repository at this point in the history
- Added `polling` decorator in `daemon.py` to `possess` and `wait_for_readiness` methods.
- Improved error handling for master and worker ports in `daemon.py`.
- Bumped package version from `0.16.5` to `0.16.6` in `pyproject.toml`.

The `polling` decorator improves process stability by ensuring certain conditions are met before proceeding. Enhanced error handling offers more robust port assignments, preventing runtime errors due to unexpected conditions. The version bump reflects these significant internal improvements. 🚀
  • Loading branch information
horta committed Dec 6, 2024
1 parent f2bd9ff commit 9cad700
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion h3daemon/h3daemon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from h3daemon.errors import ChildNotFoundError, PIDNotFoundError
from h3daemon.healthy import assert_peers_healthy
from h3daemon.master import Master
from h3daemon.polling import polling
from h3daemon.port import find_free_port
from h3daemon.worker import Worker

Expand Down Expand Up @@ -80,10 +81,12 @@ def spawn(cls, hmmfile: HMMFile, cport: int = 0, wport: int = 0):
return cls(master, worker, None)

@classmethod
@polling
def possess(cls, pidfile: PIDLockFile):
pid = pidfile.is_locked()
if not pid:
raise PIDNotFoundError("PID not in pidfile.")

process = psutil.Process(pid)
children = process.children()

Expand All @@ -109,6 +112,10 @@ def shutdown(self, force=False):
if self._process is not None:
shutdown(self._process, force)

@polling
def wait_for_readiness(self):
assert self.healthy

def healthy(self) -> bool:
try:
if self._process:
Expand All @@ -121,7 +128,14 @@ def healthy(self) -> bool:

def port(self) -> int:
master_ports = set(self._master.local_listening_ports())
master_ports.remove(self._worker.remote_established_ports()[0])
worker_ports = self._worker.remote_established_ports()
if len(worker_ports) != 1:
raise RuntimeError("Expected one remote port. Worker might have died.")
master_ports.remove(worker_ports[0])
if len(master_ports) != 1:
raise RuntimeError(
"Expected one remaining master port. Master might have died."
)
return int(list(master_ports)[0])

def join(self):
Expand Down
2 changes: 1 addition & 1 deletion h3daemon/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "h3daemon"
version = "0.16.5"
version = "0.16.6"
description = "HMMER server"
authors = ["Danilo Horta <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 9cad700

Please sign in to comment.