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):
|
||||
if worker.is_running():
|
||||
ctx.fail("Worker already running")
|
||||
daemon = daemonize.Daemonize(
|
||||
"catfish", worker.PID_FILE, worker.main, 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))
|
||||
|
||||
if no_fork:
|
||||
worker.run()
|
||||
else:
|
||||
daemon = daemonize.Daemonize(
|
||||
"catfish", worker.PID_FILE, worker.run, 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))
|
||||
|
||||
|
||||
@cli.command()
|
||||
|
|
|
@ -3,6 +3,9 @@ import os
|
|||
import tempfile
|
||||
import psutil
|
||||
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")
|
||||
|
@ -33,6 +36,9 @@ def stop_worker():
|
|||
terminate_processes([get_running_process()])
|
||||
|
||||
|
||||
def main():
|
||||
while True:
|
||||
time.sleep(1)
|
||||
async def run_worker():
|
||||
loop = asyncio.get_running_loop()
|
||||
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,
|
||||
zip_safe=False,
|
||||
pathon_requires=">=3.6",
|
||||
install_requires=["click", "daemonize", "psutil"],
|
||||
install_requires=["click", "daemonize", "psutil", "aiohttp"],
|
||||
entry_points="""
|
||||
[console_scripts]
|
||||
ctf=catfish.__main__:cli
|
||||
|
|
Reference in a new issue