Class: DLogReader::LogReader

Inherits:
Object
  • Object
show all
Defined in:
lib/distributed_logreader/log_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#filenameObject

Returns the value of attribute filename.



4
5
6
# File 'lib/distributed_logreader/log_reader.rb', line 4

def filename
  @filename
end

#statefileObject



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

#runObject

Raises:

  • (IOError)


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