1 %% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved.
3 %% This file is provided to you under the Apache License,
4 %% Version 2.0 (the "License"); you may not use this file
5 %% except in compliance with the License. You may obtain
6 %% a copy of the License at
8 %% http://www.apache.org/licenses/LICENSE-2.0
10 %% Unless required by applicable law or agreed to in writing,
11 %% software distributed under the License is distributed on an
12 %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 %% KIND, either express or implied. See the License for the
14 %% specific language governing permissions and limitations
17 %% @doc A process that does a gen_event:add_sup_handler and attempts to re-add
18 %% event handlers when they exit.
22 -module(webmachine_logger_watcher).
24 -behaviour(gen_server).
27 -include_lib("eunit/include/eunit.hrl").
38 -export([start_link/3,
41 -record(state, {module, config, event}).
43 start_link(Event, Module, Config) ->
44 gen_server:start_link(?MODULE, [Event, Module, Config], []).
46 start(Event, Module, Config) ->
47 gen_server:start(?MODULE, [Event, Module, Config], []).
49 init([Event, Module, Config]) ->
50 install_handler(Event, Module, Config),
51 {ok, #state{event=Event, module=Module, config=Config}}.
53 handle_call(_Call, _From, State) ->
56 handle_cast(_Request, State) ->
59 handle_info({gen_event_EXIT, Module, normal}, #state{module=Module} = State) ->
60 {stop, normal, State};
61 handle_info({gen_event_EXIT, Module, shutdown}, #state{module=Module} = State) ->
62 {stop, normal, State};
63 handle_info({gen_event_EXIT, Module, _Reason}, #state{module=Module,
64 config=Config, event=Event} = State) ->
65 install_handler(Event, Module, Config),
67 handle_info(reinstall_handler, #state{module=Module, config=Config, event=Event} = State) ->
68 install_handler(Event, Module, Config),
70 handle_info(_Info, State) ->
73 terminate(_Reason, _State) ->
76 code_change(_OldVsn, State, _Extra) ->
81 install_handler(Event, Module, Config) ->
82 case gen_event:add_sup_handler(Event, Module, Config) of
86 erlang:send_after(5000, self(), reinstall_handler),