Skip to content

Commit e5e640a

Browse files
committed
Introduce improved version of the log processing script.
1 parent cb2eada commit e5e640a

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

script/pbs4.rb

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
require "net/http"
2+
require_relative "../logs_parser/lib/logs_parser"
3+
4+
class LogFileMonitor
5+
def initialize(log_file_path, initial_position = 0)
6+
@log_file_path = log_file_path
7+
@file_position = initial_position
8+
@last_size = 0
9+
@last_mtime = Time.at(0)
10+
@file_handle = nil
11+
end
12+
13+
def file_changed?
14+
return false unless File.exist?(@log_file_path)
15+
16+
stat = File.stat(@log_file_path)
17+
size_changed = stat.size != @last_size
18+
mtime_changed = stat.mtime != @last_mtime
19+
20+
@last_size = stat.size
21+
@last_mtime = stat.mtime
22+
23+
size_changed || mtime_changed
24+
end
25+
26+
def file_rotated?
27+
return false unless File.exist?(@log_file_path)
28+
29+
stat = File.stat(@log_file_path)
30+
stat.size < @file_position
31+
end
32+
33+
def read_new_lines
34+
return [] unless File.exist?(@log_file_path)
35+
36+
if file_rotated?
37+
puts "Log file rotated, starting from beginning"
38+
@file_position = 0
39+
close_file
40+
end
41+
42+
open_file
43+
@file_handle.seek(@file_position)
44+
45+
new_lines = []
46+
while line = @file_handle.gets
47+
new_lines << line.chomp
48+
end
49+
50+
@file_position = @file_handle.tell
51+
new_lines
52+
end
53+
54+
def close
55+
close_file
56+
end
57+
58+
private
59+
60+
def open_file
61+
unless @file_handle && !@file_handle.closed?
62+
@file_handle = File.open(@log_file_path, "r")
63+
end
64+
end
65+
66+
def close_file
67+
if @file_handle && !@file_handle.closed?
68+
@file_handle.close
69+
@file_handle = nil
70+
end
71+
end
72+
end
73+
74+
def main
75+
game_name = ARGV[0]
76+
players_count = ARGV[1].to_i
77+
initial_position = ARGV[2].to_i || 0
78+
host = ARGV[3] || "fierce-reaches-40697.herokuapp.com"
79+
log_file_path = "net_message_debug.log"
80+
81+
unless game_name && players_count > 0
82+
puts "Usage: ruby pbs4.rb <game_name> <players_count> [initial_position] [host]"
83+
exit 1
84+
end
85+
86+
parser = LogsParser::Service.new(game_name, players_count)
87+
http_adapter = LogsParser::HttpAdapter.new(host: host)
88+
monitor = LogFileMonitor.new(log_file_path, initial_position)
89+
90+
iterations_counter = 0
91+
processed_lines = 0
92+
93+
puts "Starting log monitor for #{game_name} (#{players_count} players)"
94+
puts "Initial position: #{initial_position}"
95+
puts "Host: #{host}"
96+
97+
begin
98+
loop do
99+
iterations_counter += 1
100+
101+
if monitor.file_changed?
102+
puts "Iteration #{iterations_counter} - File changed, processing new lines..."
103+
104+
new_lines = monitor.read_new_lines
105+
106+
if new_lines.empty?
107+
puts "No new lines to process"
108+
else
109+
puts "Processing #{new_lines.count} new lines..."
110+
111+
new_lines.each do |line|
112+
begin
113+
if result = parser.call(line)
114+
response = http_adapter.send_data(result)
115+
puts "Sent: #{result.entry_type} - #{response.code}"
116+
processed_lines += 1
117+
end
118+
rescue LogsParser::HttpAdapter::NetworkError, LogsParser::HttpAdapter::ServerError => e
119+
puts "Error sending data: #{e.class} - #{e.message}"
120+
puts "Will retry on next iteration"
121+
end
122+
end
123+
end
124+
125+
puts "Current position: #{monitor.instance_variable_get(:@file_position)}"
126+
puts "Total processed lines: #{processed_lines}"
127+
else
128+
puts "Iteration #{iterations_counter} - No changes detected"
129+
end
130+
131+
sleep 5
132+
end
133+
rescue Interrupt
134+
puts "\nShutting down gracefully..."
135+
ensure
136+
monitor.close
137+
puts "Final position: #{monitor.instance_variable_get(:@file_position)}"
138+
puts "Total processed lines: #{processed_lines}"
139+
end
140+
end
141+
142+
if __FILE__ == $0
143+
main
144+
end

0 commit comments

Comments
 (0)