Permalink
Browse files

Merge pull request #54 from cloudant/14082-cleanup-db-update-listeners

BugzID: 14082
  • Loading branch information...
2 parents 1ba4140 + 141977e commit 15d00e7e82c3faccfd0a7baf6ad3985fe6676acf @kocolosk kocolosk committed Aug 1, 2012
Showing with 23 additions and 1 deletion.
  1. +23 −1 src/fabric_db_update_listener.erl
@@ -23,14 +23,15 @@ go(Parent, ParentRef, DbName, Timeout) ->
Notifiers = start_update_notifiers(DbName),
MonRefs = lists:usort([{rexi_server, Node} || {Node, _Ref} <- Notifiers]),
RexiMon = rexi_monitor:start(MonRefs),
+ MonPid = start_cleanup_monitor(self(), Notifiers),
%% Add calling controller node as rexi end point as this controller will
%% receive messages from it
Workers = [{Parent, ParentRef} | Notifiers],
try
receive_results(Workers, {Workers, Parent, unset}, Timeout)
after
rexi_monitor:stop(RexiMon),
- stop_update_notifiers(Notifiers)
+ stop_cleanup_monitor(MonPid)
end.
start_update_notifiers(DbName) ->
@@ -49,6 +50,27 @@ start_update_notifier(DbName) ->
rexi:reply({gen_event_EXIT, DbName, Reason})
end.
+start_cleanup_monitor(Parent, Notifiers) ->
+ spawn(fun() ->
+ Ref = erlang:monitor(process, Parent),
+ cleanup_monitor(Parent, Ref, Notifiers)
+ end).
+
+stop_cleanup_monitor(MonPid) ->
+ MonPid ! {self(), stop}.
+
+cleanup_monitor(Parent, Ref, Notifiers) ->
+ receive
+ {'DOWN', Ref, _, _, _} ->
+ stop_update_notifiers(Notifiers);
+ {Parent, stop} ->
+ stop_update_notifiers(Notifiers);
+ Else ->
+ twig:log(error, "Unkown message in ~w :: ~w", [?MODULE, Else]),
+ stop_update_notifiers(Notifiers),
+ exit(Parent, {unknown_message, Else})
+ end.
+
stop_update_notifiers(Notifiers) ->
[rexi:kill(Node, Ref) || {Node, Ref} <- Notifiers].

0 comments on commit 15d00e7

Please sign in to comment.