Skip to content

Commit

Permalink
Refactor shutdown with wait parameter 🎯
Browse files Browse the repository at this point in the history
- Updated `shutdown` function to include `wait` parameter in `daemon.py`.
- Adjusted all `shutdown` calls to utilize the new `wait` parameter.
- Modified `pyproject.toml` by removing `pytest-xdist`.

Introducing the `wait` parameter enhances shutdown control, improving
process management by allowing optional wait times. The removal of
`pytest-xdist` aims to simplify the development dependencies, ensuring
maintenance alignment with actual project needs.
  • Loading branch information
horta committed Dec 6, 2024
1 parent 02e8aa8 commit 259a232
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
24 changes: 13 additions & 11 deletions h3daemon/h3daemon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
__all__ = ["Daemon", "daemon_context"]


def shutdown(x: psutil.Process, force: bool):
def shutdown(x: psutil.Process, *, force: bool, wait: bool):
with suppress(psutil.NoSuchProcess):
if force:
x.kill()
x.wait()
if wait:
x.wait()
else:
x.terminate()
try:
x.wait(3)
if wait:
x.wait(5)
except psutil.TimeoutExpired:
shutdown(x, True)
shutdown(x, force=True, wait=wait)


class Daemon:
Expand Down Expand Up @@ -63,9 +65,9 @@ def spawn(cls, hmmfile: HMMFile, cport: int = 0, wport: int = 0):
except Exception as exception:
debug_exception(exception)
if worker:
shutdown(worker.process, force=True)
shutdown(worker.process, force=True, wait=False)
if master:
shutdown(master.process, force=True)
shutdown(master.process, force=True, wait=False)
raise exception

return cls(master, worker, None)
Expand Down Expand Up @@ -99,14 +101,14 @@ def possess(cls, pidfile: PIDLockFile):
return cls(master, worker, process)

def shutdown(self, force=False):
if self._worker is not None:
shutdown(self._worker.process, True)

if self._master is not None:
shutdown(self._master.process, True)
shutdown(self._master.process, force=force, wait=False)

if self._worker is not None:
shutdown(self._worker.process, force=force, wait=False)

if self._process is not None:
shutdown(self._process, force)
shutdown(self._process, force=force, wait=True)

debug_message("Daemon.shutdown finished")

Expand Down
1 change: 0 additions & 1 deletion h3daemon/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ cli = ["typer"]
[tool.poetry.group.dev.dependencies]
pytest-repeat = "^0.9.3"
pytest = "^8.3.4"
pytest-xdist = "^3.6.1"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 259a232

Please sign in to comment.