class DucksboardReporter::FileTail

Attributes

name[RW]
options[RW]
timestamp[RW]
value[RW]

Public Class Methods

new(path) click to toggle source
# File lib/ducksboard_reporter/file_tail.rb, line 8
def initialize(path)
  @path = path
end

Public Instance Methods

on_line(&block) click to toggle source
# File lib/ducksboard_reporter/file_tail.rb, line 28
def on_line(&block)
  @line_block = block
end
run() click to toggle source
# File lib/ducksboard_reporter/file_tail.rb, line 12
def run
  open_file

  while true do
    IO.select([@file])
    line = @file.gets

    @line_block.call(line)

    raise FileNotReadyForReadError if line.nil? && !file_ready_for_read?
  end
rescue Errno::ENOENT, FileNotReadyForReadError
  sleep 0.5
  retry
end

Private Instance Methods

file_ready_for_read?() click to toggle source
# File lib/ducksboard_reporter/file_tail.rb, line 54
def file_ready_for_read?
  @file && !@file.closed? && File.stat(@path).ino == @file.stat.ino
end
log_format(msg) click to toggle source
# File lib/ducksboard_reporter/file_tail.rb, line 49
def log_format(msg)
  @log_prefix ||= "FileTail (#{@path}): "
  @log_prefix + msg
end
open_file() click to toggle source
# File lib/ducksboard_reporter/file_tail.rb, line 34
def open_file
  if @file && !@file.closed?
    if (new_inode = File.stat(@path).ino) == @file.stat.ino
      return
    else
      debug(log_format("Inode changed (#{@file.stat.ino} => #{new_inode}). Closing file."))
      @file.close
    end
  end

  @file = File.open(@path, "r")
  @file.seek(0, IO::SEEK_END)
  debug(log_format("Tailing file with inode #{@file.stat.ino}"))
end