2018-12-13 18:01:40 +00:00
|
|
|
import functools
|
2018-12-12 18:47:40 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2018-12-12 15:53:56 +00:00
|
|
|
import click
|
|
|
|
import daemonize
|
|
|
|
|
|
|
|
from catfish import __version__, worker
|
2018-12-14 21:46:35 +00:00
|
|
|
from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir
|
2018-12-12 15:53:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
@click.version_option(__version__, prog_name="catfish")
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
2018-12-13 18:01:40 +00:00
|
|
|
@click.option("--port", default=8080, type=int)
|
2018-12-12 15:53:56 +00:00
|
|
|
@click.option("--no-fork", is_flag=True)
|
2018-12-12 18:47:40 +00:00
|
|
|
@click.pass_context
|
2018-12-13 18:01:40 +00:00
|
|
|
def start(ctx, port, no_fork):
|
2018-12-12 18:47:40 +00:00
|
|
|
if worker.is_running():
|
|
|
|
ctx.fail("Worker already running")
|
2018-12-12 20:03:45 +00:00
|
|
|
|
2018-12-14 21:46:35 +00:00
|
|
|
create_base_socket_dir()
|
|
|
|
|
2018-12-12 20:03:45 +00:00
|
|
|
if no_fork:
|
2018-12-14 21:46:35 +00:00
|
|
|
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))
|
2018-12-12 15:53:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
2018-12-12 18:47:40 +00:00
|
|
|
@click.pass_context
|
|
|
|
def stop(ctx):
|
2018-12-12 15:53:56 +00:00
|
|
|
if not worker.is_running():
|
2018-12-12 18:47:40 +00:00
|
|
|
ctx.fail("Worker not running")
|
2018-12-12 15:53:56 +00:00
|
|
|
proc = worker.get_running_process()
|
|
|
|
click.echo("Terminating process {}".format(proc.pid))
|
2018-12-12 18:47:40 +00:00
|
|
|
worker.stop_worker()
|
2018-12-14 21:46:35 +00:00
|
|
|
delete_base_socket_dir()
|
2018-12-12 15:53:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|