Sha256: 5f458d76e1a0eda3bbe4a83f11a1cf99d450e100088340c97e5188d8b6fbfce0

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'file-tail'

module Spanx
  module Actor
    class LogReader
      attr_accessor :file, :queue, :whitelist

      def initialize file, queue, interval = 1, whitelist = nil
        @file = Spanx::Actor::File.new(file)
        @file.interval = interval
        @file.backward(0)
        @whitelist = whitelist
        @queue = queue
      end

      def run
        Thread.new do
          Thread.current[:name] = "log_reader"
          Logger.log "tailing the log file #{file.path}...."
          self.read do |line|
            queue << [line, Time.now.to_i ] if line
          end
        end
      end

      def read &block
        @file.tail do |line|
          block.call(extract_ip(line)) unless whitelist && whitelist.match?(line)
        end
      end

      def close
        (@file.close if @file) rescue nil
      end

      def extract_ip line
        matchers = line.match(/^((\d{1,3}\.?){4})/)
        matchers[1] unless matchers.nil?
      end
    end

    class File < ::File
      include ::File::Tail
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spanx-0.1.1 lib/spanx/actor/log_reader.rb
spanx-0.1.0 lib/spanx/actor/log_reader.rb