Reload config on init

This commit is contained in:
Fabio Salvini 2017-07-02 10:00:06 +02:00
parent d5136c731c
commit 8d828abea1

View File

@ -17,17 +17,7 @@ start_link() ->
init([]) ->
register(config, self()),
Statuses = ets:new(log_statuses, []),
%% TODO: reload?
mnesia:activity(
transaction,
fun() ->
mnesia:foldl(
fun(#log_monitor_file{file = File, error_regex = ErrorRegex}, _Acc) ->
ets:insert(Statuses, {File, inactive}),
logfiles_sup:add_child([File, ErrorRegex]),
ok
end, [], log_monitor_file)
end),
timer:apply_after(1000, ?MODULE, reload, []),
{ok, #state{statuses = Statuses}}.
handle_info({watcher_init, File}, State = #state{statuses = Statuses}) ->
@ -63,7 +53,7 @@ handle_call({reload}, _From, State = #state{statuses = Statuses}) ->
manage_new_groups(proplists:get_value(new, GroupsPartitions)),
LogfilesPartitions = compare_logfiles(Logfiles),
manage_deleted_logfiles(proplists:get_value(deleted, LogfilesPartitions), Statuses),
manage_existing_logfiles(proplists:get_value(existing, LogfilesPartitions)),
manage_existing_logfiles(proplists:get_value(existing, LogfilesPartitions), Statuses),
manage_new_logfiles(proplists:get_value(new, LogfilesPartitions), Statuses),
{reply, ok, State};
handle_call({get_statuses}, _From, State = #state{statuses = Statuses}) ->
@ -151,17 +141,27 @@ manage_deleted_logfiles(Logfiles, Statuses) ->
end, [], Logfiles)
end).
manage_existing_logfiles(Logfiles) ->
manage_existing_logfiles(Logfiles, Statuses) ->
mnesia:activity(
transaction,
fun() ->
lists:foldl(fun(Logfile = #log_monitor_file{file = File, error_regex = ErrorRegex, group = _}, _Acc) ->
mnesia:write(Logfile),
OldLogfile = hd(mnesia:read(log_monitor_file, File)),
FileStatus = ets:lookup(Statuses, File),
if (OldLogfile#log_monitor_file.error_regex =/= ErrorRegex) or (FileStatus == []) ->
logfiles_sup:remove_child(File),
ets:insert(Statuses, {File, inactive}),
logfiles_sup:add_child([File, ErrorRegex]);
true ->
ok
end,
case mnesia:read(log_monitor_file, File) of
[#log_monitor_file{file = File, error_regex = ErrorRegex, group = _}] -> ok;
_ ->
%% The error regex has changed
logfiles_sup:remove_child(File),
ets:insert(Statuses, {File, inactive}),
logfiles_sup:add_child([File, ErrorRegex])
end
end, [], Logfiles)