diff options
| author | Daniil Fedotov <hairyhum@gmail.com> | 2019-06-26 17:28:58 -0400 |
|---|---|---|
| committer | Daniil Fedotov <hairyhum@gmail.com> | 2019-06-26 17:28:58 -0400 |
| commit | 7a96d094b8f895623c98371f5af0aafcd17c16b9 (patch) | |
| tree | 571afc6828ed08f00c3b531dfcfcc7cb52da37f0 | |
| parent | 2103440692ada9c32473df6e951868a217e63352 (diff) | |
| download | rabbitmq-server-git-7a96d094b8f895623c98371f5af0aafcd17c16b9.tar.gz | |
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.
| -rw-r--r-- | src/rabbit_log_tail.erl | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/rabbit_log_tail.erl b/src/rabbit_log_tail.erl index d5764c90b9..d0fb55ec84 100644 --- a/src/rabbit_log_tail.erl +++ b/src/rabbit_log_tail.erl @@ -1,10 +1,43 @@ -module(rabbit_log_tail). -export([tail_n_lines/2]). -% -export([init_tail_stream/2, tail_send/2, tail_receive/2]). +-export([init_tail_stream/3]). -define(GUESS_OFFSET, 200). +init_tail_stream(Filename, Pid, Ref) -> + RPCProc = self(), + Reader = spawn(fun() -> + link(Pid), + case file:open(Filename, [read, binary]) of + {ok, File} -> + {ok, _} = file:position(File, eof), + RPCProc ! {Ref, opened}, + read_loop(File, Pid, Ref); + {error, _} = Err -> + RPCProc ! {Ref, Err} + end + end), + receive + {Ref, opened} -> {ok, Ref}; + {Ref, {error, Err}} -> {error, Err} + after 5000 -> + exit(Reader, timeout), + {error, timeout} + end. + +read_loop(File, Pid, Ref) -> + case file:read(File, ?GUESS_OFFSET) of + {ok, Data} -> + Pid ! {Ref, Data, confinue}, + read_loop(File, Pid, Ref); + eof -> + timer:sleep(1000), + read_loop(File, Pid, Ref); + {error, _} = Err -> + Pid ! {Ref, Err, finished} + end. + tail_n_lines(Filename, N) -> case file:open(Filename, [read, binary]) of {ok, File} -> |
