diff --git a/changelog/1063.bugfix.md b/changelog/1063.bugfix.md new file mode 100644 index 000000000..f22da8a4e --- /dev/null +++ b/changelog/1063.bugfix.md @@ -0,0 +1 @@ +Instantiate sanic application with `AppLoader` as recommended in the [documentation](https://sanic.dev/en/guide/running/app-loader.html#dynamic-applications). \ No newline at end of file diff --git a/rasa_sdk/endpoint.py b/rasa_sdk/endpoint.py index eb922c51a..fddbda63d 100644 --- a/rasa_sdk/endpoint.py +++ b/rasa_sdk/endpoint.py @@ -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 @@ -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...") @@ -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__": diff --git a/tests/test_plugin.py b/tests/test_plugin.py index c984f7e73..b76e10493 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -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