Created
March 5, 2010 02:02
-
-
Save seanlynch/322375 to your computer and use it in GitHub Desktop.
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
%% 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