Watcher state and constant

This commit is contained in:
Fabio Salvini 2017-07-02 10:55:43 +02:00
parent 006a241293
commit 85c9443407

View File

@ -18,8 +18,9 @@
terminate/2, code_change/3]). terminate/2, code_change/3]).
-define(SERVER, ?MODULE). -define(SERVER, ?MODULE).
-define(TAIL_CMD, "/usr/bin/tail -n0 --follow=name ").
-record(state, {}). -record(state, {file, supervisor, port}).
%%%=================================================================== %%%===================================================================
%%% API %%% API
@ -53,9 +54,9 @@ start_link(SupPid, File) ->
init([SupPid, File]) -> init([SupPid, File]) ->
timer:sleep(1000), timer:sleep(1000),
config ! {watcher_init, File}, config ! {watcher_init, File},
Cmd = "/usr/bin/tail -n0 --follow=name " ++ File, Cmd = ?TAIL_CMD ++ File,
Port = open_port({spawn, Cmd}, [stderr_to_stdout, exit_status, binary]), Port = open_port({spawn, Cmd}, [stderr_to_stdout, exit_status, binary]),
{ok, [File, SupPid, Port]}. {ok, #state{file = File, supervisor = SupPid, port = Port}}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @private %% @private
@ -98,14 +99,14 @@ handle_cast(_Msg, State) ->
%% {stop, Reason, State} %% {stop, Reason, State}
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
handle_info(Msg, [File, SupPid, Port]) -> handle_info(Msg, State = #state{file = _, supervisor = SupPid, port = Port}) ->
case Msg of case Msg of
{Port, {data, Text}} -> {Port, {data, Text}} ->
GathererPid = gatherer_pid(SupPid), GathererPid = gatherer_pid(SupPid),
GathererPid ! {log_line, binary_to_list(Text)}, GathererPid ! {log_line, binary_to_list(Text)},
{noreply, [File, SupPid, Port]}; {noreply, State};
{Port, {exit_status, _Status}} -> {Port, {exit_status, _Status}} ->
{stop, tail_exit, [File]} {stop, tail_exit, State}
end. end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -119,8 +120,8 @@ handle_info(Msg, [File, SupPid, Port]) ->
%% @spec terminate(Reason, State) -> void() %% @spec terminate(Reason, State) -> void()
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
terminate(_Reason, [File]) -> terminate(_Reason, State) ->
config ! {watcher_terminate, File}, config ! {watcher_terminate, State#state.file},
shutdown. shutdown.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------