summaryrefslogtreecommitdiff
path: root/src/mem3_sync.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/mem3_sync.erl')
-rw-r--r--src/mem3_sync.erl46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/mem3_sync.erl b/src/mem3_sync.erl
new file mode 100644
index 00000000..d50514d9
--- /dev/null
+++ b/src/mem3_sync.erl
@@ -0,0 +1,46 @@
+-module(mem3_sync).
+-behaviour(supervisor).
+
+-export([start_link/0, init/1, childspec/1, sup_upgrade_notify/2]).
+
+-include("mem3.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, 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.