diff options
author | Brad Anderson <brad@cloudant.com> | 2010-05-28 16:50:35 -0400 |
---|---|---|
committer | Brad Anderson <brad@cloudant.com> | 2010-05-28 16:50:35 -0400 |
commit | 916871eb58001d8f261edeb838f6839dbc195303 (patch) | |
tree | 76846eaa8be2b6491ea4b9c7ed6c2054d2433917 /src/dbs.erl | |
parent | 4c6b7c7c12ba03e5b50d7379ab14cb0ba0037965 (diff) |
begin move of dynomite to membership
Diffstat (limited to 'src/dbs.erl')
-rw-r--r-- | src/dbs.erl | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/dbs.erl b/src/dbs.erl new file mode 100644 index 00000000..b5e17b6a --- /dev/null +++ b/src/dbs.erl @@ -0,0 +1,46 @@ +-module(dbs). +-behaviour(supervisor). + +-export([start_link/0, init/1, childspec/1, sup_upgrade_notify/2]). + +-include("membership.hrl"). + +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +init([]) -> + {ok, MemNodes} = mem3:nodes(), + LiveNodes = nodes(), + ChildSpecs = [childspec(N) || N <- MemNodes, lists:member(N, LiveNodes)], + %gen_event:add_handler(membership_events, showroom_dbs_event, []), + {ok, {{one_for_one, 10, 8}, ChildSpecs}}. + +childspec(Node) -> + ?LOG_INFO("dbs repl ~p --> ~p starting", [node(), Node]), + PostBody = {[ + {<<"source">>, <<"dbs">>}, + {<<"target">>, {[{<<"node">>, Node}, {<<"name">>, <<"dbs">>}]}}, + {<<"continuous">>, true} + ]}, + Id = couch_util:to_hex(erlang:md5(term_to_binary([node(), Node]))), + MFA = {couch_rep, start_link, [Id, PostBody, #user_ctx{}]}, + {Node, MFA, permanent, 100, worker, [couch_rep]}. + +% from http://code.google.com/p/erlrc/wiki/ErlrcHowto +sup_upgrade_notify (_Old, _New) -> + {ok, {_, Specs}} = init([]), + + Old = sets:from_list( + [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]), + New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]), + Kill = sets:subtract(Old, New), + + sets:fold(fun(Id, ok) -> + supervisor:terminate_child(?MODULE, Id), + supervisor:delete_child(?MODULE, Id), + ok + end, + ok, + Kill), + [supervisor:start_child (?MODULE, Spec) || Spec <- Specs ], + ok. |