Created
January 2, 2017 15:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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