class Carnivore::Files::Util::Fetcher::Penguin

NIO based fetcher

Attributes

notify[RW]

@return [SP::Inotify]

notify_descriptors[RW]

@return [Hash] registered file descriptors

Public Class Methods

new(args={}) click to toggle source

Create new instance

@param args [Hash] initialization arguments (unused)

Calls superclass method Carnivore::Files::Util::Fetcher::new
# File lib/carnivore-files/util/penguin.rb, line 18
def initialize(args={})
  super
  @notify = SP::Inotify.new
  @notify_descriptors = {}
end

Public Instance Methods

start_fetcher() click to toggle source

Start the fetcher

# File lib/carnivore-files/util/penguin.rb, line 25
def start_fetcher
  loop do
    build_io
    notify.each do |event|
      defer{ event.events }.each do |ev|
        case ev
        when :MODIFY
          retrieve_lines.each do |l|
            self.messages << l
          end
        when :MOVE_SELF, :DELETE_SELF, :ATTRIB
          info "Destroying file IO due to FS modification! (#{ev.inspect})"
          destroy_io
          @waited = true
          break
        else
          debug "Received unhandled event: #{ev.inspect}"
        end
      end
      break unless io
    end
  end
end

Private Instance Methods

build_io() click to toggle source

Build the IO and monitor

@return [TrueClass, FalseClass]

# File lib/carnivore-files/util/penguin.rb, line 54
def build_io
  result = super
  if(result)
    notify_descriptors[:file_watch] = notify.add_watch(path, :ALL_EVENTS)
  end
  result
end
destroy_io() click to toggle source

Destroy the IO and monitor

@return [TrueClass]

# File lib/carnivore-files/util/penguin.rb, line 65
def destroy_io
  if(io)
    notify.rm_watch(notify_descriptors.delete(:file_watch))
    @io.close
    @io = nil
  end
  true
end
wait_for_file() click to toggle source

Wait helper for file to appear (waits for expected notification)

@return [TrueClass]

# File lib/carnivore-files/util/penguin.rb, line 78
def wait_for_file
  until(::File.exists?(path))
    notified = false
    directory = ::File.dirname(path)
    notify_descriptors[:file_wait] = notify.add_watch(directory, :OPEN)
    until(notified)
      warn "Waiting for file to appear (#{path})"
      event = defer{ notify.take }
      notified = ::File.exists?(path)
    end
    notify.rm_watch(notify_descriptors.delete(:file_wait))
  end
  @waited = true
  info "File has appeared (#{path})!"
end