Skip to content

Instantly share code, notes, and snippets.

@mrrooijen
Created January 2, 2017 15:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrrooijen/ad2b331f2b8674eb10a8100c5803d98d to your computer and use it in GitHub Desktop.
Save mrrooijen/ad2b331f2b8674eb10a8100c5803d98d to your computer and use it in GitHub Desktop.
Ensure that only one instance of a given (supervised) process exists in the cluster.
defmodule Party.Clock do
use GenServer
def start_link do
case GenServer.start_link(__MODULE__, [], name: {:global, __MODULE__}) do
{:ok, pid} ->
{:ok, pid}
{:error, {:already_started, pid}} ->
IO.puts("Already started!!")
Process.link(pid)
{:ok, pid}
end
end
end
defmodule Party.Clock.Supervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_) do
children = [supervisor(Party.Clock, [])]
supervise(children, strategy: :one_for_one)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment