Limit number of emails sent per minute.

This commit is contained in:
Fabio Salvini 2017-10-25 05:27:41 +02:00
parent b9a57d0054
commit 1ca50dbb90
3 changed files with 28 additions and 8 deletions

View File

@ -23,7 +23,7 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).
-record(state, {}). -record(state, {max_emails_per_minute, remaining_emails}).
%%%=================================================================== %%%===================================================================
%%% API %%% API
@ -66,8 +66,15 @@ queue() ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([]) -> init([]) ->
register(mailer, self()), register(mailer, self()),
{ok, EmailConfig} = application:get_env(log_monitor, email_config),
MaxEmailsPerMinute = proplists:get_value(max_emails_per_minute, EmailConfig),
timer:send_after(1000, {send_emails}), timer:send_after(1000, {send_emails}),
{ok, #state{}}. timer:send_interval(60000, {reset_count}),
{ok, #state{
max_emails_per_minute = MaxEmailsPerMinute,
remaining_emails = MaxEmailsPerMinute
}
}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @private %% @private
@ -123,7 +130,7 @@ handle_info({error, File, Text}, State) ->
end), end),
{noreply, State}; {noreply, State};
handle_info({send_emails}, State) -> handle_info({send_emails}, State) ->
Emails = emails_to_send(), Emails = lists:sublist(emails_to_send(), State#state.remaining_emails),
try send_emails(Emails) of try send_emails(Emails) of
_ -> timer:send_after(1000, {send_emails}) _ -> timer:send_after(1000, {send_emails})
catch catch
@ -131,7 +138,17 @@ handle_info({send_emails}, State) ->
error_logger:info_msg("Waiting 60s before sending new emails~n"), error_logger:info_msg("Waiting 60s before sending new emails~n"),
timer:send_after(60000, {send_emails}) timer:send_after(60000, {send_emails})
end, end,
{noreply, State}. {noreply, #state{
max_emails_per_minute = State#state.max_emails_per_minute,
remaining_emails = State#state.remaining_emails - length(Emails)
}
};
handle_info({reset_count}, State) ->
{noreply, #state{
max_emails_per_minute = State#state.max_emails_per_minute,
remaining_emails = State#state.max_emails_per_minute
}
}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @private %% @private
@ -335,8 +352,8 @@ first_line(Text) ->
%%%=================================================================== %%%===================================================================
-ifdef(TEST). -ifdef(TEST).
start_test() -> %% start_test() ->
{ok, _Pid} = start_link(). %% {ok, _Pid} = start_link().
email_subject_test() -> email_subject_test() ->
?assertEqual("Error", email_subject("Error", "/var/log/myApp.log", "Line")), ?assertEqual("Error", email_subject("Error", "/var/log/myApp.log", "Line")),

View File

@ -26,6 +26,9 @@
%% The receiver to use for the application errors. %% The receiver to use for the application errors.
{admin_receiver, "salvini.fabio001@gmail.com"}, {admin_receiver, "salvini.fabio001@gmail.com"},
%% Maximum number of emails per minute.
{max_emails_per_minute, 0},
%% Subject of the email. %% Subject of the email.
%% Placeholders: %% Placeholders:
%% %f: the full path of the log file (ex. /var/log/myapp.log). %% %f: the full path of the log file (ex. /var/log/myapp.log).
@ -42,7 +45,7 @@
}, },
%% Location of the file that contains the logs to monitor. %% Location of the file that contains the logs to monitor.
{logfiles_config, "/home/fsalvini/gitRepos/log_monitor/config/logfiles.config"} {logfiles_config, "/home/fsalvini/Projects/log_monitor/config/logfiles.config"}
] ]
}, },

View File

@ -1,4 +1,4 @@
[ [
{ log_monitor, [] { log_monitor, []
}, "/home/fsalvini/gitRepos/log_monitor/config/log_monitor.config" }, "/home/fsalvini/Projects/log_monitor/config/log_monitor.config"
]. ].