Rasa Actions Startup and Shutdown Code

So, I got stuck on where to write the startup and shutdown code for rasa actions server. Here’s the code and instruction so no one else gets stuck. Create a directory called rasa_sdk_plugins inside the root directory not inside actions/

create __init__.py file

rasa_sdk_plugins/_init_.py

import pluggy # MUST be imported

def init_hooks(manager): # no type hint if pluggy import fails

"""Initialise hooks into Rasa SDK."""

import sys

import rasa_sdk_plugins.lifecycle

manager.register(sys.modules\["rasa_sdk_plugins.lifecycle"\])

Here lifecycle is your model name

Then create a module.py file lifecycle.py in this case import logging

import threading

from sanic import Sanic

from pluggy import HookimplMarker

LOGGER = logging.getLogger(_name_)

hookimpl = HookimplMarker(“rasa_sdk”)

@hookimpl

def attach_sanic_app_extensions(app: Sanic):

"""Attach Sanic app extensions for lifecycle management."""

LOGGER.info("Registering Sanic listeners")

app.register_listener(before_server_start, "before_server_start")

app.register_listener(after_server_stop, "after_server_stop")

LOGGER.info("Sanic listeners registered")

async def before_server_start(app: Sanic, loop):

#Code Here

async def after_server_stop(app: Sanic, loop):

# Code Here