Allow to delete the mail queue of a single file

This commit is contained in:
Fabio Salvini 2017-12-02 15:55:16 +01:00
parent 269813aa18
commit c80d15cfe8
1 changed files with 37 additions and 1 deletions

View File

@ -17,7 +17,7 @@
%% API
-export([start_link/0]).
-export([queue/0, empty_queue/0]).
-export([queue/0, empty_queue/0, empty_file_queue/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@ -59,6 +59,16 @@ queue() ->
empty_queue() ->
gen_server:call(mailer, {empty_queue}).
%%--------------------------------------------------------------------
%% @doc
%% Delete all the mails of the given file from the queue.
%%
%% @spec empty_file_queue(File) -> ok
%% @end
%%--------------------------------------------------------------------
empty_file_queue(File) ->
gen_server:call(mailer, {empty_file_queue, File}).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
@ -106,6 +116,9 @@ handle_call({queue_count}, _From, State) ->
handle_call({empty_queue}, _From, State) ->
remove_all_emails(),
{reply, ok, State};
handle_call({empty_file_queue, File}, _From, State) ->
remove_file_emails(File),
{reply, ok, State};
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
@ -278,6 +291,29 @@ remove_email(Id) ->
end),
ok.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Remove all emails of the given file from the database.
%%
%% @spec remove_file_emails(File) -> void()
%% @end
%%--------------------------------------------------------------------
remove_file_emails(File) ->
mnesia:activity(
transaction,
fun() ->
mnesia:foldl(
fun(#log_monitor_error{id = Id, file = ErrorFile, text = _Text}, _Acc) ->
case ErrorFile of
File -> mnesia:delete({log_monitor_error, Id});
_ -> ok
end
end, [], log_monitor_error)
end),
ok.
%%--------------------------------------------------------------------
%% @private
%% @doc