Sort out detecing worker running
This commit is contained in:
parent
c17484b0ec
commit
bedc434332
4 changed files with 37 additions and 31 deletions
|
@ -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__":
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
Reference in a new issue