Skip to content

Instantly share code, notes, and snippets.

@seanlynch
Created March 5, 2010 02:02
Show Gist options
  • Save seanlynch/322375 to your computer and use it in GitHub Desktop.
Save seanlynch/322375 to your computer and use it in GitHub Desktop.
%% Processes supervised by this supervisor will only exist in one
%% place in a cluster. If it goes down on any node, some node will
%% start it back up again.
-module(global_singleton_supervisor).
-behaviour(supervisor).
%% API
-export([start_link/0]).
%% Supervisor callbacks
-export([init/1]).
start_link() ->
case supervisor:start_link({global, ?MODULE}, ?MODULE, []) of
{ok, Pid} -> {ok, Pid};
{error, {already_started, Pid}} ->
{ok, spawn_link(fun () -> watch(Pid) end)}
end.
watch(Pid) ->
process_flag(trap_exit, true),
erlang:monitor(process, Pid),
error_logger:info_msg("Monitoring global singleton at ~p~n", [Pid]),
receive
LikelyExit ->
error_logger:info_msg("Global singleton supervisor at ~p exited. Restarting.~n",
[Pid])
end.
init([]) ->
{ok,{{one_for_all, 5, 5},
[
%% Everything in here is run once per entire cluster. Be careful.
%% Put all your stuff to be supervised here.
]}}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment