Skip to content

Commit

Permalink
Use daemonize lib, instead of homebrewing.
Browse files Browse the repository at this point in the history
  • Loading branch information
brainbot-devops committed Aug 14, 2019
1 parent 93d8dab commit 910a9cb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 89 deletions.
40 changes: 17 additions & 23 deletions scenario_player/services/common/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,16 @@

import structlog
import waitress
from daemonize import Daemonize

from scenario_player.services.utils.factories import (
construct_flask_app,
default_service_daemon_cli,
start_daemon,
stop_daemon,
)

log = structlog.getLogger(__name__)


def serve_spaas_stack(logfile_path, host, port):
"""Run an RPC flask app as a daemonized process."""
logging.basicConfig(filename=logfile_path, filemode="a+", level=logging.DEBUG)
log = structlog.getLogger()

app = construct_flask_app()

log.info("Starting SPaaS Service Stack", host=host, port=port)
waitress.serve(app, host=host, port=port)


def service_daemon():
parser = default_service_daemon_cli()

Expand All @@ -36,15 +24,21 @@ def service_daemon():

PIDFILE = args.raiden_dir.joinpath("spaas", "service-stack.pid")

host, port = args.host, args.port

def serve_spaas_stack():
"""Run an RPC flask app as a daemonized process."""
logging.basicConfig(filename=logfile_path, filemode="a+", level=logging.DEBUG)
log = structlog.getLogger()

app = construct_flask_app()

log.info("Starting SPaaS Service Stack", host=host, port=port)
waitress.serve(app, host=host, port=port)

daemon = Daemonize("SPaaS-RPC", PIDFILE, serve_rpc)

if args.command == "start":
start_daemon(
PIDFILE,
serve_spaas_stack,
logfile_path,
args.host,
args.port,
stdout=logfile_path,
stderr=logfile_path,
)
daemon.start()
elif args.command == "stop":
stop_daemon(PIDFILE)
daemon.exit()
43 changes: 17 additions & 26 deletions scenario_player/services/rpc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import flask
import structlog
import waitress
from daemonize import Daemonize

from scenario_player.services.common.blueprints import admin_blueprint, metrics_blueprint
from scenario_player.services.rpc.blueprints import (
Expand All @@ -16,11 +17,7 @@
transactions_blueprint,
)
from scenario_player.services.rpc.utils import RPCRegistry
from scenario_player.services.utils.factories import (
default_service_daemon_cli,
start_daemon,
stop_daemon,
)
from scenario_player.services.utils.factories import default_service_daemon_cli


def rpc_app():
Expand Down Expand Up @@ -50,17 +47,6 @@ def rpc_app():
return app


def serve_rpc(logfile_path, host, port):
"""Run an RPC flask app as a daemonized process."""
logging.basicConfig(filename=logfile_path, filemode="a+", level=logging.DEBUG)
log = structlog.getLogger()

app = rpc_app()

log.info("Starting RPC Service", host=host, port=port)
waitress.serve(app, host=host, port=port)


def service_daemon():
parser = default_service_daemon_cli()

Expand All @@ -71,17 +57,22 @@ def service_daemon():
logfile_path = logfile_path.joinpath("SPaaS-RPC.log")
logfile_path.touch()

host, port = args.host, args.port

def serve_rpc():
"""Run an RPC flask app as a daemonized process."""
logging.basicConfig(filename=logfile_path, filemode="a+", level=logging.DEBUG)
log = structlog.getLogger()

app = rpc_app()

log.info("Starting RPC Service", host=host, port=port)
waitress.serve(app, host=host, port=port)

PIDFILE = args.raiden_dir.joinpath("spaas", "rpc-service.pid")

daemon = Daemonize("SPaaS-RPC", PIDFILE, serve_rpc)
if args.command == "start":
start_daemon(
PIDFILE,
serve_rpc,
logfile_path,
args.host,
args.port,
stdout=logfile_path,
stderr=logfile_path,
)
daemon.start()
elif args.command == "stop":
stop_daemon(PIDFILE)
daemon.exit()
22 changes: 0 additions & 22 deletions tests/unittests/services/common/test_app.py

This file was deleted.

18 changes: 0 additions & 18 deletions tests/unittests/services/rpc/test_app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import logging
from unittest.mock import patch

from scenario_player.services.rpc.app import (
admin_blueprint,
instances_blueprint,
metrics_blueprint,
rpc_app,
serve_rpc,
tokens_blueprint,
transactions_blueprint,
)
Expand All @@ -15,22 +13,6 @@
dummy_app = object()


@patch("scenario_player.services.rpc.app.rpc_app", return_value=dummy_app)
@patch("scenario_player.services.rpc.app.waitress.serve")
class TestServeRPC:
def test_calls_waitress_serve_with_args(self, mock_serve, _, tmp_path):
serve_rpc(tmp_path.joinpath("tetst.log"), "127.0.0.666", 1000)
mock_serve.assert_called_once_with(dummy_app, host="127.0.0.666", port=1000)

@patch("scenario_player.services.rpc.app.logging.basicConfig", autospec=True)
@patch("scenario_player.services.rpc.app.structlog.getLogger", autospec=True)
def test_configures_logging(self, mock_structlog, mock_logging, _, __, tmp_path):
logfile = tmp_path.joinpath("test.log")
serve_rpc(logfile, "127.0.0.1", 5000)
mock_structlog.assert_called_once()
mock_logging.assert_called_once_with(filename=logfile, filemode="a+", level=logging.DEBUG)


@patch("scenario_player.services.rpc.app.flask.Flask.register_blueprint")
def test_rpc_app_constructor(mock_register_bp):
app = rpc_app()
Expand Down

0 comments on commit 910a9cb

Please sign in to comment.