From bedc4343323d469b556e50478778758ce787ccab Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 14 Dec 2018 21:46:35 +0000 Subject: [PATCH] Sort out detecing worker running --- catfish/__main__.py | 33 +++++++++++++++++---------------- catfish/utils/processes.py | 7 ++++++- catfish/utils/sockets.py | 5 ++++- catfish/worker/__init__.py | 23 ++++++++++------------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/catfish/__main__.py b/catfish/__main__.py index 5e8b963..2a18e35 100644 --- a/catfish/__main__.py +++ b/catfish/__main__.py @@ -6,6 +6,7 @@ import click import daemonize from catfish import __version__, worker +from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir @click.group() @@ -22,23 +23,22 @@ def start(ctx, port, no_fork): if worker.is_running(): ctx.fail("Worker already running") + create_base_socket_dir() + if no_fork: - worker.run(port) - else: - daemon = daemonize.Daemonize( - "catfish", - worker.PID_FILE, - functools.partial(worker.run, port), - foreground=no_fork, - ) - try: - # HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved - os._exit = sys.exit - daemon.start() - except SystemExit: - worker.wait_for_worker() - proc = worker.get_running_process() - click.echo("Worker started with pid {}".format(proc.pid)) + return worker.run(port) + daemon = daemonize.Daemonize( + "catfish", worker.PID_FILE, functools.partial(worker.run, port), verbose=True + ) + try: + # HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved + os._exit = sys.exit + daemon.start() + except SystemExit: + pass + worker.wait_for_running_worker() + proc = worker.get_running_process() + click.echo("Worker started with pid {}".format(proc.pid)) @cli.command() @@ -49,6 +49,7 @@ def stop(ctx): proc = worker.get_running_process() click.echo("Terminating process {}".format(proc.pid)) worker.stop_worker() + delete_base_socket_dir() if __name__ == "__main__": diff --git a/catfish/utils/processes.py b/catfish/utils/processes.py index 595db9b..fbe83cd 100644 --- a/catfish/utils/processes.py +++ b/catfish/utils/processes.py @@ -35,6 +35,11 @@ def is_process_running(pid: int) -> bool: return False -def wait_for_process(pid: int): +def wait_for_process_start(pid: int): while not is_process_running(pid): time.sleep(0.1) + + +def wait_for_process_terminate(pid: int): + while is_process_running(pid): + time.sleep(0.1) diff --git a/catfish/utils/sockets.py b/catfish/utils/sockets.py index eb52a26..3be14b5 100644 --- a/catfish/utils/sockets.py +++ b/catfish/utils/sockets.py @@ -35,7 +35,10 @@ def create_base_socket_dir(): def delete_base_socket_dir(): - shutil.rmtree(BASE_SOCKET_DIR) + try: + shutil.rmtree(BASE_SOCKET_DIR) + except FileNotFoundError: + pass def stdout_socket_for_pid(pid: int) -> str: diff --git a/catfish/worker/__init__.py b/catfish/worker/__init__.py index 28479a3..7f032d8 100644 --- a/catfish/worker/__init__.py +++ b/catfish/worker/__init__.py @@ -14,27 +14,24 @@ PID_FILE = os.path.join(BASE_SOCKET_DIR, "catfish.pid") def is_running(): - return os.path.exists(PID_FILE) + if not os.path.exists(PID_FILE): + return False + if not os.path.exists(WORKER_SERVER_SOCKET): + return False + try: + get_running_process() + except ValueError: + return False + return True def get_running_process() -> psutil.Process: - assert is_running() with open(PID_FILE) as f: return psutil.Process(int(f.read())) -def wait_for_worker(): - while True: - try: - get_running_process() - return - except ValueError: - pass - time.sleep(0.1) - - def wait_for_running_worker(): - while not all([os.path.exists(WORKER_SERVER_SOCKET)]): + while not is_running(): time.sleep(0.1)