|
| 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 | + if ARGV.length < 3 |
| 76 | + puts "Usage: ruby pbs5.rb <game_name> <players_count> <pitboss_entries_password> [initial_position] [host]" |
| 77 | + puts "Example: ruby pbs5.rb my_game 4 secret_password 0 fierce-reaches-40697.herokuapp.com" |
| 78 | + exit 1 |
| 79 | + end |
| 80 | + |
| 81 | + game_name = ARGV[0] |
| 82 | + players_count = ARGV[1].to_i |
| 83 | + password = ARGV[2] |
| 84 | + initial_position = ARGV[3].to_i || 0 |
| 85 | + host = ARGV[4] || "fierce-reaches-40697.herokuapp.com" |
| 86 | + log_file_path = "net_message_debug.log" |
| 87 | + |
| 88 | + unless game_name && players_count > 0 && password |
| 89 | + puts "Error: game_name, players_count, and password are required" |
| 90 | + exit 1 |
| 91 | + end |
| 92 | + |
| 93 | + parser = LogsParser::Service.new(game_name, players_count) |
| 94 | + http_adapter = LogsParser::HttpAdapter.new(host: host, password: password) |
| 95 | + monitor = LogFileMonitor.new(log_file_path, initial_position) |
| 96 | + |
| 97 | + iterations_counter = 0 |
| 98 | + processed_lines = 0 |
| 99 | + |
| 100 | + puts "Starting log monitor for #{game_name} (#{players_count} players)" |
| 101 | + puts "Initial position: #{initial_position}" |
| 102 | + puts "Host: #{host}" |
| 103 | + puts "Authentication: enabled" |
| 104 | + |
| 105 | + begin |
| 106 | + loop do |
| 107 | + iterations_counter += 1 |
| 108 | + |
| 109 | + if monitor.file_changed? |
| 110 | + puts "Iteration #{iterations_counter} - File changed, processing new lines..." |
| 111 | + |
| 112 | + new_lines = monitor.read_new_lines |
| 113 | + |
| 114 | + if new_lines.empty? |
| 115 | + puts "No new lines to process" |
| 116 | + else |
| 117 | + puts "Processing #{new_lines.count} new lines..." |
| 118 | + |
| 119 | + new_lines.each do |line| |
| 120 | + begin |
| 121 | + if result = parser.call(line) |
| 122 | + response = http_adapter.send_data(result) |
| 123 | + puts "Sent: #{result.entry_type} - #{response.code}" |
| 124 | + processed_lines += 1 |
| 125 | + end |
| 126 | + rescue LogsParser::HttpAdapter::NetworkError, LogsParser::HttpAdapter::ServerError => e |
| 127 | + puts "Error sending data: #{e.class} - #{e.message}" |
| 128 | + puts "Will retry on next iteration" |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + puts "Current position: #{monitor.instance_variable_get(:@file_position)}" |
| 134 | + puts "Total processed lines: #{processed_lines}" |
| 135 | + else |
| 136 | + puts "Iteration #{iterations_counter} - No changes detected" |
| 137 | + end |
| 138 | + |
| 139 | + sleep 5 |
| 140 | + end |
| 141 | + rescue Interrupt |
| 142 | + puts "\nShutting down gracefully..." |
| 143 | + ensure |
| 144 | + monitor.close |
| 145 | + puts "Final position: #{monitor.instance_variable_get(:@file_position)}" |
| 146 | + puts "Total processed lines: #{processed_lines}" |
| 147 | + end |
| 148 | +end |
| 149 | + |
| 150 | +if __FILE__ == $0 |
| 151 | + main |
| 152 | +end |
0 commit comments