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