class Turnstile::Collector::LogReader

Attributes

file[RW]
filename[RW]
matcher[RW]
should_reopen[RW]

Public Class Methods

new(log_file:, matcher:, **opts) click to toggle source
Calls superclass method Turnstile::Collector::Actor::new
# File lib/turnstile/collector/log_reader.rb, line 14
def initialize(log_file:, matcher:, **opts)
  super(**opts)
  self.matcher  = matcher
  self.filename = log_file
  self.should_reopen = false
  open_and_watch(opts[:tail] ? opts[:tail].to_i : 0)

  reader = self
  Signal.trap('HUP') { reader.should_reopen = true }
end

Private Class Methods

colon_delimited(file, queue, **opts) click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 94
def colon_delimited(file, queue, **opts)
  new(log_file: file,
      queue:    queue,
      matcher:  delimited_matcher(':'),
      **opts)
end
comma_delimited(file, queue, **opts) click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 87
def comma_delimited(file, queue, **opts)
  new(log_file: file,
      queue:    queue,
      matcher:  delimited_matcher(','),
      **opts)
end
custom(file, queue, **opts) click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 73
def custom(file, queue, **opts)
  new(log_file: file,
      queue:    queue,
      matcher:  custom_matcher,
      **opts)
end
default(file, queue, **opts)
Alias for: pipe_delimited
delimited(file, queue, **opts) click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 101
def delimited(file, queue, **opts)
  new(log_file: file,
      queue:    queue,
      matcher:  delimited_matcher(opts[:delimiter]),
      **opts)
end
json_formatted(file, queue, **opts) click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 108
def json_formatted(file, queue, **opts)
  new(log_file: file,
      queue:    queue,
      matcher:  json_matcher,
      **opts)
end
pipe_delimited(file, queue, **opts) click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 80
def pipe_delimited(file, queue, **opts)
  new(log_file: file,
      queue:    queue,
      matcher:  delimited_matcher,
      **opts)
end
Also aliased as: default

Public Instance Methods

close() click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 51
def close
  (file.close rescue nil) if file
end
execute() click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 32
def execute
  self.read do |token|
    self.queue << token if token
  end
rescue IOError
  open_and_watch if File.exist?(filename)
ensure
  close
end
read() { |token| ... } click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 42
def read(&_block)
  file.tail do |line|
    token = matcher.tokenize(line) if matcher
    yield(token) if block_given? && token
    break if stopping?
    reopen if should_reopen?
  end
end
reopen(a_file = nil) click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 25
def reopen(a_file = nil)
  self.should_reopen = false
  close rescue nil
  self.filename = a_file if a_file
  open_and_watch(0)
end

Private Instance Methods

open_and_watch(tail_lines = 0) click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 57
def open_and_watch(tail_lines = 0)
  self.file = LogFile.new(filename)

  file.interval = 1
  file.backward(0) if tail_lines == 0
  file.backward(tail_lines) if tail_lines > 0
  file.forward(0) if tail_lines == -1
end
should_reopen?() click to toggle source
# File lib/turnstile/collector/log_reader.rb, line 66
def should_reopen?
  should_reopen
end