Skip to content

Commit 7a96d09

Browse files
committed
Functions to support log streaming to a CLI node.
To stream log updates to remote node there will be a process started on the server node to read the log file and send its contents. Unfortunately it's hard to detect the file to be deleted and the streaming process will try indefinitely to get more data from the deleted file. Streaming process is linked to the CLI process and should eventually stop.
1 parent 2103440 commit 7a96d09

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/rabbit_log_tail.erl

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
-module(rabbit_log_tail).
22

33
-export([tail_n_lines/2]).
4-
% -export([init_tail_stream/2, tail_send/2, tail_receive/2]).
4+
-export([init_tail_stream/3]).
55

66
-define(GUESS_OFFSET, 200).
77

8+
init_tail_stream(Filename, Pid, Ref) ->
9+
RPCProc = self(),
10+
Reader = spawn(fun() ->
11+
link(Pid),
12+
case file:open(Filename, [read, binary]) of
13+
{ok, File} ->
14+
{ok, _} = file:position(File, eof),
15+
RPCProc ! {Ref, opened},
16+
read_loop(File, Pid, Ref);
17+
{error, _} = Err ->
18+
RPCProc ! {Ref, Err}
19+
end
20+
end),
21+
receive
22+
{Ref, opened} -> {ok, Ref};
23+
{Ref, {error, Err}} -> {error, Err}
24+
after 5000 ->
25+
exit(Reader, timeout),
26+
{error, timeout}
27+
end.
28+
29+
read_loop(File, Pid, Ref) ->
30+
case file:read(File, ?GUESS_OFFSET) of
31+
{ok, Data} ->
32+
Pid ! {Ref, Data, confinue},
33+
read_loop(File, Pid, Ref);
34+
eof ->
35+
timer:sleep(1000),
36+
read_loop(File, Pid, Ref);
37+
{error, _} = Err ->
38+
Pid ! {Ref, Err, finished}
39+
end.
40+
841
tail_n_lines(Filename, N) ->
942
case file:open(Filename, [read, binary]) of
1043
{ok, File} ->

0 commit comments

Comments
 (0)