25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-28 16:34:13 +01:00

Kill and restart lager when it's overloaded

This commit is contained in:
Evgeniy Khramtsov 2017-12-17 21:03:40 +03:00
parent bd1ff0e897
commit 59f4efe3dd
2 changed files with 43 additions and 24 deletions

View File

@ -27,7 +27,8 @@
-behaviour(ejabberd_config). -behaviour(ejabberd_config).
%% API %% API
-export([start/0, reopen_log/0, rotate_log/0, get/0, set/1, get_log_path/0, opt_type/1]). -export([start/0, restart/0, reopen_log/0, rotate_log/0, get/0, set/1,
get_log_path/0, opt_type/1]).
-include("ejabberd.hrl"). -include("ejabberd.hrl").
@ -102,6 +103,11 @@ get_string_env(Name, Default) ->
%% @spec () -> ok %% @spec () -> ok
start() -> start() ->
start(4).
-spec start(loglevel()) -> ok.
start(Level) ->
LLevel = get_lager_loglevel(Level),
StartedApps = application:which_applications(5000), StartedApps = application:which_applications(5000),
case lists:keyfind(logger, 1, StartedApps) of case lists:keyfind(logger, 1, StartedApps) of
%% Elixir logger is started. We assume everything is in place %% Elixir logger is started. We assume everything is in place
@ -109,24 +115,24 @@ start() ->
{logger, _, _} -> {logger, _, _} ->
error_logger:info_msg("Ignoring ejabberd logger options, using Elixir Logger.", []), error_logger:info_msg("Ignoring ejabberd logger options, using Elixir Logger.", []),
%% Do not start lager, we rely on Elixir Logger %% Do not start lager, we rely on Elixir Logger
do_start_for_logger(); do_start_for_logger(LLevel);
_ -> _ ->
do_start() do_start(LLevel)
end. end.
do_start_for_logger() -> do_start_for_logger(Level) ->
application:load(sasl), application:load(sasl),
application:set_env(sasl, sasl_error_logger, false), application:set_env(sasl, sasl_error_logger, false),
application:load(lager), application:load(lager),
application:set_env(lager, error_logger_redirect, false), application:set_env(lager, error_logger_redirect, false),
application:set_env(lager, error_logger_whitelist, ['Elixir.Logger.ErrorHandler']), application:set_env(lager, error_logger_whitelist, ['Elixir.Logger.ErrorHandler']),
application:set_env(lager, crash_log, false), application:set_env(lager, crash_log, false),
application:set_env(lager, handlers, [{elixir_logger_backend, [{level, info}]}]), application:set_env(lager, handlers, [{elixir_logger_backend, [{level, Level}]}]),
ejabberd:start_app(lager), ejabberd:start_app(lager),
ok. ok.
%% Start lager %% Start lager
do_start() -> do_start(Level) ->
application:load(sasl), application:load(sasl),
application:set_env(sasl, sasl_error_logger, false), application:set_env(sasl, sasl_error_logger, false),
application:load(lager), application:load(lager),
@ -141,8 +147,8 @@ do_start() ->
application:set_env(lager, error_logger_hwm, LogRateLimit), application:set_env(lager, error_logger_hwm, LogRateLimit),
application:set_env( application:set_env(
lager, handlers, lager, handlers,
[{lager_console_backend, info}, [{lager_console_backend, Level},
{lager_file_backend, [{file, ConsoleLog}, {level, info}, {date, LogRotateDate}, {lager_file_backend, [{file, ConsoleLog}, {level, Level}, {date, LogRotateDate},
{count, LogRotateCount}, {size, LogRotateSize}]}, {count, LogRotateCount}, {size, LogRotateSize}]},
{lager_file_backend, [{file, ErrorLog}, {level, error}, {date, LogRotateDate}, {lager_file_backend, [{file, ErrorLog}, {level, error}, {date, LogRotateDate},
{count, LogRotateCount}, {size, LogRotateSize}]}]), {count, LogRotateCount}, {size, LogRotateSize}]}]),
@ -156,6 +162,11 @@ do_start() ->
end, gen_event:which_handlers(lager_event)), end, gen_event:which_handlers(lager_event)),
ok. ok.
restart() ->
Level = ejabberd_config:get_option(loglevel, 4),
application:stop(lager),
start(Level).
%% @spec () -> ok %% @spec () -> ok
reopen_log() -> reopen_log() ->
%% Lager detects external log rotation automatically. %% Lager detects external log rotation automatically.
@ -187,15 +198,7 @@ get() ->
%% @spec (loglevel() | {loglevel(), list()}) -> {module, module()} %% @spec (loglevel() | {loglevel(), list()}) -> {module, module()}
set(LogLevel) when is_integer(LogLevel) -> set(LogLevel) when is_integer(LogLevel) ->
LagerLogLevel = case LogLevel of LagerLogLevel = get_lager_loglevel(LogLevel),
0 -> none;
1 -> critical;
2 -> error;
3 -> warning;
4 -> info;
5 -> debug;
E -> throw({wrong_loglevel, E})
end,
case get_lager_loglevel() of case get_lager_loglevel() of
LagerLogLevel -> LagerLogLevel ->
ok; ok;
@ -228,6 +231,17 @@ get_lager_loglevel() ->
end, end,
none, Handlers). none, Handlers).
get_lager_loglevel(LogLevel) ->
case LogLevel of
0 -> none;
1 -> critical;
2 -> error;
3 -> warning;
4 -> info;
5 -> debug;
E -> erlang:error({wrong_loglevel, E})
end.
get_lager_handlers() -> get_lager_handlers() ->
case catch gen_event:which_handlers(lager_event) of case catch gen_event:which_handlers(lager_event) of
{'EXIT',noproc} -> {'EXIT',noproc} ->

View File

@ -68,11 +68,7 @@ start() ->
ejabberd:start_app(os_mon). ejabberd:start_app(os_mon).
excluded_apps() -> excluded_apps() ->
%% FIXME: lager gets overloaded very often, but [os_mon, mnesia, sasl, stdlib, kernel].
%% it fails to get recovered after brutal kill,
%% so it's better to make it tolerate crashes
%% rather than ignoring the overload
[os_mon, mnesia, sasl, stdlib, kernel, lager].
%%%=================================================================== %%%===================================================================
%%% gen_event callbacks %%% gen_event callbacks
@ -270,8 +266,11 @@ do_kill(Stats, Threshold) ->
false; false;
false -> false ->
case kill_proc(Name) of case kill_proc(Name) of
false -> false; false ->
Pid -> {true, Pid} false;
Pid ->
maybe_restart_app(App),
{true, Pid}
end end
end; end;
(_) -> (_) ->
@ -295,6 +294,12 @@ kill_proc(Pid) ->
exit(Pid, kill), exit(Pid, kill),
Pid. Pid.
-spec maybe_restart_app(atom()) -> any().
maybe_restart_app(lager) ->
ejabberd_logger:restart();
maybe_restart_app(_) ->
ok.
-spec opt_type(oom_killer) -> fun((boolean()) -> boolean()); -spec opt_type(oom_killer) -> fun((boolean()) -> boolean());
(atom()) -> [atom()]. (atom()) -> [atom()].
opt_type(oom_killer) -> opt_type(oom_killer) ->