1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
-module(couch_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]).
-include("couch_db.hrl").
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) ->
?LOG_INFO("config_event handler ~p terminating with ~p", [St, Reason]),
ok.
code_change(_OldVsn, St, _Extra) ->
{ok, St}.
|