Unmonitor log

This commit is contained in:
Fabio Salvini 2017-06-12 14:46:17 +02:00
parent 810b6c0ada
commit bbf2a4ff54
2 changed files with 28 additions and 9 deletions

View File

@ -1,7 +1,7 @@
-module(config).
-behaviour(gen_server).
-export([monitor_log/2]).
-export([monitor_log/2, unmonitor_log/1]).
-export([logfiles/0]).
-export([start_link/0, init/1, terminate/2]).
-export([handle_info/2, handle_cast/2, handle_call/3]).
@ -15,6 +15,7 @@ init([]) ->
{ok, Storage} = application:get_env(log_monitor, storage),
{ok, Logfiles} = dets:open_file(Storage, []),
Statuses = ets:new(log_statuses, []),
%% TODO: start logs inside Logfile
{ok, [Logfiles, Statuses]}.
handle_info({watcher_init, File}, State = [_Logfiles, Statuses]) ->
@ -34,10 +35,15 @@ handle_call({monitor, File, ErrorRegex}, _From, State = [Logfiles, Statuses]) ->
[] ->
dets:insert(Logfiles, {File, ErrorRegex}),
ets:insert(Statuses, {File, disabled}),
supervisor:start_child(logfiles_sup, [File, ErrorRegex]),
logfiles_sup:add_child([File, ErrorRegex]),
{reply, ok, State};
_ -> {reply, duplicate, State}
end;
handle_call({unmonitor, File}, _From, State = [Logfiles, Statuses]) ->
logfiles_sup:remove_child(File),
ets:delete(Statuses, File),
dets:delete(Logfiles, File),
{reply, ok, State};
handle_call({get_statuses}, _From, State = [_Logfiles, Statuses]) ->
{reply, ets:tab2list(Statuses), State}.
@ -50,5 +56,8 @@ code_change(_OldVsn, State, _Extra) ->
monitor_log(File, ErrorRegex) ->
gen_server:call(config, {monitor, File, ErrorRegex}).
unmonitor_log(File) ->
gen_server:call(config, {unmonitor, File}).
logfiles() ->
gen_server:call(config, {get_statuses}).

View File

@ -3,20 +3,30 @@
-export([start_link/0]).
-export([init/1]).
-export([add_child/1, remove_child/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
SupFlags = #{
strategy => simple_one_for_one,
strategy => one_for_one,
intensity => 0,
period => 1
},
ChildSpecs = [#{
id => log_sup,
start => {log_sup, start_link, []},
restart => temporary,
shutdown => 5000
}],
ChildSpecs = [],
{ok, {SupFlags, ChildSpecs}}.
add_child(Args = [File, _ErrorRegex]) ->
supervisor:start_child(
?MODULE,
#{
id => File,
start => {log_sup, start_link, Args},
restart => temporary,
shutdown => 5000
}
).
remove_child(File) ->
supervisor:terminate_child(?MODULE, File).