Allow running the worker on a custom port

This commit is contained in:
Jake Howard 2018-12-13 18:01:40 +00:00
parent cd8cae18c5
commit c2b6c38d37
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 21 additions and 8 deletions

View file

@ -1,3 +1,4 @@
import functools
import os import os
import sys import sys
@ -14,17 +15,21 @@ def cli():
@cli.command() @cli.command()
@click.option("--port", default=8080, type=int)
@click.option("--no-fork", is_flag=True) @click.option("--no-fork", is_flag=True)
@click.pass_context @click.pass_context
def start(ctx, no_fork): def start(ctx, port, no_fork):
if worker.is_running(): if worker.is_running():
ctx.fail("Worker already running") ctx.fail("Worker already running")
if no_fork: if no_fork:
worker.run() worker.run(port)
else: else:
daemon = daemonize.Daemonize( daemon = daemonize.Daemonize(
"catfish", worker.PID_FILE, worker.run, foreground=no_fork "catfish",
worker.PID_FILE,
functools.partial(worker.run, port),
foreground=no_fork,
) )
try: try:
# HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved # HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved

View file

@ -48,5 +48,5 @@ async def run_worker(port):
await asyncio.gather(start_router(loop, port), start_server()) await asyncio.gather(start_router(loop, port), start_server())
def run(port=8080): def run(port):
return asyncio.run(run_worker(port)) return asyncio.run(run_worker(port))

View file

@ -5,7 +5,6 @@ from aiohttp.test_utils import unused_port
from aiounittest import AsyncTestCase from aiounittest import AsyncTestCase
from click.testing import CliRunner from click.testing import CliRunner
from catfish import worker
from catfish.__main__ import cli from catfish.__main__ import cli
from catfish.utils.processes import CURRENT_PROCESS, terminate_processes from catfish.utils.processes import CURRENT_PROCESS, terminate_processes
from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir
@ -16,13 +15,11 @@ class BaseTestCase(AsyncTestCase):
def setUp(self): def setUp(self):
create_base_socket_dir() create_base_socket_dir()
worker.stop_worker()
self.cli_runner = CliRunner() self.cli_runner = CliRunner()
self.cli = cli self.cli = cli
self.run_cli = functools.partial(self.cli_runner.invoke, self.cli) self.run_cli = functools.partial(self.cli_runner.invoke, self.cli)
def tearDown(self): def tearDown(self):
worker.stop_worker()
terminate_processes(CURRENT_PROCESS.children(recursive=True)) terminate_processes(CURRENT_PROCESS.children(recursive=True))
delete_base_socket_dir() delete_base_socket_dir()
@ -31,7 +28,9 @@ class BaseWorkerTestCase(BaseTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.unused_port = unused_port() self.unused_port = unused_port()
self.assertEqual(self.run_cli(["start"]).exit_code, 0) self.assertEqual(
self.run_cli(["start", "--port", self.unused_port]).exit_code, 0
)
def tearDown(self): def tearDown(self):
self.assertEqual(self.run_cli(["stop"]).exit_code, 0) self.assertEqual(self.run_cli(["stop"]).exit_code, 0)

View file

@ -1,3 +1,5 @@
from aiohttp.test_utils import unused_port
from catfish import __version__, worker from catfish import __version__, worker
from tests import BaseTestCase from tests import BaseTestCase
@ -18,6 +20,13 @@ class WorkerControlTestCase(BaseTestCase):
worker_process = worker.get_running_process() worker_process = worker.get_running_process()
self.assertIn(str(worker_process.pid), result.output) self.assertIn(str(worker_process.pid), result.output)
def test_starts_worker_custom_port(self):
port = unused_port()
result = self.run_cli(["start", "--port", port])
self.assertEqual(result.exit_code, 0)
worker_process = worker.get_running_process()
self.assertEqual(worker_process.connections("inet4")[0].laddr.port, port)
def test_stops_worker(self): def test_stops_worker(self):
result = self.run_cli(["start"]) result = self.run_cli(["start"])
self.assertEqual(result.exit_code, 0) self.assertEqual(result.exit_code, 0)