# `ClusterHelper.EventHandler`
[🔗](https://github.com/ohhi-vn/cluster_helper/blob/main/lib/cluster_helper/event_handler.ex#L1)

Optional callback behaviour for `ClusterHelper` events.

Implement this behaviour in any module and set it in your config:

    config :cluster_helper, event_handler: MyApp.ClusterEvents

All callbacks are optional — implement only the ones you need.

## Example
    ```elixir
    defmodule MyApp.ClusterEvents do
      @behaviour ClusterHelper.EventHandler

      @impl true
      def on_role_added(node, role) do
        Logger.info("Node #{node} gained role #{inspect(role)}")
      end

      @impl true
      def on_role_removed(node, role) do
        Logger.info("Node #{node} lost role #{inspect(role)}")
      end

      @impl true
      def on_node_added(node) do
        Logger.info("New node joined the cluster: #{node}")
      end

      @impl true
      def on_node_removed(node) do
        Logger.info("Node left the cluster: #{node}")
      end
    end
    ```

# `on_node_added`
*optional* 

```elixir
@callback on_node_added(node :: node()) :: :ok
```

Called when a previously unknown node is discovered for the first time
(i.e. on the initial pull after it joins the cluster).

# `on_node_removed`
*optional* 

```elixir
@callback on_node_removed(node :: node()) :: :ok
```

Called when a node goes down and is removed from the cluster.

# `on_role_added`
*optional* 

```elixir
@callback on_role_added(node :: node(), role :: ClusterHelper.role()) :: :ok
```

Called whenever a role is inserted into the local ETS table for any node
(local or remote). Fired once per role, even when a batch is added.

`node` is the node that owns the role; `role` can be any Elixir term.

# `on_role_removed`
*optional* 

```elixir
@callback on_role_removed(node :: node(), role :: ClusterHelper.role()) :: :ok
```

Called whenever a role is removed from a node in the local ETS table.
Fired once per role, even when a batch is removed.

`node` is the node that lost the role; `role` can be any Elixir term.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
