|
|
@ -1,40 +1,22 @@ |
|
|
|
%% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved. |
|
|
|
%% |
|
|
|
%% This file is provided to you 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. |
|
|
|
|
|
|
|
%% @doc A process that does a gen_event:add_sup_handler and attempts to re-add |
|
|
|
%% event handlers when they exit. |
|
|
|
|
|
|
|
%% @private |
|
|
|
|
|
|
|
-module(rumHleWatcherSrv). |
|
|
|
|
|
|
|
-behaviour(gen_server). |
|
|
|
|
|
|
|
-include("rum.hrl"). |
|
|
|
-behaviour(gen_srv). |
|
|
|
|
|
|
|
-ifdef(TEST). |
|
|
|
-include_lib("eunit/include/eunit.hrl"). |
|
|
|
-export([pop_until/2]). |
|
|
|
-endif. |
|
|
|
-include("eRum.hrl"). |
|
|
|
|
|
|
|
%% callbacks |
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, |
|
|
|
code_change/3]). |
|
|
|
-export([ |
|
|
|
start/3 |
|
|
|
, start_link/3 |
|
|
|
]). |
|
|
|
|
|
|
|
-export([start_link/3, start/3]). |
|
|
|
-export([ |
|
|
|
init/1 |
|
|
|
, handleCall/3 |
|
|
|
, handleCast/2 |
|
|
|
, handleInfo/2 |
|
|
|
, terminate/2, |
|
|
|
code_change/3 |
|
|
|
]). |
|
|
|
|
|
|
|
-record(state, { |
|
|
|
module :: atom(), |
|
|
@ -53,17 +35,17 @@ init([Sink, Module, Config]) -> |
|
|
|
install_handler(Sink, Module, Config), |
|
|
|
{ok, #state{sink = Sink, module = Module, config = Config}}. |
|
|
|
|
|
|
|
handle_call(_Call, _From, State) -> |
|
|
|
handleCall(_Call, State, _From) -> |
|
|
|
{reply, ok, State}. |
|
|
|
|
|
|
|
handle_cast(_Request, State) -> |
|
|
|
handleCast(_Request, State) -> |
|
|
|
{noreply, State}. |
|
|
|
|
|
|
|
handle_info({gen_event_EXIT, Module, normal}, #state{module = Module} = State) -> |
|
|
|
handleInfo({gen_event_EXIT, Module, normal}, #state{module = Module} = State) -> |
|
|
|
{stop, normal, State}; |
|
|
|
handle_info({gen_event_EXIT, Module, shutdown}, #state{module = Module} = State) -> |
|
|
|
handleInfo({gen_event_EXIT, Module, shutdown}, #state{module = Module} = State) -> |
|
|
|
{stop, normal, State}; |
|
|
|
handle_info({gen_event_EXIT, Module, {'EXIT', {kill_me, [_KillerHWM, KillerReinstallAfter]}}}, |
|
|
|
handleInfo({gen_event_EXIT, Module, {'EXIT', {kill_me, [_KillerHWM, KillerReinstallAfter]}}}, |
|
|
|
#state{module = Module, sink = Sink, config = Config} = State) -> |
|
|
|
%% Brutally kill the manager but stay alive to restore settings. |
|
|
|
%% |
|
|
@ -77,10 +59,9 @@ handle_info({gen_event_EXIT, Module, {'EXIT', {kill_me, [_KillerHWM, KillerReins |
|
|
|
exit(SinkPid, kill), |
|
|
|
_ = timer:apply_after(KillerReinstallAfter, lager_app, start_handler, [Sink, Module, Config]), |
|
|
|
{stop, normal, State}; |
|
|
|
handle_info({gen_event_EXIT, Module, Reason}, #state{module = Module, |
|
|
|
handleInfo({gen_event_EXIT, Module, Reason}, #state{module = Module, |
|
|
|
config = Config, sink = Sink} = State) -> |
|
|
|
case eRum:log(error, self(), "Lager event handler ~p exited with reason ~s", |
|
|
|
[Module, rumErrLoggerH:format_reason(Reason)]) of |
|
|
|
case eRum:log(error, self(), "Lager event handler ~p exited with reason ~s", [Module, rumErrLoggerH:format_reason(Reason)]) of |
|
|
|
ok -> |
|
|
|
install_handler(Sink, Module, Config); |
|
|
|
{error, _} -> |
|
|
@ -88,19 +69,19 @@ handle_info({gen_event_EXIT, Module, Reason}, #state{module = Module, |
|
|
|
ok |
|
|
|
end, |
|
|
|
{noreply, State}; |
|
|
|
handle_info(reinstall_handler, #state{module = Module, config = Config, sink = Sink} = State) -> |
|
|
|
handleInfo(reinstall_handler, #state{module = Module, config = Config, sink = Sink} = State) -> |
|
|
|
install_handler(Sink, Module, Config), |
|
|
|
{noreply, State}; |
|
|
|
handle_info({reboot, Sink}, State) -> |
|
|
|
handleInfo({reboot, Sink}, State) -> |
|
|
|
_ = lager_app:boot(Sink), |
|
|
|
{noreply, State}; |
|
|
|
handle_info(stop, State) -> |
|
|
|
handleInfo(stop, State) -> |
|
|
|
{stop, normal, State}; |
|
|
|
handle_info({'EXIT', _Pid, killed}, #state{module = Module, config = Config, sink = Sink} = State) -> |
|
|
|
handleInfo({'EXIT', _Pid, killed}, #state{module = Module, config = Config, sink = Sink} = State) -> |
|
|
|
Tmr = application:get_env(lager, killer_reinstall_after, 5000), |
|
|
|
_ = timer:apply_after(Tmr, lager_app, start_handler, [Sink, Module, Config]), |
|
|
|
{stop, normal, State}; |
|
|
|
handle_info(_Info, State) -> |
|
|
|
handleInfo(_Info, State) -> |
|
|
|
{noreply, State}. |
|
|
|
|
|
|
|
terminate(_Reason, _State) -> |
|
|
@ -128,15 +109,13 @@ install_handler2(Sink, Module, Config) -> |
|
|
|
eRum:update_loglevel_config(Sink), |
|
|
|
ok; |
|
|
|
{error, {fatal, Reason}} -> |
|
|
|
?INT_LOG(error, "Lager fatally failed to install handler ~p into" |
|
|
|
" ~p, NOT retrying: ~p", [Module, Sink, Reason]), |
|
|
|
?INT_LOG(error, "Lager fatally failed to install handler ~p into ~p, NOT retrying: ~p", [Module, Sink, Reason]), |
|
|
|
%% tell ourselves to stop |
|
|
|
self() ! stop, |
|
|
|
ok; |
|
|
|
Error -> |
|
|
|
%% try to reinstall it later |
|
|
|
?INT_LOG(error, "Lager failed to install handler ~p into" |
|
|
|
" ~p, retrying later : ~p", [Module, Sink, Error]), |
|
|
|
?INT_LOG(error, "Lager failed to install handler ~p into ~p, retrying later : ~p", [Module, Sink, Error]), |
|
|
|
erlang:send_after(5000, self(), reinstall_handler), |
|
|
|
ok |
|
|
|
end. |
|
|
|