availability_group.api.get_node_state
()
Get the state of the node with respect to Availability groups.
Return type: NodeState
Returns: An instance of NodeState with the details of the node state.
Example:
from availability_group.api import (
HeartbeatMethod, NodeDnsConnectionState, NodeRole, get_node_state
)
state = get_node_state()
print(state)
# Sample output, broken into multiple lines for readability:
#
# NodeState(
# node_role='primary',
# node_dns_connection_state='connected,
# last_heartbeat_time="2022-11-24T04:31:32+00:00",
# heartbeat_method='direct'
# )
print(state.node_role == NodeRole.PRIMARY)
print(state.node_dns_connection_state == NodeDnsConnectionState.DIRECT)
print(state.last_heartbeat_time)
print(state.heartbeat_method == HeartbeatMethod.DIRECT)
Example of preventing a Secondary node from acting upon user request:
from availability_group.api import NodeRole, get_node_state
@route(app, "/workflow_name/path1")
def get_path1():
if get_node_state().node_role == NodeRole.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.