Update handle_config_terminate API

COUCHDB-3102
diff --git a/src/couch_index.erl b/src/couch_index.erl
index b6a6c99..fdc7246 100644
--- a/src/couch_index.erl
+++ b/src/couch_index.erl
@@ -12,9 +12,8 @@
 
 -module(couch_index).
 -behaviour(gen_server).
--behaviour(config_listener).
 
--vsn(1).
+-vsn(2).
 
 %% API
 -export([start_link/1, stop/1, get_state/2, get_info/1]).
@@ -26,15 +25,12 @@
 -export([init/1, terminate/2, code_change/3]).
 -export([handle_call/3, handle_cast/2, handle_info/2]).
 
-% config_listener api
--export([handle_config_change/5, handle_config_terminate/3]).
-
 
 -include_lib("couch/include/couch_db.hrl").
 
 
 -define(CHECK_INTERVAL, 600000). % 10 minutes
-
+-define(RELISTEN_DELAY, 5000).
 
 -record(st, {
     mod,
@@ -89,7 +85,7 @@
 
 
 init({Mod, IdxState}) ->
-    ok = config:listen_for_changes(?MODULE, nil),
+    ok = config:subscribe_for_changes([{"query_server_config", "commit_freq"}]),
     DbName = Mod:get(db_name, IdxState),
     erlang:send_after(?CHECK_INTERVAL, self(), maybe_close),
     Resp = couch_util:with_db(DbName, fun(Db) ->
@@ -344,25 +340,19 @@
     Args = [Mod:get(db_name, IdxState), Mod:get(idx_name, IdxState)],
     couch_log:info("Index shutdown by monitor notice for db: ~s idx: ~s", Args),
     catch send_all(State#st.waiters, shutdown),
-    {stop, normal, State#st{waiters=[]}}.
-
+    {stop, normal, State#st{waiters=[]}};
+handle_info({config_change, "query_server_config", "commit_freq", NewDelay, _}, State) ->
+    handle_cast({config_change, NewDelay}, State);
+handle_info({gen_event_EXIT, _Handler, _Reason}, State) ->
+    erlang:send_after(?RELISTEN_DELAY, self(), restart_config_listener),
+    {noreply, State};
+handle_info(restart_config_listener, State) ->
+    ok = config:subscribe_for_changes([{"query_server_config", "commit_freq"}]),
+    {noreply, State}.
 
 code_change(_OldVsn, State, _Extra) ->
     {ok, State}.
 
-
-handle_config_change("query_server_config", "commit_freq", Val, _, _) ->
-    {ok, gen_server:cast(?MODULE, {config_update, Val})};
-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;
 maybe_restart_updater(#st{mod=Mod, idx_state=IdxState}=State) ->
diff --git a/src/couch_index_server.erl b/src/couch_index_server.erl
index ac2c609..4e86f5e 100644
--- a/src/couch_index_server.erl
+++ b/src/couch_index_server.erl
@@ -14,7 +14,7 @@
 -behaviour(gen_server).
 -behaviour(config_listener).
 
--vsn(1).
+-vsn(2).
 
 -export([start_link/0, validate/2, get_index/4, get_index/3, get_index/2]).
 
@@ -33,7 +33,7 @@
 -define(BY_SIG, couchdb_indexes_by_sig).
 -define(BY_PID, couchdb_indexes_by_pid).
 -define(BY_DB, couchdb_indexes_by_db).
-
+-define(RELISTEN_DELAY, 5000).
 
 -record(st, {root_dir}).
 
@@ -115,7 +115,7 @@
 
 init([]) ->
     process_flag(trap_exit, true),
-    ok = config:listen_for_changes(?MODULE, nil),
+    ok = config:listen_for_changes(?MODULE, couch_index_util:root_dir()),
     ets:new(?BY_SIG, [protected, set, named_table]),
     ets:new(?BY_PID, [private, set, named_table]),
     ets:new(?BY_DB, [protected, bag, named_table]),
@@ -175,6 +175,9 @@
             ok
     end,
     {noreply, Server};
+handle_info(restart_config_listener, State) ->
+    ok = config:listen_for_changes(?MODULE, couch_index_util:root_dir()),
+    {noreply, State};
 handle_info(Msg, State) ->
     couch_log:warning("~p did not expect ~p", [?MODULE, Msg]),
     {noreply, State}.
@@ -197,13 +200,11 @@
 handle_config_change(_, _, _, _, RootDir) ->
     {ok, RootDir}.
 
-handle_config_terminate(_Server, stop, _State) -> ok;
+handle_config_terminate(_, stop, _) ->
+    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).
+    erlang:send_after(?RELISTEN_DELAY, whereis(?MODULE), restart_config_listener),
+    {ok, couch_index_util:root_dir()}.
 
 new_index({Mod, IdxState, DbName, Sig}) ->
     DDocId = Mod:get(idx_name, IdxState),