Init server
This commit is contained in:
parent
96c52a0328
commit
81789ec29e
4 changed files with 39 additions and 15 deletions
|
@ -19,17 +19,21 @@ def cli():
|
||||||
def start(ctx, no_fork):
|
def start(ctx, no_fork):
|
||||||
if worker.is_running():
|
if worker.is_running():
|
||||||
ctx.fail("Worker already running")
|
ctx.fail("Worker already running")
|
||||||
daemon = daemonize.Daemonize(
|
|
||||||
"catfish", worker.PID_FILE, worker.main, foreground=no_fork
|
if no_fork:
|
||||||
)
|
worker.run()
|
||||||
try:
|
else:
|
||||||
# HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved
|
daemon = daemonize.Daemonize(
|
||||||
os._exit = sys.exit
|
"catfish", worker.PID_FILE, worker.run, foreground=no_fork
|
||||||
daemon.start()
|
)
|
||||||
except SystemExit:
|
try:
|
||||||
worker.wait_for_worker()
|
# HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved
|
||||||
proc = worker.get_running_process()
|
os._exit = sys.exit
|
||||||
click.echo("Worker started with pid {}".format(proc.pid))
|
daemon.start()
|
||||||
|
except SystemExit:
|
||||||
|
worker.wait_for_worker()
|
||||||
|
proc = worker.get_running_process()
|
||||||
|
click.echo("Worker started with pid {}".format(proc.pid))
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
|
|
@ -3,6 +3,9 @@ import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import psutil
|
import psutil
|
||||||
from catfish.utils.processes import terminate_processes
|
from catfish.utils.processes import terminate_processes
|
||||||
|
import asyncio
|
||||||
|
from .router import run_server
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
|
||||||
PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid")
|
PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid")
|
||||||
|
@ -33,6 +36,9 @@ def stop_worker():
|
||||||
terminate_processes([get_running_process()])
|
terminate_processes([get_running_process()])
|
||||||
|
|
||||||
|
|
||||||
def main():
|
async def run_worker():
|
||||||
while True:
|
loop = asyncio.get_running_loop()
|
||||||
time.sleep(1)
|
await asyncio.gather(run_server(loop))
|
||||||
|
|
||||||
|
|
||||||
|
run = partial(asyncio.run, run_worker())
|
||||||
|
|
14
catfish/worker/router.py
Normal file
14
catfish/worker/router.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import click
|
||||||
|
from aiohttp import web
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_request(request):
|
||||||
|
return web.json_response({})
|
||||||
|
|
||||||
|
|
||||||
|
async def run_server(loop, port=8080):
|
||||||
|
click.echo("Starting server...")
|
||||||
|
aiohttp_server = web.Server(handle_request)
|
||||||
|
aio_server = await loop.create_server(aiohttp_server, "0.0.0.0", port)
|
||||||
|
click.echo("Server listening on port {}".format(port))
|
||||||
|
await aio_server.serve_forever()
|
2
setup.py
2
setup.py
|
@ -11,7 +11,7 @@ setup(
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
pathon_requires=">=3.6",
|
pathon_requires=">=3.6",
|
||||||
install_requires=["click", "daemonize", "psutil"],
|
install_requires=["click", "daemonize", "psutil", "aiohttp"],
|
||||||
entry_points="""
|
entry_points="""
|
||||||
[console_scripts]
|
[console_scripts]
|
||||||
ctf=catfish.__main__:cli
|
ctf=catfish.__main__:cli
|
||||||
|
|
Reference in a new issue