From af8fad987dad13bdebe1f4a451f9021bbd127e23 Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Mon, 12 Jun 2017 19:44:06 +0200 Subject: [PATCH] External configuration and supervisor child modules --- apps/log_monitor/src/coordinator.erl | 9 ++++++--- apps/log_monitor/src/log_sup.erl | 6 ++++-- apps/log_monitor/src/logfiles_sup.erl | 9 +++++---- apps/log_monitor/src/mailer.erl | 14 +++++++------- config/log_monitor.config | 20 ++++++++++++++++++++ config/sys.config | 16 ++-------------- 6 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 config/log_monitor.config diff --git a/apps/log_monitor/src/coordinator.erl b/apps/log_monitor/src/coordinator.erl index c9ad376..a428222 100644 --- a/apps/log_monitor/src/coordinator.erl +++ b/apps/log_monitor/src/coordinator.erl @@ -13,20 +13,23 @@ init([]) -> id => mailer, start => {mailer, start_link, []}, restart => permanent, - shutdown => 5000 + shutdown => 5000, + modules => [mailer] }, #{ id => logfiles_sup, start => {logfiles_sup, start_link, []}, restart => permanent, type => supervisor, - shutdown => 5000 + shutdown => 5000, + modules => [logfiles_sup] }, #{ id => config, start => {config, start_link, []}, restart => permanent, - shutdown => 5000 + shutdown => 5000, + modules => [config] } ], {ok, {SupFlags, ChildSpecs}}. diff --git a/apps/log_monitor/src/log_sup.erl b/apps/log_monitor/src/log_sup.erl index 8c1c2b0..078a39a 100644 --- a/apps/log_monitor/src/log_sup.erl +++ b/apps/log_monitor/src/log_sup.erl @@ -13,12 +13,14 @@ init([File, ErrorRegex]) -> id => gatherer, start => {gatherer, start_link, [File, ErrorRegex]}, restart => permanent, - shutdown => 5000 + shutdown => 5000, + modules => [gatherer] }, #{ id => watcher, start => {watcher, start_link, [self(), File]}, restart => permanent, - shutdown => 5000 + shutdown => 5000, + modules => [watcher] }], {ok, {SupFlags, ChildSpecs}}. diff --git a/apps/log_monitor/src/logfiles_sup.erl b/apps/log_monitor/src/logfiles_sup.erl index 20fa40d..2241e53 100644 --- a/apps/log_monitor/src/logfiles_sup.erl +++ b/apps/log_monitor/src/logfiles_sup.erl @@ -21,10 +21,11 @@ add_child(Args = [File, _ErrorRegex]) -> supervisor:start_child( ?MODULE, #{ - id => File, - start => {log_sup, start_link, Args}, - restart => temporary, - shutdown => 5000 + id => File, + start => {log_sup, start_link, Args}, + restart => temporary, + shutdown => 5000, + modules => [log_sup] } ). diff --git a/apps/log_monitor/src/mailer.erl b/apps/log_monitor/src/mailer.erl index 4617299..bafc0f1 100644 --- a/apps/log_monitor/src/mailer.erl +++ b/apps/log_monitor/src/mailer.erl @@ -34,12 +34,12 @@ code_change(_OldVsn, State, _Extra) -> send_email(File, Text) -> {ok, EmailConfig} = application:get_env(log_monitor, email_config), - Sender = proplists:get_value(sender, EmailConfig, "log@monitor.com"), + Sender = proplists:get_value(sender, EmailConfig), DefaultReceiver = proplists:get_value(default_receiver, EmailConfig), - Relay = proplists:get_value(relay, EmailConfig), - Username = proplists:get_value(username, EmailConfig), - Password = proplists:get_value(password, EmailConfig), + Connection = proplists:get_value(connection, EmailConfig), error_logger:info_report("Sending email for file ~s with text: ~n~s~n", [File, Text]), - gen_smtp_client:send_blocking({Sender, [DefaultReceiver], - io_lib:format("Subject: Error notification\r\nFrom: Fabio\r\n\r\nLogfile: ~s~nError: ~n~s~n", [File, Text])}, - [{relay, Relay}, {username, Username}, {password, Password}]). + gen_smtp_client:send_blocking( + {Sender, [DefaultReceiver], + io_lib:format("Subject: Error notification\r\nFrom: Fabio\r\n\r\nLogfile: ~s~nError: ~n~s~n", [File, Text])}, + Connection + ). diff --git a/config/log_monitor.config b/config/log_monitor.config new file mode 100644 index 0000000..d7417fb --- /dev/null +++ b/config/log_monitor.config @@ -0,0 +1,20 @@ +[ + { log_monitor, + [ + %% File where the list of monitored log files is stored. + %% WARNING: If you leave it inside /tmp, every time the node is restarted the log files will be lost. + {storage, "/tmp/logfiles"}, + {email_config, + [ + {sender, "log@monitor.com"}, + {default_receiver, "salvini.fabio001@gmail.com"}, + {connection, [ + {relay, "smtp.fabiosalvinii.com"}, + {username, "salvini.fabio001@gmail.com"}, + {password, ""}] + } + ] + } + ] + } +]. diff --git a/config/sys.config b/config/sys.config index 967c843..72f7e3e 100644 --- a/config/sys.config +++ b/config/sys.config @@ -1,16 +1,4 @@ [ - { log_monitor, - [ - {storage, "/tmp/logfiles"}, - {email_config, - [ - {sender, "me@example.com"}, - {default_receiver, "salvini.fabio001@gmail.com"}, - {relay, "smtp.fabiosalvinii.com"}, - {username, "salvini.fabio001@gmail.com"}, - {password, ""} - ] - } - ] - } + { log_monitor, [] + }, "/home/fsalvini/gitRepos/log_monitor/config/log_monitor.config" ].