gen_server for watcher
This commit is contained in:
parent
f64408f584
commit
f0403bfa26
|
@ -54,5 +54,6 @@ isError(Text) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
startWatcher(File) ->
|
startWatcher(File) ->
|
||||||
WatcherPid = spawn_link(watcher, init, [self(), File]),
|
%% WatcherPid = spawn_link(watcher, init, [self(), File]),
|
||||||
|
WatcherPid = watcher:start_link(self(), File),
|
||||||
{ok, WatcherPid}.
|
{ok, WatcherPid}.
|
||||||
|
|
|
@ -1,20 +1,35 @@
|
||||||
-module(watcher).
|
-module(watcher).
|
||||||
|
-behaviour(gen_server).
|
||||||
-compile(export_all).
|
-compile(export_all).
|
||||||
|
|
||||||
start(MonitorPid, File) ->
|
start_link(MonitorPid, File) ->
|
||||||
spawn_link(?MODULE, init, [MonitorPid, File]).
|
gen_server:start_link(?MODULE, [MonitorPid, File], []).
|
||||||
|
|
||||||
init(MonitorPid, File) ->
|
init([MonitorPid, File]) ->
|
||||||
|
io:format("Init~n"),
|
||||||
Cmd = "/usr/bin/tail -n0 --follow=name " ++ File,
|
Cmd = "/usr/bin/tail -n0 --follow=name " ++ File,
|
||||||
Port = open_port({spawn, Cmd}, [stderr_to_stdout, exit_status, binary]),
|
Port = open_port({spawn, Cmd}, [stderr_to_stdout, exit_status, binary]),
|
||||||
loop(MonitorPid, Port).
|
{ok, [MonitorPid, Port]}.
|
||||||
|
|
||||||
loop(MonitorPid, Port) ->
|
handle_info(Msg, [MonitorPid, Port]) ->
|
||||||
receive
|
case Msg of
|
||||||
{Port, {data, {eol, Bin}}} ->
|
{Port, {data, Text}} ->
|
||||||
Text = binary_to_list(iolist_to_binary(Bin)),
|
|
||||||
MonitorPid ! {log_line, Text},
|
MonitorPid ! {log_line, Text},
|
||||||
loop(MonitorPid, Port);
|
{noreply, [MonitorPid, Port]};
|
||||||
{Port, {exit_status, _Status}} ->
|
{Port, {exit_status, _Status}} ->
|
||||||
io:format("Watcher terminated~n")
|
io:format("Watcher terminated~n"),
|
||||||
|
{stop, tail_exit, []}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
handle_cast(_Msg, State) ->
|
||||||
|
{noreply, State}.
|
||||||
|
|
||||||
|
handle_call(_Request, _From, State) ->
|
||||||
|
{noreply, State}.
|
||||||
|
|
||||||
|
terminate(Reason, _State) ->
|
||||||
|
io:format("Reason: ~s~n", [Reason]),
|
||||||
|
shutdown.
|
||||||
|
|
||||||
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
|
{ok, State}.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user