Merge remote-tracking branch 'iilyak/2561-make-config-API-consistent'

This closes #4

COUCHDB-2561
diff --git a/src/couch_index.erl b/src/couch_index.erl
index b44d351..63a6bf9 100644
--- a/src/couch_index.erl
+++ b/src/couch_index.erl
@@ -14,6 +14,8 @@
 -behaviour(gen_server).
 -behaviour(config_listener).
 
+-vsn(1).
+
 %% API
 -export([start_link/1, stop/1, get_state/2, get_info/1]).
 -export([trigger_update/2]).
@@ -25,7 +27,7 @@
 -export([handle_call/3, handle_cast/2, handle_info/2]).
 
 % config_listener api
--export([handle_config_change/5]).
+-export([handle_config_change/5, handle_config_terminate/3]).
 
 
 -include_lib("couch/include/couch_db.hrl").
@@ -297,13 +299,6 @@
 handle_cast(_Mesg, State) ->
     {stop, unhandled_cast, State}.
 
-
-handle_info({gen_event_EXIT, {config_listener, ?MODULE}, _Reason}, State) ->
-    erlang:send_after(5000, self(), restart_config_listener),
-    {noreply, State};
-handle_info(restart_config_listener, State) ->
-    ok = config:listen_for_changes(?MODULE, nil),
-    {noreply, State};
 handle_info(commit, #st{committed=true}=State) ->
     {noreply, State};
 handle_info(commit, State) ->
@@ -367,6 +362,12 @@
 handle_config_change(_, _, _, _, _) ->
     {ok, nil}.
 
+handle_config_terminate(_Server, stop, _State) -> ok;
+handle_config_terminate(_Server, _Reason, _State) ->
+    spawn(fun() ->
+        timer:sleep(5000),
+        config:listen_for_changes(?MODULE, nil)
+    end).
 
 maybe_restart_updater(#st{waiters=[]}) ->
     ok;
diff --git a/src/couch_index_server.erl b/src/couch_index_server.erl
index 76adb6a..b8c29f0 100644
--- a/src/couch_index_server.erl
+++ b/src/couch_index_server.erl
@@ -14,6 +14,8 @@
 -behaviour(gen_server).
 -behaviour(config_listener).
 
+-vsn(1).
+
 -export([start_link/0, validate/2, get_index/4, get_index/3, get_index/2]).
 
 -export([init/1, terminate/2, code_change/3]).
@@ -22,6 +24,7 @@
 % Exported for callbacks
 -export([
     handle_config_change/5,
+    handle_config_terminate/3,
     handle_db_event/3
 ]).
 
@@ -163,13 +166,6 @@
     reset_indexes(DbName, State#st.root_dir),
     {noreply, State}.
 
-
-handle_info({gen_event_EXIT, {config_listener, ?MODULE}, _Reason}, State) ->
-    erlang:send_after(5000, self(), restart_config_listener),
-    {noreply, State};
-handle_info(restart_config_listener, State) ->
-    ok = config:listen_for_changes(?MODULE, State#st.root_dir),
-    {noreply, State};
 handle_info({'EXIT', Pid, Reason}, Server) ->
     case ets:lookup(?BY_PID, Pid) of
         [{Pid, {DbName, Sig}}] ->
@@ -204,6 +200,14 @@
 handle_config_change(_, _, _, _, RootDir) ->
     {ok, RootDir}.
 
+handle_config_terminate(_Server, stop, _State) -> ok;
+handle_config_terminate(_Server, _Reason, _State) ->
+    State = couch_index_util:root_dir(),
+    spawn(fun() ->
+        timer:sleep(5000),
+        config:listen_for_changes(?MODULE, State)
+    end).
+
 new_index({Mod, IdxState, DbName, Sig}) ->
     DDocId = Mod:get(idx_name, IdxState),
     case couch_index:start_link({Mod, IdxState}) of