Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Kill couch_db_event when it has installed handlers
This is to force the upgrade switch to the new couch_event application
force update notifications. Theoretically we could accomplish the same
thing with custom appups to forcefully remove couch_db_event from the
supervision tree but it turns out that's really hard.
  • Loading branch information
davisp authored and rnewson committed Jul 30, 2014
1 parent 0547000 commit 022af54ff972e3ff7ce842f5c905dbfeb7d162f9
Showing 1 changed file with 28 additions and 2 deletions.
@@ -27,13 +27,18 @@
code_change/3
]).

-export([
watchdog/0
]).


-include("couch_event_int.hrl").


-record(st, {
by_pid,
by_dbname
by_dbname,
watchdog
}).


@@ -46,7 +51,8 @@ init(_) ->
{ok, ByDbName} = khash:new(),
{ok, #st{
by_pid = ByPid,
by_dbname = ByDbName
by_dbname = ByDbName,
watchdog = spawn_monitor(?MODULE, watchdog, [])
}}.


@@ -90,6 +96,10 @@ handle_cast(Msg, St) ->
{noreply, St}.


handle_info({'DOWN', Ref, _, _, Reason}, #st{watchdog={_,Ref}}=St) ->
couch_log:notice("~s watchdog died: ~w", [?MODULE, Reason]),
erlang:send_after(60000, self(), spawn_watchdog),
{noreply, St#st{watchdog=undefined}};
handle_info({'DOWN', Ref, process, Pid, _Reason}, St) ->
case khash:get(St#st.by_pid, Pid) of
{Ref, OldDbNames} ->
@@ -99,6 +109,10 @@ handle_info({'DOWN', Ref, process, Pid, _Reason}, St) ->
end,
{noreply, St};


handle_info(spawn_watchdog, #st{watchdog=undefined}=St) ->
NewWD = spawn_monitor(?MODULE, watchdog, []),
{noreply, St#st{watchdog=NewWD}};
handle_info(Msg, St) ->
couch_log:notice("~s ignoring info ~w", [?MODULE, Msg]),
{noreply, St}.
@@ -108,6 +122,18 @@ code_change(_OldVsn, St, _Extra) ->
{ok, St}.


watchdog() ->
Handlers = gen_event:which_handlers(couch_db_update),
case length(Handlers) > 0 of
true ->
exit(whereis(couch_db_update), force_upgrade);
false ->
ok
end,
timer:sleep(5000),
?MODULE:watchdog().


notify_listeners(ByDbName, DbName, Event) ->
Msg = {'$couch_event', DbName, Event},
notify_listeners(khash:get(ByDbName, all_dbs), Msg),

0 comments on commit 022af54

Please sign in to comment.