Action Handlers

Action handlers describe how your service mutates its internal state in response to the arrival of an action from the queue. They are defined as a function of two arguments: action_type and payload. Action_type is a string that classifies the event and payload is a dictionary representing the data associated with the event. For example,

def action_handler(action_type, payload, properties):
    # if the payload represents a new recipe to add to the list
    if action_type == 'create_recipe':
        # create a new instance of the recipe model
        recipe = Recipe(**payload)
        # save the new model
        recipe.save()
    # otherwise if the payload is the id of a recipe to be deleted
    elif action_type == 'delete_recipe':
        # find the matching recipe
        recipe = Recipe.query.first(Recipe.id == payload)
        # remove the recipe from the database
        recipe.remove()

Action Handlers are defined within the service:

class MyActionHandler(nautilus.network.ActionHandler):
    async def handle_action(self, action_type, payload, props, **kwds):
        print("recieved action!")

class MyService(Service):
    action_handler = MyActionHandler

Reusing and Combining Action Handlers

As your services get more complex, you’ll want to split your action handler into separate functions which each get called with the given arguments. It can get tedious to pass the arguments to every function so Nautilus provides a function called combine_action_handlers which serves just this purpose:

nautilus.network.events.combine_action_handlers(*handlers)[source]

This function combines the given action handlers into a single function which will call all of them.

from nautilus.network import combine_action_handlers

def action_handler1(action_type, payload, properties):
    print("first handler fired!")

def action_handler2(action_type, payload, properties):
    print("second handler fired!")

combined_handler = combine_action_handlers(
    action_handler1,
    action_handler2
)

Using it in an Action Handler looks something like:

from nautilus.network import combine_action_handlers, ActionHandler

class MyActionHandler(ActionHandler):

    async def handle_action(self, *args, **kwds):
        # assuming action_handlers 1 and 2 were defined as above
        combined = combine_action_handlers(
            action_handler1,
            action_handler2
        )
        # call the combined handler
        combined(*args, **kwds)

Provided Action Handlers

Nautilus provides some action handlers to mix with your own services when creating custom solutions.

Factories

The following are functions that take a paramter and return an an action creator.

nautilus.network.events.actionHandlers.crud_handler(Model, name=None, **kwds)[source]

This action handler factory reaturns an action handler that responds to actions with CRUD types (following nautilus conventions) and performs the necessary mutation on the model’s database.

Parameters:Model (nautilus.BaseModel) – The model to delete when the action received.
Returns:function – The action handler for this model
Return type:type, payload
nautilus.network.events.actionHandlers.create_handler(Model, name=None, **kwds)[source]

This factory returns an action handler that creates a new instance of the specified model when a create action is recieved, assuming the action follows nautilus convetions.

Parameters:Model (nautilus.BaseModel) – The model to create when the action received.
Returns:function – The action handler for this model
Return type:action_type, payload
nautilus.network.events.actionHandlers.read_handler(Model, name=None, **kwds)[source]

This factory returns an action handler that responds to read requests by resolving the payload as a graphql query against the internal schema.

Parameters:Model (nautilus.BaseModel) – The model to delete when the action received.
Returns:function – The action handler for this model
Return type:type, payload
nautilus.network.events.actionHandlers.update_handler(Model, name=None, **kwds)[source]

This factory returns an action handler that updates a new instance of the specified model when a update action is recieved, assuming the action follows nautilus convetions.

Parameters:Model (nautilus.BaseModel) – The model to update when the action received.
Returns:function – The action handler for this model
Return type:type, payload
nautilus.network.events.actionHandlers.delete_handler(Model, name=None, **kwds)[source]

This factory returns an action handler that deletes a new instance of the specified model when a delete action is recieved, assuming the action follows nautilus convetions.

Parameters:Model (nautilus.BaseModel) – The model to delete when the action received.
Returns:function – The action handler for this model
Return type:type, payload
nautilus.network.events.actionHandlers.roll_call_handler(service, action_type, payload, props, **kwds)[source]

This action handler responds to the “roll call” emitted by the api gateway when it is brought up with the normal summary produced by the service.