undeadlabs / discovery

An OTP application for auto-discovering services with Consul

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Callback when node has been restored

cjimison opened this issue · comments

Is there a way to get a callback for when a node of a given service has been restored? For example:

Node A is running service test_service
Node B starts and is Polling for nodes of type test_service
Node B sees Node A and connects via the Discovery.Handler.NodeConnect

Node A goes down (elixir app not consul)
Node B does not get a callback
Node A returns
Node B is not getting a callback (however the: [info] Connected to: name@route is printing so they are reconnecting)

Example code for Node B:

defmodule Sample do
    use Application
    require Logger

    def start(_type, _args) do
        import Supervisor.Spec, warn: false
        children = 
        [
            worker(Discovery.Poller, 
                ["test_service", [Discovery.Handler.NodeConnect, &discoveryCB/1]], 
                id: :test_service_id)
        ]
        opts = [strategy: :one_for_one, name: Sample.Supervisor]
        Supervisor.start_link(children, opts)
    end

    def discoveryCB(services) do
        Logger.debug("[Sample] services available = #{inspect services}")
    end
end

While getting a callback for when the Node goes down is not really a big deal, getting a callback for when a node returns would be VERY helpful.

NOTE: If both consul and elixir go down, callbacks are hit upon the return of the service.

@cjimison yes, but it's not super clear how to do it because I've got a bit of work to do in naming and documentation. This is what you are looking for:

defmodule MyApp.Supervisor do
  def init([]) do
    children = [
      worker(Discovery.Poller, ["my_service", [
        {Discovery.Handler.NodeConnect, [
          {MyEventHandler, []}
        ]}
      ]], id: :my_service),
    ]

    supervise(children, strategy: :one_for_one)
  end
end

defmodule MyEventHandler do
  use Discovery.NodeConnector.Handler

  def on_connect(node, service, state), do: state
  def on_disconnect(node, service, state), do: state
end

The event handler will fire each time a connect / disconnect event is fired

Bad ass, this is exactly what I needed to know! I saw the NodeConnector.Handler in the code base, just wasn’t sure how add my own via the publicly exposed API (and I am now happily reverting my hacks :).

Thanks for the help!

-Chris

On Apr 8, 2015, at 11:16 AM, Jamie Winsor notifications@github.com wrote:

defmodule MyEventHandler do
use Discovery.NodeConnector.Handler

def on_connect(node, service, state), do: state
def on_disconnect(node, service, state), do: state
end

@cjimison no problem at all, this is something I need to make more clear in the documentation!