Class: Perfm::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/perfm/queue.rb

Constant Summary collapse

FLUSH_INTERVAL =
60
FLUSH_THRESHOLD =
100

Instance Method Summary collapse

Constructor Details

#initialize(storage) ⇒ Queue

Returns a new instance of Queue.



6
7
8
9
10
11
12
# File 'lib/perfm/queue.rb', line 6

def initialize(storage)
  @metrics = []
  @storage = storage
  @mutex = Mutex.new
  Kernel.at_exit { flush }
  start_thread
end

Instance Method Details

#collect_pending_metricsObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/perfm/queue.rb', line 21

def collect_pending_metrics
  result = nil
  mutex.synchronize do
    if @metrics.size > 0
      result = @metrics
      @metrics = []
    end
  end
  result
end

#flushObject



32
33
34
35
36
# File 'lib/perfm/queue.rb', line 32

def flush
  if data = collect_pending_metrics
    @storage.store(data)
  end
end

#flush_indefinitelyObject



38
39
40
41
42
# File 'lib/perfm/queue.rb', line 38

def flush_indefinitely
  while true
    sleep(FLUSH_INTERVAL) and flush
  end
end

#push_metrics(data) ⇒ Object



14
15
16
17
18
19
# File 'lib/perfm/queue.rb', line 14

def push_metrics(data)
  mutex.synchronize do
    @metrics.push(data)
    wakeup_thread if @metrics.size >= FLUSH_THRESHOLD || !thread.alive?
  end
end