Skip to content

Commit

Permalink
improve rasa up server startup and thread handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tmbo committed Apr 21, 2019
1 parent bf01cfb commit 09e8a07
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ venv
.pytype
dist/
data/*
pip-wheel-metadata
!data/examples
!data/test
!data/README.md
Expand Down
71 changes: 47 additions & 24 deletions rasa/cli/up.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import signal
import sys
from multiprocessing import Process
from multiprocessing import get_context
from typing import List, Text

import rasa.cli.run
Expand All @@ -14,13 +14,17 @@ def add_subparser(
):
from rasa.core import cli

shell_parser = subparsers.add_parser(
"up",
parents=parents,
conflict_handler="resolve",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
help="Run the Rasa Interface",
)
up_parser_args ={
"parents": parents,
"conflict_handler": "resolve",
"formatter_class": argparse.ArgumentDefaultsHelpFormatter,
}

if is_rasa_x_installed():
# only if rasa x is installed, we show the command on the CLI
up_parser_args["help"] = "Start Rasa X and the Interface"

shell_parser = subparsers.add_parser("up", **up_parser_args)

shell_parser.add_argument(
"--production", action="store_true", help="Run Rasa in a production environment"
Expand Down Expand Up @@ -68,15 +72,27 @@ def add_subparser(
cli.arguments.add_logging_option_arguments(shell_parser)


def start_event_service():
from rasa_platform.services.event_service import main
def _event_service():
# noinspection PyUnresolvedReferences
from rasa_platform.community.services.event_service import main

main()


def start_core(args: argparse.Namespace, endpoints: "AvailableEndpoints" = None):
def start_event_service():
ctx = get_context("spawn")
p = ctx.Process(target=_event_service)
p.start()


def _core_service(args: argparse.Namespace, endpoints: "AvailableEndpoints" = None):
"""Starts the Rasa Core application."""
from rasa.core.run import serve_application
from rasa.nlu.utils import configure_colored_logging

configure_colored_logging(args.loglevel)
logging.getLogger("rasa.core.agent").setLevel(logging.ERROR)
logging.getLogger("apscheduler.executors.default").setLevel(logging.WARNING)

if endpoints is None:
from rasa.core.utils import AvailableEndpoints
Expand Down Expand Up @@ -121,19 +137,29 @@ def start_core_for_local_platform(args: argparse.Namespace, platform_token: Text
)
)

p = Process(target=start_core, args=(args, endpoints))
ctx = get_context("spawn")
p = ctx.Process(target=_core_service, args=(args, endpoints))
p.start()


def is_rasa_x_installed():
try:
# noinspection PyUnresolvedReferences
import rasa_platform.community
return True
except:
return False


def up(args: argparse.Namespace):
from rasa.cli.utils import print_success, print_error, signal_handler
from rasa.core.utils import configure_file_logging
from rasa.utils.io import configure_colored_logging

signal.signal(signal.SIGINT, signal_handler)

logging.getLogger("werkzeug").setLevel(logging.WARN)
logging.getLogger("engineio").setLevel(logging.WARN)
logging.getLogger("werkzeug").setLevel(logging.WARNING)
logging.getLogger("engineio").setLevel(logging.WARNING)
logging.getLogger("socketio").setLevel(logging.ERROR)

if not args.vvvv:
Expand All @@ -152,25 +178,22 @@ def up(args: argparse.Namespace):
configure_file_logging(args.loglevel, args.log_file)

if args.production:
print_success("Starting Rasa Core")
start_core(args)
print_success("Starting Rasa X in production mode... 🚀")
_core_service(args)
else:
print_success("Starting Rasa X in local mode... 🚀")
try:
from rasa_platform import config
from rasa_platform.api.local import main_local
from rasa_platform.community import config
from rasa_platform.community.api.local import main_local
except ImportError as e:
print_error(
"Rasa X is not installed. The `rasa up` "
"command requires an installation of Rasa X."
"command requires an installation of Rasa X. "
"Error:\n{}".format(e)
)
sys.exit()

print_success("Starting Rasa 🚀")

p = Process(target=start_event_service)
p.start()

start_event_service()
start_core_for_local_platform(args, config.platform_token)

main_local(args.project_path, args.data_path)
24 changes: 11 additions & 13 deletions rasa/core/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ async def _pull_model_and_fingerprint(
return resp.headers.get("ETag")

except aiohttp.ClientError as e:
logger.warning(
logger.info(
"Tried to fetch model from server, but "
"couldn't reach server. We'll retry later... "
"Error: {}.".format(e)
Expand All @@ -189,18 +189,16 @@ async def _pull_model_and_fingerprint(
async def _run_model_pulling_worker(
model_server: EndpointConfig, wait_time_between_pulls: int, agent: "Agent"
) -> None:
while True:
# noinspection PyBroadException
try:
await asyncio.sleep(wait_time_between_pulls)
await _update_model_from_server(model_server, agent)
except CancelledError:
logger.warning("Stopping model pulling (cancelled).")
except Exception:
logger.exception(
"An exception was raised while fetching "
"a model. Continuing anyways..."
)
# noinspection PyBroadException
try:
await _update_model_from_server(model_server, agent)
except CancelledError:
logger.warning("Stopping model pulling (cancelled).")
except Exception:
logger.exception(
"An exception was raised while fetching "
"a model. Continuing anyways..."
)


async def schedule_model_pulling(
Expand Down
2 changes: 1 addition & 1 deletion rasa/core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def create_app(
@app.listener("after_server_start")
async def warn_if_agent_is_unavailable(app, loop):
if not app.agent or not app.agent.is_ready():
logger.warning(
logger.info(
"The loaded agent is not ready to be used yet "
"(e.g. only the NLU interpreter is configured, "
"but no Core model is loaded). This is NOT AN ISSUE "
Expand Down

0 comments on commit 09e8a07

Please sign in to comment.