-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Daemon management and improve code structure 🛠️
- Deleted h3daemon/TODO.txt file. - Renamed h3daemon/manager.py to h3daemon/daemon.py. - Updated imports in __init__.py, cli.py to reflect new module updates. - Introduced h3daemon/daemonize.py and h3daemon/possess.py for better clarity. - Refactored daemon management logic for improved reliability. - Enhanced error handling with DaemonAlreadyRunningError, PIDNotFoundError. - Introduced Polling class for better retry logic than tenacity. - Simplified health checks by separating logic into h3daemon/healthy.py. This comprehensive overhaul streamlines the daemon process management, enhances code maintainability, and removes legacy dependencies. By modularizing components and introducing robust polling mechanisms, the code now supports more efficient daemon controls and error handling.
- Loading branch information
Showing
12 changed files
with
199 additions
and
125 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from h3daemon.daemon import Daemon | ||
from h3daemon.daemonize import spawn | ||
from h3daemon.ensure_pressed import ensure_pressed | ||
from h3daemon.possess import possess | ||
|
||
__all__ = ["Daemon", "ensure_pressed", "spawn", "possess"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from multiprocessing import Process | ||
from typing import Any, Optional | ||
|
||
from daemon import DaemonContext | ||
from deciphon_schema import HMMFile | ||
|
||
from h3daemon.daemon import Daemon | ||
from h3daemon.ensure_pressed import ensure_pressed | ||
from h3daemon.errors import DaemonAlreadyRunningError | ||
from h3daemon.pidfile import create_pidfile | ||
|
||
__all__ = ["daemonize", "spawn"] | ||
|
||
|
||
def daemonize( | ||
hmmfile: HMMFile, | ||
cport: int = 0, | ||
wport: int = 0, | ||
stdin: Optional[Any] = None, | ||
stdout: Optional[Any] = None, | ||
stderr: Optional[Any] = None, | ||
detach: Optional[bool] = None, | ||
): | ||
ensure_pressed(hmmfile) | ||
fin = open(stdin, "r") if stdin else stdin | ||
fout = open(stdout, "w+") if stdout else stdout | ||
ferr = open(stderr, "w+") if stderr else stderr | ||
|
||
pidfile = create_pidfile(hmmfile.path) | ||
assert pidfile.is_locked() is None | ||
with DaemonContext( | ||
working_directory=str(hmmfile.path.parent), | ||
pidfile=pidfile, | ||
detach_process=True, | ||
stdin=fin, | ||
stdout=fout, | ||
stderr=ferr, | ||
): | ||
x = Daemon.spawn(hmmfile, cport, wport) | ||
x.join() | ||
|
||
return pidfile | ||
|
||
|
||
def spawn( | ||
hmmfile: HMMFile, | ||
cport: int = 0, | ||
wport: int = 0, | ||
stdin: Optional[Any] = None, | ||
stdout: Optional[Any] = None, | ||
stderr: Optional[Any] = None, | ||
force: Optional[bool] = False, | ||
): | ||
pidfile = create_pidfile(hmmfile.path) | ||
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, cport, wport, stdin, stdout, stderr) | ||
p = Process(target=daemonize, args=args, daemon=True) | ||
p.start() | ||
p.join() | ||
return pidfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
__all__ = ["ChildNotFoundError"] | ||
__all__ = ["ChildNotFoundError", "DaemonAlreadyRunningError", "PIDNotFoundError"] | ||
|
||
|
||
class ChildNotFoundError(RuntimeError): | ||
def __str__(self): | ||
return repr(self) | ||
|
||
|
||
class DaemonAlreadyRunningError(RuntimeError): | ||
def __str__(self): | ||
return repr(self) | ||
|
||
|
||
class PIDNotFoundError(RuntimeError): | ||
def __str__(self): | ||
return repr(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from h3daemon.master import Master | ||
from h3daemon.worker import Worker | ||
|
||
|
||
def assert_peers_healthy(master: Master, worker: Worker): | ||
assert master.healthy() | ||
assert worker.healthy() | ||
master_listen = master.local_listening_ports() | ||
master_lport = master.local_established_ports() | ||
master_rport = master.remote_established_ports() | ||
worker_lport = worker.local_established_ports() | ||
worker_rport = worker.remote_established_ports() | ||
|
||
assert len(master_lport) == 1 | ||
assert len(worker_rport) == 1 | ||
assert master_lport[0] == worker_rport[0] | ||
assert len(master_rport) == 1 | ||
assert len(worker_lport) == 1 | ||
assert master_rport[0] == worker_lport[0] | ||
assert len(master_listen) == 2 | ||
master_ports = set(master_listen) | ||
assert len(master_ports) == 2 | ||
master_ports.remove(worker_rport[0]) | ||
assert len(master_ports) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.