Managed problem of infinite consecutive errors

This commit is contained in:
Fabio Salvini 2017-07-02 13:35:56 +02:00
parent 84592a6987
commit 70ab6cdd98
2 changed files with 17 additions and 7 deletions

View File

@ -1,4 +1,3 @@
TODO
=====
- Fix problem of infinite consecutive errors.
- Limit number of emails that can be sent in a period of time.

View File

@ -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},