Class: SuperPoller::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/super_poller/stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(router, out = STDERR, interval = 60) ⇒ Stats

Returns a new instance of Stats.



2
3
4
5
6
7
8
9
# File 'lib/super_poller/stats.rb', line 2

def initialize(router, out = STDERR, interval = 60)
  @router = router
  @out = out
  @semaphore = Mutex.new
  @interval = interval
  reset
  ensure_worker_is_running
end

Instance Method Details

#call(msg) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/super_poller/stats.rb', line 24

def call(msg)
  ensure_worker_is_running
  @semaphore.synchronize do
    begin
      name = msg[:name]
      @counts[name] += 1
      start_time = Time.now
      @router.call(msg)

    ensure
      end_time = Time.now
      duration = end_time.to_f - start_time.to_f
      @durations[name] << duration
    end
  end
end

#ensure_worker_is_runningObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/super_poller/stats.rb', line 11

def ensure_worker_is_running
  return if @worker && @worker.alive?
  @worker.join if @worker

  @worker = Thread.start{
    Thread.abort_on_exception = true
    while true
      flush
      sleep(@interval)
    end
  }
end