From ccfc089f31287aa85a758b802a0e3d3d2e98f318 Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Sun, 18 Jun 2017 16:50:22 +0200 Subject: [PATCH] hrl file for mnesia tables, removed coordinator --- apps/log_monitor/src/config.erl | 26 ++---------------- apps/log_monitor/src/coordinator.erl | 35 ------------------------ apps/log_monitor/src/log_monitor_app.erl | 23 ++++++++++++++++ apps/log_monitor/src/log_monitor_sup.erl | 28 +++++++++++++++++-- apps/log_monitor/src/mnesia_tables.hrl | 8 ++++++ 5 files changed, 58 insertions(+), 62 deletions(-) delete mode 100644 apps/log_monitor/src/coordinator.erl create mode 100644 apps/log_monitor/src/mnesia_tables.hrl diff --git a/apps/log_monitor/src/config.erl b/apps/log_monitor/src/config.erl index 5890cdc..b17c4e4 100644 --- a/apps/log_monitor/src/config.erl +++ b/apps/log_monitor/src/config.erl @@ -1,6 +1,8 @@ -module(config). -behaviour(gen_server). +-include_lib("mnesia_tables.hrl"). + -export([create_group/2, monitor_log/3, unmonitor_log/1]). -export([logfiles/0]). -export([start_link/0, init/1, terminate/2]). @@ -9,17 +11,12 @@ -record(state, {statuses}). -%% Mnesia tables --record(log_monitor_group, {name, email_receivers=[]}). --record(log_monitor_file, {file, error_regex, status, group}). - start_link() -> gen_server:start_link(?MODULE, [], []). init([]) -> register(config, self()), Statuses = ets:new(log_statuses, []), - start_mnesia(), mnesia:activity( transaction, fun() -> @@ -116,22 +113,3 @@ unmonitor_log(File) -> logfiles() -> gen_server:call(config, {get_statuses}). - -start_mnesia() -> - Nodes = [node()], - %% Stop Mnesia if is running, cannot create schema otherwise. - mnesia:stop(), - mnesia:create_schema(Nodes), - mnesia:start(), - mnesia:create_table(log_monitor_group, - [ - {attributes, record_info(fields, log_monitor_group)}, - {disc_copies, Nodes} - ]), - mnesia:create_table(log_monitor_file, - [ - {attributes, record_info(fields, log_monitor_file)}, - {index, [#log_monitor_file.group]}, - {disc_copies, Nodes} - ]), - ok = mnesia:wait_for_tables([log_monitor_group, log_monitor_file], 30000). diff --git a/apps/log_monitor/src/coordinator.erl b/apps/log_monitor/src/coordinator.erl deleted file mode 100644 index a428222..0000000 --- a/apps/log_monitor/src/coordinator.erl +++ /dev/null @@ -1,35 +0,0 @@ --module(coordinator). --behaviour(supervisor). - --export([start_link/0]). --export([init/1]). - -start_link() -> - supervisor:start_link(?MODULE, []). - -init([]) -> - SupFlags = #{strategy => one_for_one}, - ChildSpecs = [#{ - id => mailer, - start => {mailer, start_link, []}, - restart => permanent, - shutdown => 5000, - modules => [mailer] - }, - #{ - id => logfiles_sup, - start => {logfiles_sup, start_link, []}, - restart => permanent, - type => supervisor, - shutdown => 5000, - modules => [logfiles_sup] - }, - #{ - id => config, - start => {config, start_link, []}, - restart => permanent, - shutdown => 5000, - modules => [config] - } - ], - {ok, {SupFlags, ChildSpecs}}. diff --git a/apps/log_monitor/src/log_monitor_app.erl b/apps/log_monitor/src/log_monitor_app.erl index 0820c5c..5631820 100644 --- a/apps/log_monitor/src/log_monitor_app.erl +++ b/apps/log_monitor/src/log_monitor_app.erl @@ -7,6 +7,8 @@ -behaviour(application). +-include_lib("mnesia_tables.hrl"). + %% Application callbacks -export([start/2, stop/1]). @@ -15,12 +17,33 @@ %%==================================================================== start(_StartType, _StartArgs) -> + start_mnesia(), log_monitor_sup:start_link(). %%-------------------------------------------------------------------- stop(_State) -> + mnesia:stop(), ok. %%==================================================================== %% Internal functions %%==================================================================== + +start_mnesia() -> + Nodes = [node()], + %% Stop Mnesia if is running, cannot create schema otherwise. + mnesia:stop(), + mnesia:create_schema(Nodes), + mnesia:start(), + mnesia:create_table(log_monitor_group, + [ + {attributes, record_info(fields, log_monitor_group)}, + {disc_copies, Nodes} + ]), + mnesia:create_table(log_monitor_file, + [ + {attributes, record_info(fields, log_monitor_file)}, + {index, [#log_monitor_file.group]}, + {disc_copies, Nodes} + ]), + ok = mnesia:wait_for_tables([log_monitor_group, log_monitor_file], 30000). diff --git a/apps/log_monitor/src/log_monitor_sup.erl b/apps/log_monitor/src/log_monitor_sup.erl index f1f9331..721bd5d 100644 --- a/apps/log_monitor/src/log_monitor_sup.erl +++ b/apps/log_monitor/src/log_monitor_sup.erl @@ -28,9 +28,31 @@ start_link() -> %% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules} init([]) -> - {ok, { {one_for_all, 0, 1}, [{console, - {coordinator, start_link, []}, - permanent, 5000, supervisor, [coordinator]}]} }. + SupFlags = #{strategy => one_for_one}, + ChildSpecs = [#{ + id => mailer, + start => {mailer, start_link, []}, + restart => permanent, + shutdown => 5000, + modules => [mailer] + }, + #{ + id => logfiles_sup, + start => {logfiles_sup, start_link, []}, + restart => permanent, + type => supervisor, + shutdown => 5000, + modules => [logfiles_sup] + }, + #{ + id => config, + start => {config, start_link, []}, + restart => permanent, + shutdown => 5000, + modules => [config] + } + ], + {ok, {SupFlags, ChildSpecs}}. %%==================================================================== %% Internal functions diff --git a/apps/log_monitor/src/mnesia_tables.hrl b/apps/log_monitor/src/mnesia_tables.hrl new file mode 100644 index 0000000..ecf98ab --- /dev/null +++ b/apps/log_monitor/src/mnesia_tables.hrl @@ -0,0 +1,8 @@ +-ifndef(MNESIA_HRL). +-define(MNESIA_HRL, 1). + +%% Mnesia tables +-record(log_monitor_group, {name, email_receivers=[]}). +-record(log_monitor_file, {file, error_regex, status, group}). + +-endif.