Skip to content

Commit

Permalink
Refactor Daemon context and remove unused files 🛠️
Browse files Browse the repository at this point in the history
- Updated `__init__.py` to simplify imports and __all__ definition.
- Changed `start` function in `cli.py` for parameter renaming.
- Refactored `daemon.py` to streamline context handling and added new `spawn` function.
- Deleted unused `daemonize.py` and `possess.py` files to clean up the codebase.
- Modified test in `test_daemon.py` to remove unnecessary debug file operations.
  • Loading branch information
horta committed Jan 22, 2025
1 parent fb39e66 commit 87a7065
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 99 deletions.
7 changes: 2 additions & 5 deletions h3daemon/h3daemon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from h3daemon.daemon import Daemon, daemon_context
from h3daemon.daemonize import spawn
from h3daemon.ensure_pressed import ensure_pressed
from h3daemon.possess import possess
from h3daemon.daemon import context, possess, spawn

__all__ = ["Daemon", "ensure_pressed", "spawn", "possess", "daemon_context"]
__all__ = ["spawn", "possess", "context"]
4 changes: 2 additions & 2 deletions h3daemon/h3daemon/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typer import echo

from h3daemon.daemon import Daemon
from h3daemon.daemonize import spawn
from h3daemon import spawn
from h3daemon.pidfile import create_pidfile
from h3daemon import possess

Expand Down Expand Up @@ -56,7 +56,7 @@ def start(
ferr = open(stderr, "w+") if stderr else stderr
spawn(
hmm,
cport=port,
port=port,
stdin=fin,
stdout=fout,
stderr=ferr,
Expand Down
75 changes: 68 additions & 7 deletions h3daemon/h3daemon/daemon.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
from contextlib import contextmanager, suppress
from multiprocessing import Process
from typing import Any, Optional

import psutil
from daemon import DaemonContext
from deciphon_schema import HMMFile
from pidlockfile import PIDLockFile
from tenacity import retry, stop_after_delay, wait_exponential

from h3daemon.debug import debug_exception, debug_message
from h3daemon.ensure_pressed import ensure_pressed
from h3daemon.errors import ChildNotFoundError, PIDNotFoundError
from h3daemon.errors import (
ChildNotFoundError,
DaemonAlreadyRunningError,
PIDNotFoundError,
)
from h3daemon.master import Master
from h3daemon.portfile import read_portfile
from h3daemon.pidfile import create_pidfile
from h3daemon.port import find_free_port
from h3daemon.portfile import create_portfile, read_portfile
from h3daemon.worker import Worker

__all__ = ["Daemon", "daemon_context"]
__all__ = ["Daemon", "context"]


def shutdown(x: psutil.Process, *, force: bool, wait: bool):
Expand Down Expand Up @@ -137,10 +146,62 @@ def join(self):


@contextmanager
def daemon_context(hmmfile: HMMFile, cport: int = 0, wport: int = 0):
x = Daemon.create(hmmfile, cport, wport)
def context(hmmfile: HMMFile, port: int = 0):
x = Daemon.create(hmmfile, port, find_free_port())
try:
x.wait_for_readiness()
yield x
yield x.port()
finally:
x.shutdown()


def daemonize(
hmmfile: HMMFile,
port: int = 0,
stdin: Optional[Any] = None,
stdout: Optional[Any] = None,
stderr: Optional[Any] = None,
detach: Optional[bool] = None,
):
pidfile = create_pidfile(hmmfile)
assert pidfile.is_locked() is None
with DaemonContext(
working_directory=str(hmmfile.path.parent),
pidfile=pidfile,
detach_process=detach,
stdin=stdin,
stdout=stdout,
stderr=stderr,
):
port = find_free_port() if port == 0 else port
wport = find_free_port()
create_portfile(pidfile, port, wport)
x = Daemon.create(hmmfile, port, wport)
x.join()


def spawn(
hmmfile: HMMFile,
port: int = 0,
stdin: Optional[Any] = None,
stdout: Optional[Any] = None,
stderr: Optional[Any] = None,
detach: Optional[bool] = None,
force: Optional[bool] = False,
):
pidfile = create_pidfile(hmmfile)
if pidfile.is_locked():
if not force:
raise DaemonAlreadyRunningError(f"Daemon for {hmmfile} is already running.")
x = Daemon.possess(pidfile)
x.shutdown(force=force)

args = (hmmfile, port, stdin, stdout, stderr, detach)
p = Process(target=daemonize, args=args, daemon=False)
p.start()
return pidfile


def possess(pidfile: PIDLockFile, wait=True):
if wait:
return Daemon.possess_wait(pidfile)
return Daemon.possess(pidfile)
62 changes: 0 additions & 62 deletions h3daemon/h3daemon/daemonize.py

This file was deleted.

11 changes: 0 additions & 11 deletions h3daemon/h3daemon/possess.py

This file was deleted.

16 changes: 4 additions & 12 deletions h3daemon/tests/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@
@pytest.mark.repeat(30)
def test_hmmer(tmp_path, files_path: Path):
os.chdir(tmp_path)
debug_file = Path(tmp_path / "h3daemon_debug.txt")
debug_file.touch()

shutil.copy(files_path / "minifam.hmm", Path("minifam.hmm"))

try:
hmmfile = HMMFile(path=Path("minifam.hmm"))
pidfile = h3daemon.spawn(hmmfile, detach=True, force=True)
daemon = h3daemon.possess(pidfile, wait=True)
daemon.shutdown()
finally:
with open(debug_file, "r") as f:
print(f.read())
hmmfile = HMMFile(path=Path("minifam.hmm"))
pidfile = h3daemon.spawn(hmmfile, detach=True, force=True)
daemon = h3daemon.possess(pidfile, wait=True)
daemon.shutdown()

0 comments on commit 87a7065

Please sign in to comment.