Skip to content

Commit

Permalink
Merge remote branch 'cloudant:3102-fix-config_subscription'
Browse files Browse the repository at this point in the history
This closes #4

Signed-off-by: ILYA Khlopotov <iilyak@ca.ibm.com>
  • Loading branch information
iilyak committed Aug 23, 2016
2 parents c7c75eb + 126a849 commit ba99ec7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/ioq.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
% config_listener api
-export([handle_config_change/5, handle_config_terminate/3]).

-define(RELISTEN_DELAY, 5000).

-record(state, {
concurrency,
ratio,
Expand Down Expand Up @@ -83,6 +85,9 @@ handle_info({'DOWN', Ref, _, _, Reason}, State) ->
false ->
{noreply, State, 0}
end;
handle_info(restart_config_listener, State) ->
ok = config:listen_for_changes(?MODULE, nil),
{noreply, State};
handle_info(timeout, State) ->
{noreply, maybe_submit_request(State)}.

Expand All @@ -91,13 +96,10 @@ handle_config_change("ioq", _, _, _, _) ->
handle_config_change(_, _, _, _, _) ->
{ok, nil}.

handle_config_terminate(_, stop, _) -> ok;
handle_config_terminate(_, _, _) ->
spawn(fun() ->
timer:sleep(5000),
config:listen_for_changes(?MODULE, nil)
end),
ok.
handle_config_terminate(_Server, stop, _State) ->
ok;
handle_config_terminate(_Server, _Reason, _State) ->
erlang:send_after(?RELISTEN_DELAY, whereis(?MODULE), restart_config_listener).

code_change(_Vsn, State, _Extra) ->
{ok, State}.
Expand Down
23 changes: 22 additions & 1 deletion src/ioq_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
-module(ioq_sup).
-behaviour(supervisor).
-export([start_link/0, init/1]).
-export([handle_config_change/5, handle_config_terminate/3]).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
Expand All @@ -21,4 +22,24 @@ start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
{ok, { {one_for_one, 5, 10}, [?CHILD(ioq, worker)]}}.
{ok, { {one_for_one, 5, 10}, [
{
config_listener_mon,
{config_listener_mon, start_link, [?MODULE, nil]},
permanent,
5000,
worker,
[config_listener_mon]
},
?CHILD(ioq, worker)
]} }.

handle_config_change("ioq", _Key, _Val, _Persist, St) ->
gen_server:cast(ioq_server, update_config),
{ok, St};
handle_config_change(_Sec, _Key, _Val, _Persist, St) ->
{ok, St}.

handle_config_terminate(_Server, _Reason, _State) ->
gen_server:cast(ioq_server, update_config),
ok.

0 comments on commit ba99ec7

Please sign in to comment.