Class: DLogReader::LogReader
- Inherits:
-
Object
- Object
- DLogReader::LogReader
- Defined in:
- lib/distributed_logreader/log_reader.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
- #statefile ⇒ Object
Instance Method Summary collapse
-
#initialize(filename, &b) ⇒ LogReader
constructor
A new instance of LogReader.
- #run ⇒ Object
Constructor Details
#initialize(filename, &b) ⇒ LogReader
Returns a new instance of LogReader.
7 8 9 10 |
# File 'lib/distributed_logreader/log_reader.rb', line 7 def initialize(filename, &b) self.filename = filename @b = b end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
4 5 6 |
# File 'lib/distributed_logreader/log_reader.rb', line 4 def filename @filename end |
#statefile ⇒ Object
36 37 38 39 40 41 |
# File 'lib/distributed_logreader/log_reader.rb', line 36 def statefile @statefile ||= begin log_basename = File.basename(filename) File.join("/tmp", "log_state_#{log_basename}") end end |
Instance Method Details
#run ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/distributed_logreader/log_reader.rb', line 12 def run # raise IOError.new("no file given") if filename.nil? raise IOError.new("File not readable") unless File.readable?(filename) f = File.open(filename, "r+") load_saved_state(f) # raise IOError.new("File is locked") unless f.flock(File::LOCK_EX | File::LOCK_NB) unless f.eof? last_report = Time.now line_count = 0 f.each_line do |line| @b.call(line) line_count += 1 if (line_count % 100 == 0) time_passed = Time.now - last_report $dlog_logger.info( "#{Time.now.to_s} #{filename}: Processed (#{line_count}) lines in #{time_passed}s [#{(line_count.to_f / time_passed.to_f).to_i} lines/s]") last_report = Time.now save_state(f) end end save_state(f) end # f.flock(File::LOCK_UN) end |