You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2014/01/17 22:31:15 UTC
[12/28] git commit: Refactor config listening into custom behaviour
Refactor config listening into custom behaviour
Project: http://git-wip-us.apache.org/repos/asf/couchdb-config/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-config/commit/d221cb48
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-config/tree/d221cb48
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-config/diff/d221cb48
Branch: refs/heads/import
Commit: d221cb48603c2a9ad6720dbb219ef40f8f90a771
Parents: bc181f1
Author: Adam Kocoloski <ad...@cloudant.com>
Authored: Fri Jan 18 15:31:34 2013 -0500
Committer: Adam Kocoloski <ad...@cloudant.com>
Committed: Fri Jan 18 15:31:34 2013 -0500
----------------------------------------------------------------------
src/config.erl | 11 +++------
src/config_event.erl | 55 --------------------------------------------
src/config_listener.erl | 38 ++++++++++++++++++++++++++++++
src/config_sup.erl | 2 +-
4 files changed, 42 insertions(+), 64 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/d221cb48/src/config.erl
----------------------------------------------------------------------
diff --git a/src/config.erl b/src/config.erl
index cd93583..127dc2a 100644
--- a/src/config.erl
+++ b/src/config.erl
@@ -21,7 +21,7 @@
-export([start_link/1, stop/0]).
-export([all/0, get/1, get/2, get/3, set/3, set/4, delete/2, delete/3]).
--export([register/1, register/2]).
+-export([listen_for_changes/1]).
-export([parse_ini_file/1]).
-export([init/1, terminate/2, code_change/3]).
@@ -80,13 +80,8 @@ delete(Section, Key, Persist) when is_binary(Section) and is_binary(Key) ->
delete(Section, Key, Persist) ->
gen_server:call(?MODULE, {delete, Section, Key, Persist}).
-
-register(Fun) ->
- ?MODULE:register(Fun, self()).
-
-register(Fun, Pid) ->
- config_event:register(Fun, Pid).
-
+listen_for_changes(CallbackModule) ->
+ config_listener:start(CallbackModule).
init(IniFiles) ->
ets:new(?MODULE, [named_table, set, protected]),
http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/d221cb48/src/config_event.erl
----------------------------------------------------------------------
diff --git a/src/config_event.erl b/src/config_event.erl
deleted file mode 100644
index 193d055..0000000
--- a/src/config_event.erl
+++ /dev/null
@@ -1,55 +0,0 @@
-% Licensed under the Apache License, Version 2.0 (the "License"); you may not
-% use this file except in compliance with the License. You may obtain a copy of
-% the License at
-%
-% http://www.apache.org/licenses/LICENSE-2.0
-%
-% Unless required by applicable law or agreed to in writing, software
-% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-% License for the specific language governing permissions and limitations under
-% the License.
-
--module(config_event).
--behaviour(gen_event).
--export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2,
- code_change/3]).
-
--export([start_link/0, register/2]).
-
-start_link() ->
- gen_event:start_link({local, ?MODULE}).
-
-register(Fun, Pid) ->
- gen_event:add_handler(?MODULE, {?MODULE, Fun}, [Fun, Pid]).
-
-init([Fun, Pid]) ->
- Ref = erlang:monitor(process, Pid),
- {ok, {Fun, Ref}}.
-
-handle_event({config_change,Sec,_,_,_}, {F,_}=St) when is_function(F,1) ->
- catch F(Sec),
- {ok, St};
-handle_event({config_change,Sec,K,_,_}, {F,_}=St) when is_function(F,2) ->
- catch F(Sec,K),
- {ok, St};
-handle_event({config_change,Sec,K,V,_}, {F,_}=St) when is_function(F,3) ->
- catch F(Sec,K,V),
- {ok, St};
-handle_event({config_change,Sec,K,V,Write}, {F,_}=St) when is_function(F,4) ->
- catch F(Sec,K,V,Write),
- {ok, St}.
-
-handle_call(_Request, St) ->
- {ok, ok, St}.
-
-handle_info({'DOWN', Ref, _, _, _}, {_, Ref}) ->
- remove_handler;
-handle_info(_Info, St) ->
- {ok, St}.
-
-terminate(_Reason, _St) ->
- ok.
-
-code_change(_OldVsn, St, _Extra) ->
- {ok, St}.
http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/d221cb48/src/config_listener.erl
----------------------------------------------------------------------
diff --git a/src/config_listener.erl b/src/config_listener.erl
new file mode 100644
index 0000000..91134da
--- /dev/null
+++ b/src/config_listener.erl
@@ -0,0 +1,38 @@
+-module(config_listener).
+
+-behaviour(gen_event).
+
+%% Public interface
+-export([start/1]).
+
+-export([behaviour_info/1]).
+
+%% Required gen_event interface
+-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2,
+ code_change/3]).
+
+behaviour_info(callbacks) ->
+ [{handle_config_change/4}];
+behaviour_info(_) ->
+ undefined.
+
+start(Module) ->
+ gen_event:add_sup_handler(config_event, {?MODULE, Module}, [Module]).
+
+init(Module) ->
+ {ok, Module}.
+
+handle_event({config_change, Sec, Key, Value, Persist}, Module) ->
+ Module:handle_config_change(Sec, Key, Value, Persist).
+
+handle_call(_Request, Module) ->
+ {ok, ignored, St}.
+
+handle_info(_Info, Module) ->
+ {ok, St}.
+
+terminate(_Reason, Module) ->
+ ok.
+
+code_change(_OldVsn, Module, _Extra) ->
+ {ok, St}.
http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/d221cb48/src/config_sup.erl
----------------------------------------------------------------------
diff --git a/src/config_sup.erl b/src/config_sup.erl
index 779b589..a595b3c 100644
--- a/src/config_sup.erl
+++ b/src/config_sup.erl
@@ -40,7 +40,7 @@ init(IniFiles) ->
[config]
},
{config_event,
- {config_event, start_link, []},
+ {gen_event, start_link, [{local, config_event}]},
permanent,
5000,
worker,