Repository: couchdb-peruser Updated Branches: refs/heads/master ff7d19097 -> 39ef15a28
Update handle_config_terminate API COUCHDB-3102 Project: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/commit/ed3ad6fc Tree: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/tree/ed3ad6fc Diff: http://git-wip-us.apache.org/repos/asf/couchdb-peruser/diff/ed3ad6fc Branch: refs/heads/master Commit: ed3ad6fc97e10bdae73e87b36f8c2446d5bf2fcf Parents: ff7d190 Author: ILYA Khlopotov <[email protected]> Authored: Mon Aug 22 15:10:44 2016 -0700 Committer: ILYA Khlopotov <[email protected]> Committed: Mon Aug 22 15:10:44 2016 -0700 ---------------------------------------------------------------------- src/couch_peruser.erl | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-peruser/blob/ed3ad6fc/src/couch_peruser.erl ---------------------------------------------------------------------- diff --git a/src/couch_peruser.erl b/src/couch_peruser.erl index f20b979..d637916 100644 --- a/src/couch_peruser.erl +++ b/src/couch_peruser.erl @@ -12,7 +12,6 @@ -module(couch_peruser). -behaviour(gen_server). --behaviour(config_listener). -include_lib("couch/include/couch_db.hrl"). @@ -22,13 +21,12 @@ -export([start_link/0, init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -% config_listener callbacks --export([handle_config_change/5, handle_config_terminate/3]). - -export([init_changes_handler/1, changes_handler/3]). -record(state, {parent, db_name, delete_dbs, changes_pid, changes_ref}). +-define(RELISTEN_DELAY, 5000). + start_link() -> gen_server:start_link(?MODULE, [], []). @@ -168,7 +166,7 @@ user_db_name(User) -> %% gen_server callbacks init([]) -> - ok = config:listen_for_changes(?MODULE, self()), + ok = subscribe_for_changes(), {ok, init()}. handle_call(_Msg, _From, State) -> @@ -188,9 +186,29 @@ handle_cast(_Msg, State) -> handle_info({'DOWN', Ref, _, _, _Reason}, #state{changes_ref=Ref} = State) -> {stop, normal, State}; +handle_info({config_change, "couch_peruser", _, _}, State) -> + handle_cast(update_config, State); +handle_info({config_change, "couch_httpd_auth", "authentication_db", _}, State) -> + handle_cast(update_config, State); +handle_info({gen_event_EXIT, _Handler, _Reason}, State) -> + erlang:send_after(?RELISTEN_DELAY, self(), restart_config_listener), + {noreply, State}; +handle_info({'EXIT', _Pid, _Reason}, State) -> + erlang:send_after(?RELISTEN_DELAY, self(), restart_config_listener), + {noreply, State}; +handle_info(restart_config_listener, State) -> + ok = subscribe_for_changes(), + {noreply, State}; handle_info(_Msg, State) -> {noreply, State}. +subscribe_for_changes() -> + config:subscribe_for_changes([ + {"couch_httpd_auth", "authentication_db"}, + "couch_peruser" + ]). + + terminate(_Reason, _State) -> %% Everything should be linked or monitored, let nature %% take its course. @@ -198,18 +216,3 @@ terminate(_Reason, _State) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. - - -%% config_listener callbacks - -handle_config_change("couch_httpd_auth", "authentication_db", _Value, _Persist, Server) -> - ok = gen_server:cast(Server, update_config), - {ok, Server}; -handle_config_change("couch_peruser", _Key, _Value, _Persist, Server) -> - ok = gen_server:cast(Server, update_config), - {ok, Server}; -handle_config_change(_Section, _Key, _Value, _Persist, Server) -> - {ok, Server}. - -handle_config_terminate(_Self, Reason, _Server) -> - {stop, Reason}.
