Skip to content

Commit

Permalink
Merge pull request #1063 from RasaHQ/ATO-2016-fix-app-instantiation-e…
Browse files Browse the repository at this point in the history
…rror

[ATO-2016] Fix App Instantiation Error
  • Loading branch information
Tawakalt authored Jan 15, 2024
2 parents 531f3f4 + 5d195f8 commit 073db29
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/1063.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Instantiate sanic application with `AppLoader` as recommended in the [documentation](https://sanic.dev/en/guide/running/app-loader.html#dynamic-applications).
23 changes: 17 additions & 6 deletions rasa_sdk/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from ssl import SSLContext
from sanic import Sanic, response
from sanic.response import HTTPResponse
from functools import partial
from sanic.worker.loader import AppLoader

# catching:
# - all `pkg_resources` deprecation warning from multiple dependencies
Expand Down Expand Up @@ -178,12 +180,18 @@ def run(
) -> None:
"""Starts the action endpoint server with given config values."""
logger.info("Starting action endpoint server...")
app = create_app(
action_package_name,
cors_origins=cors_origins,
auto_reload=auto_reload,
tracer_provider=tracer_provider,
# as recommended by Sanic in their documentation
# https://sanic.dev/en/guide/running/app-loader.html#dynamic-applications
loader = AppLoader(
factory=partial(
create_app,
action_package_name,
cors_origins=cors_origins,
auto_reload=auto_reload,
tracer_provider=tracer_provider,
)
)
app = loader.load()
app.config.KEEP_ALIVE_TIMEOUT = keep_alive_timeout
## Attach additional sanic extensions: listeners, middleware and routing
logger.info("Starting plugins...")
Expand All @@ -193,7 +201,10 @@ def run(
host = os.environ.get("SANIC_HOST", "0.0.0.0")

logger.info(f"Action endpoint is up and running on {protocol}://{host}:{port}")
app.run(host, port, ssl=ssl_context, workers=utils.number_of_sanic_workers())
app.prepare(
host=host, port=port, ssl=ssl_context, workers=utils.number_of_sanic_workers()
)
Sanic.serve(primary=app, app_loader=loader)


if __name__ == "__main__":
Expand Down
4 changes: 4 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def test_plugin_attach_sanic_app_extension(
# Set the create_app() method to return create_app_mock
monkeypatch.setattr("rasa_sdk.endpoint.create_app", create_app_mock)

# mock sanic.serve
sanic_serve_mock = MagicMock()
monkeypatch.setattr("rasa_sdk.endpoint.Sanic.serve", sanic_serve_mock)

# Set the return value of app_mock.run() to None
app_mock.run.return_value = None

Expand Down

0 comments on commit 073db29

Please sign in to comment.