subscribe function - Platform - BlueCat Gateway - 24.1

Gateway Administration Guide

Product name
BlueCat Gateway

availability_group.api.subscribe(event_id, callback)

Register a function (or a callable object) to be invoked when a specified event occurs.

This can be useful to workflows that need to be aware of changes to an Availability group as they happen. A workflow should register an event handler using this subscribe function.

For example, a workflow should not perform tasks when the node that they are on is Secondary in the group.

Parameters Description
event_id (str)

The ID of the Availability group-related event for which you want to register a callback function.

callback (Callable)

The function to be called when the event happens.

Table 1. Events and parameters to callbacks
EventID Callback parameter Type of parameter
EventId.NODE_ROLE_CHANGED event_data NodeRoleChanged

Example of responding to a request only on Primary node:

from availability_group.api import EventId, NodeRole, NodeRoleChanged, subscribe

role_cached_in_my_workflow: str = None

# Define a custom handler for when the node role has changed.
def ag_hook(event: NodeRoleChanged, **kwargs):
    """Custom handling of the change of the node's role."""

    if event.previous:
        logger.debug(f"The node started with role '{}'.")
        logger.debug(f"The role changed from '{event.previous}' to '{}'.")

    global role_cached_in_my_workflow

    # Store the new role in a global variable for later use.
    role_cached_in_my_workflow =

    if == Role.NOT_IN_GROUP:
        # Perform the actions specific to when the node is not in a group.
    if == Role.PRIMARY:
        # Perform the actions specific to when the node becomes the Primary one in a group.
    if == Role.SECONDARY:
        # Perform the actions specific to when the node becomes the Secondary one in a group.

# Register the custom handler for the event of a change of the node's role.
subscribe(EventId.NODE_ROLE_CHANGED, ag_hook)

@route(app, "/workflow_name/path2")
def get_path2():
    global role_cached_in_my_workflow
    if role_cached_in_my_workflow == Role.SECONDARY:
        return jsonify({
            "result": "error",
            "message": "Secondary node should not perform the action."

    # This is reached on a Primary node or an instance that's not in a group.
    # Perform the action.

    return jsonify({"result": "success"})

New in version 22.11.1.