Ali Rajabi
Ali Rajabi

Reputation: 45

Phoenix framework distribution

I have a phoenix project that must be AP in the CAP theorem so I need to detect network partitions and ensure my node connects to at least one node.

My solution is a GenServer that calls a function every 30 secs and checks Node.list and if it is [] stop the node.

I have a questions

Is this the best solution or Erlang/OTP have a straight forward solution for that?

I searched and didn't find my answer on this document: http://erlang.org/doc/design_principles/distributed_applications.html

Upvotes: 0

Views: 147

Answers (1)

BurmajaM
BurmajaM

Reputation: 724

There's :global_group.monitor/1 function that can be used to get notification when some node in cluster went up or down:

defmodule NodeMonitor do
  def start_link do
    {:ok, spawn_link fn -> 
      :global_group.monitor_nodes true
      monitor()
    end}
  end

  def monitor do
    receive do
      {:nodeup, node}   -> Logger.info "NodeMonitor: #{node} joined"
      {:nodedown, node} -> Logger.warn "NodeMonitor: #{node} left"
    end
    monitor()
  end
end

Upvotes: 1

Related Questions