From 70ab6cdd988091207c983f68206417157ed72ad6 Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Sun, 2 Jul 2017 13:35:56 +0200 Subject: [PATCH] Managed problem of infinite consecutive errors --- TODO.md | 1 - apps/log_monitor/src/gatherer.erl | 23 +++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index bac07dc..0ab8bed 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ TODO ===== - - Fix problem of infinite consecutive errors. - Limit number of emails that can be sent in a period of time. diff --git a/apps/log_monitor/src/gatherer.erl b/apps/log_monitor/src/gatherer.erl index 1fa3a68..2134b67 100644 --- a/apps/log_monitor/src/gatherer.erl +++ b/apps/log_monitor/src/gatherer.erl @@ -24,7 +24,7 @@ -record(log, {file, error_regex}). -record(state_off, {log}). --record(state_on, {log, error, until}). +-record(state_on, {log, error, until, max_until}). %%%=================================================================== %%% API @@ -81,9 +81,14 @@ state_off({log_line, Text}, State = #state_off{log = Log}) -> case isError(Text, Log#log.error_regex) of true -> Timeout = timer_time(), + MaxTimeout = safe_timer_time(), Now = to_milliseconds(os:timestamp()), Until = Now + Timeout, - {next_state, state_on, #state_on{log = Log, error = Text, until = Until}, Timeout}; + MaxUntil = Now + MaxTimeout, + {next_state, + state_on, + #state_on{log = Log, error = Text, until = Until, max_until = MaxUntil}, + Timeout}; false -> {next_state, state_off, State} end. @@ -102,17 +107,23 @@ state_off({log_line, Text}, State = #state_off{log = Log}) -> %% {stop, Reason, NewState} %% @end %%-------------------------------------------------------------------- -state_on({log_line, Text}, #state_on{log = Log, error = Error, until = Until}) -> +state_on({log_line, Text}, #state_on{log = Log, error = Error, until = Until, max_until = MaxUntil}) -> case isError(Text, Log#log.error_regex) of true -> Timeout = timer_time(), Now = to_milliseconds(os:timestamp()), - Until = Now + Timeout, - {next_state, state_on, #state_on{log = Log, error = Error ++ Text, until = Until}, Timeout}; + NewUntil = min(Now + Timeout, MaxUntil), + {next_state, + state_on, + #state_on{log = Log, error = Error ++ Text, until = NewUntil, max_until = MaxUntil}, + if MaxUntil > Now -> Timeout; true -> 0 end}; false -> Now = to_milliseconds(os:timestamp()), Timeout = Until - Now, - {next_state, state_on, #state_on{log = Log, error = Error ++ Text, until = Until}, Timeout} + {next_state, + state_on, + #state_on{log = Log, error = Error ++ Text, until = Until, max_until = MaxUntil}, + Timeout} end; state_on(timeout, #state_on{log = Log, error = Error, until = _}) -> mailer ! {error, Log#log.file, Error},