From 2a80282b8e7f8a6c9a211c7e78d5ae95349faa72 Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Mon, 12 Jun 2017 10:41:45 +0200 Subject: [PATCH] Renamed monitor module to log_sup and added config module --- apps/log_monitor/src/config.erl | 34 +++++++++++++++++++ apps/log_monitor/src/coordinator.erl | 6 ++++ .../src/{monitor.erl => log_sup.erl} | 2 +- apps/log_monitor/src/logfiles_sup.erl | 9 ++--- 4 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 apps/log_monitor/src/config.erl rename apps/log_monitor/src/{monitor.erl => log_sup.erl} (97%) diff --git a/apps/log_monitor/src/config.erl b/apps/log_monitor/src/config.erl new file mode 100644 index 0000000..c9caa0e --- /dev/null +++ b/apps/log_monitor/src/config.erl @@ -0,0 +1,34 @@ +-module(config). +-behaviour(gen_server). + +-export([monitor_log/1]). +-export([start_link/0, init/1, terminate/2]). +-export([handle_info/2, handle_cast/2, handle_call/3]). +-export([code_change/3]). + +start_link() -> + gen_server:start_link(?MODULE, [], []). + +init([]) -> + register(config, self()), + {ok, []}. + +handle_info(_Msg, State) -> + {noreply, State}. + +handle_cast(_Msg, State) -> + {noreply, State}. + +handle_call({monitor, Path}, _From, State) -> + supervisor:start_child(logfiles_sup, [Path]), + {reply, ok, State}. + +terminate(_Reason, _State) -> + shutdown. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +monitor_log(Path) -> + config ! {monitor, Path}, + gen_server:call(config, {monitor, Path}). diff --git a/apps/log_monitor/src/coordinator.erl b/apps/log_monitor/src/coordinator.erl index f63125f..c9ad376 100644 --- a/apps/log_monitor/src/coordinator.erl +++ b/apps/log_monitor/src/coordinator.erl @@ -21,6 +21,12 @@ init([]) -> restart => permanent, type => supervisor, shutdown => 5000 + }, + #{ + id => config, + start => {config, start_link, []}, + restart => permanent, + shutdown => 5000 } ], {ok, {SupFlags, ChildSpecs}}. diff --git a/apps/log_monitor/src/monitor.erl b/apps/log_monitor/src/log_sup.erl similarity index 97% rename from apps/log_monitor/src/monitor.erl rename to apps/log_monitor/src/log_sup.erl index 126fd7e..9ae6824 100644 --- a/apps/log_monitor/src/monitor.erl +++ b/apps/log_monitor/src/log_sup.erl @@ -1,4 +1,4 @@ --module(monitor). +-module(log_sup). -behaviour(supervisor). -export([start_link/1]). diff --git a/apps/log_monitor/src/logfiles_sup.erl b/apps/log_monitor/src/logfiles_sup.erl index e9ec2ec..6d0f942 100644 --- a/apps/log_monitor/src/logfiles_sup.erl +++ b/apps/log_monitor/src/logfiles_sup.erl @@ -5,7 +5,7 @@ -export([init/1]). start_link() -> - supervisor:start_link({local, logfiles_sup}, ?MODULE, []). + supervisor:start_link({local, ?MODULE}, ?MODULE, []). init([]) -> SupFlags = #{ @@ -14,12 +14,9 @@ init([]) -> period => 1 }, ChildSpecs = [#{ - id => monitor, - start => {monitor, start_link, []}, + id => log_sup, + start => {log_sup, start_link, []}, restart => temporary, shutdown => 5000 }], {ok, {SupFlags, ChildSpecs}}. - -%% Start monitor with: -%% supervisor:start_child(logfiles_sup, "/tmp/lines.log")