Class: NginxTop::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/nginxtop/listener.rb

Constant Summary collapse

START_TIME =
Time.now

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Listener

Returns a new instance of Listener.



6
7
8
9
10
# File 'lib/nginxtop/listener.rb', line 6

def initialize(options)
  @log_file = options[:file]
  @no_watch = options[:no_watch]
  install_signal_handlers
end

Instance Method Details

#install_signal_handlersObject



43
44
45
46
47
48
# File 'lib/nginxtop/listener.rb', line 43

def install_signal_handlers
  Signal.trap('INT') do
    puts 'Caught interrupt! Stopping parsing...'
    exit 130
  end
end

#listenObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nginxtop/listener.rb', line 12

def listen

  File.readlines(@log_file).each do |line|
    Parser.new.parse(line.chomp)
  end

  Output.create

  file = File.open(@log_file)
  file.seek(0,IO::SEEK_END)
  thread1 = Thread.new do
    loop do
      select([file])
      line = file.gets
      Parser.new.parse(line.chomp) if line
    end
  end

  thread2 = Thread.new do
    loop do
      sleep 1
      Output.create
    end
  end
  unless @no_watch
    thread1.join
    thread2.join
  end

end