hrl file for mnesia tables, removed coordinator

This commit is contained in:
Fabio Salvini 2017-06-18 16:50:22 +02:00
parent d3d42097d9
commit ccfc089f31
5 changed files with 58 additions and 62 deletions

View File

@ -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).

View File

@ -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}}.

View File

@ -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).

View File

@ -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

View File

@ -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.