Sha256: ce64c67fbd491a49900a8194c6c917499f6feb178c1d0ba19087ec07e654df59

Contents?: true

Size: 980 Bytes

Versions: 2

Compression:

Stored size: 980 Bytes

Contents

require 'database_stalker/util'

module DatabaseStalker
  class LogStalker

    def initialize(stalked_file_path, stalking_result_path)
      @stalked_file_path = stalked_file_path
      @stalking_result_path = stalking_result_path
    end

    def run
      @runned_tails = Util.runned_tail_pids
      spawn("tail -f -n 0 #{@stalked_file_path} > #{@stalking_result_path}")
      wait_for_tail_process_runninng
    end

    def stop
      wait_for_tail_process_output
      current_runned_tails = Util.runned_tail_pids
      (current_runned_tails - @runned_tails).each do |pid|
        Process.kill('KILL', pid)
      end
    end

    def result
      result = []
      File.open(@stalking_result_path) do |file|
        file.each_line do |line|
          result << line
        end
      end
      result
    end


    private

      def wait_for_tail_process_runninng
        sleep(0.01)
      end

      def wait_for_tail_process_output
        sleep(0.01)
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
database_stalker-0.1.4 lib/database_stalker/log_stalker.rb
database_stalker-0.1.3 lib/database_stalker/log_stalker.rb