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
|
import daemonize
|
||||||
|
|
||||||
from catfish import __version__, worker
|
from catfish import __version__, worker
|
||||||
|
from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
|
@ -22,23 +23,22 @@ def start(ctx, port, no_fork):
|
||||||
if worker.is_running():
|
if worker.is_running():
|
||||||
ctx.fail("Worker already running")
|
ctx.fail("Worker already running")
|
||||||
|
|
||||||
|
create_base_socket_dir()
|
||||||
|
|
||||||
if no_fork:
|
if no_fork:
|
||||||
worker.run(port)
|
return worker.run(port)
|
||||||
else:
|
daemon = daemonize.Daemonize(
|
||||||
daemon = daemonize.Daemonize(
|
"catfish", worker.PID_FILE, functools.partial(worker.run, port), verbose=True
|
||||||
"catfish",
|
)
|
||||||
worker.PID_FILE,
|
try:
|
||||||
functools.partial(worker.run, port),
|
# HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved
|
||||||
foreground=no_fork,
|
os._exit = sys.exit
|
||||||
)
|
daemon.start()
|
||||||
try:
|
except SystemExit:
|
||||||
# HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved
|
pass
|
||||||
os._exit = sys.exit
|
worker.wait_for_running_worker()
|
||||||
daemon.start()
|
proc = worker.get_running_process()
|
||||||
except SystemExit:
|
click.echo("Worker started with pid {}".format(proc.pid))
|
||||||
worker.wait_for_worker()
|
|
||||||
proc = worker.get_running_process()
|
|
||||||
click.echo("Worker started with pid {}".format(proc.pid))
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
@ -49,6 +49,7 @@ def stop(ctx):
|
||||||
proc = worker.get_running_process()
|
proc = worker.get_running_process()
|
||||||
click.echo("Terminating process {}".format(proc.pid))
|
click.echo("Terminating process {}".format(proc.pid))
|
||||||
worker.stop_worker()
|
worker.stop_worker()
|
||||||
|
delete_base_socket_dir()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -35,6 +35,11 @@ def is_process_running(pid: int) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def wait_for_process(pid: int):
|
def wait_for_process_start(pid: int):
|
||||||
while not is_process_running(pid):
|
while not is_process_running(pid):
|
||||||
time.sleep(0.1)
|
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():
|
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:
|
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():
|
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:
|
def get_running_process() -> psutil.Process:
|
||||||
assert is_running()
|
|
||||||
with open(PID_FILE) as f:
|
with open(PID_FILE) as f:
|
||||||
return psutil.Process(int(f.read()))
|
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():
|
def wait_for_running_worker():
|
||||||
while not all([os.path.exists(WORKER_SERVER_SOCKET)]):
|
while not is_running():
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue