Unmonitor log
This commit is contained in:
parent
810b6c0ada
commit
bbf2a4ff54
|
@ -1,7 +1,7 @@
|
||||||
-module(config).
|
-module(config).
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
-export([monitor_log/2]).
|
-export([monitor_log/2, unmonitor_log/1]).
|
||||||
-export([logfiles/0]).
|
-export([logfiles/0]).
|
||||||
-export([start_link/0, init/1, terminate/2]).
|
-export([start_link/0, init/1, terminate/2]).
|
||||||
-export([handle_info/2, handle_cast/2, handle_call/3]).
|
-export([handle_info/2, handle_cast/2, handle_call/3]).
|
||||||
|
@ -15,6 +15,7 @@ init([]) ->
|
||||||
{ok, Storage} = application:get_env(log_monitor, storage),
|
{ok, Storage} = application:get_env(log_monitor, storage),
|
||||||
{ok, Logfiles} = dets:open_file(Storage, []),
|
{ok, Logfiles} = dets:open_file(Storage, []),
|
||||||
Statuses = ets:new(log_statuses, []),
|
Statuses = ets:new(log_statuses, []),
|
||||||
|
%% TODO: start logs inside Logfile
|
||||||
{ok, [Logfiles, Statuses]}.
|
{ok, [Logfiles, Statuses]}.
|
||||||
|
|
||||||
handle_info({watcher_init, File}, State = [_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}),
|
dets:insert(Logfiles, {File, ErrorRegex}),
|
||||||
ets:insert(Statuses, {File, disabled}),
|
ets:insert(Statuses, {File, disabled}),
|
||||||
supervisor:start_child(logfiles_sup, [File, ErrorRegex]),
|
logfiles_sup:add_child([File, ErrorRegex]),
|
||||||
{reply, ok, State};
|
{reply, ok, State};
|
||||||
_ -> {reply, duplicate, State}
|
_ -> {reply, duplicate, State}
|
||||||
end;
|
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]) ->
|
handle_call({get_statuses}, _From, State = [_Logfiles, Statuses]) ->
|
||||||
{reply, ets:tab2list(Statuses), State}.
|
{reply, ets:tab2list(Statuses), State}.
|
||||||
|
|
||||||
|
@ -50,5 +56,8 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
monitor_log(File, ErrorRegex) ->
|
monitor_log(File, ErrorRegex) ->
|
||||||
gen_server:call(config, {monitor, File, ErrorRegex}).
|
gen_server:call(config, {monitor, File, ErrorRegex}).
|
||||||
|
|
||||||
|
unmonitor_log(File) ->
|
||||||
|
gen_server:call(config, {unmonitor, File}).
|
||||||
|
|
||||||
logfiles() ->
|
logfiles() ->
|
||||||
gen_server:call(config, {get_statuses}).
|
gen_server:call(config, {get_statuses}).
|
||||||
|
|
|
@ -3,20 +3,30 @@
|
||||||
|
|
||||||
-export([start_link/0]).
|
-export([start_link/0]).
|
||||||
-export([init/1]).
|
-export([init/1]).
|
||||||
|
-export([add_child/1, remove_child/1]).
|
||||||
|
|
||||||
start_link() ->
|
start_link() ->
|
||||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
SupFlags = #{
|
SupFlags = #{
|
||||||
strategy => simple_one_for_one,
|
strategy => one_for_one,
|
||||||
intensity => 0,
|
intensity => 0,
|
||||||
period => 1
|
period => 1
|
||||||
},
|
},
|
||||||
ChildSpecs = [#{
|
ChildSpecs = [],
|
||||||
id => log_sup,
|
|
||||||
start => {log_sup, start_link, []},
|
|
||||||
restart => temporary,
|
|
||||||
shutdown => 5000
|
|
||||||
}],
|
|
||||||
{ok, {SupFlags, 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).
|
||||||
|
|
Loading…
Reference in New Issue
Block a user