From bbf2a4ff54f53527dccddabb861af73cd41ca92c Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Mon, 12 Jun 2017 14:46:17 +0200 Subject: [PATCH] Unmonitor log --- apps/log_monitor/src/config.erl | 13 +++++++++++-- apps/log_monitor/src/logfiles_sup.erl | 24 +++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/apps/log_monitor/src/config.erl b/apps/log_monitor/src/config.erl index c73e438..fdf64d3 100644 --- a/apps/log_monitor/src/config.erl +++ b/apps/log_monitor/src/config.erl @@ -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}). diff --git a/apps/log_monitor/src/logfiles_sup.erl b/apps/log_monitor/src/logfiles_sup.erl index 6d0f942..20fa40d 100644 --- a/apps/log_monitor/src/logfiles_sup.erl +++ b/apps/log_monitor/src/logfiles_sup.erl @@ -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).