-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
55 lines (48 loc) · 1.3 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require "sinatra"
require "sinatra-websocket"
require "./models/log_file_details"
require "./io/tail_f"
require "./io/changes_poller"
set :server, "thin"
set :sockets, []
LOG_FILE = "./examples/example_logs.log"
get "/" do
if !request.websocket?
erb :index
else
request.websocket do |websocket|
websocket.onopen do
file = File.open(LOG_FILE)
settings.sockets << websocket
send_tailed_logs(websocket, file)
poll_for_changes(websocket, file)
rescue Errno::ENOENT, Errno::EINVAL
websocket.send("Something went wrong reading #{LOG_FILE}")
rescue EncodingError
websocket.send("Error encoding logfile")
end
websocket.onclose do
warn("websocket closed")
settings.sockets.delete(websocket)
end
end
end
end
private def send_tailed_logs(websocket, file)
tailed_logs = Io::TailF.tail(file, 10)
tailed_logs.each do |log|
encoded_log = log.encode!(Encoding::UTF_8)
websocket.send(encoded_log)
end
end
private def poll_for_changes(websocket, file)
log_file_details = LogFileDetails.new(
last_modified_time: file.mtime.to_i,
last_read_position: file.stat.size
)
Io::ChangesPoller.send_updates(
websocket: websocket,
log_file_details: log_file_details,
log_file: LOG_FILE
)
end