Class: PulseMeter::CommandAggregator::Async

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pulse-meter/command_aggregator/async.rb

Constant Summary collapse

MAX_QUEUE_LENGTH =
10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAsync

Returns a new instance of Async.



13
14
15
16
17
18
19
# File 'lib/pulse-meter/command_aggregator/async.rb', line 13

def initialize
  @max_queue_length = MAX_QUEUE_LENGTH
  @queue = Queue.new
  @buffer = []
  @in_multi = false
  @consumer_thread = run_consumer
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



29
30
31
32
# File 'lib/pulse-meter/command_aggregator/async.rb', line 29

def method_missing(*args)
  @buffer << args
  send_buffer_to_queue unless @in_multi
end

Instance Attribute Details

#max_queue_lengthObject (readonly)

Returns the value of attribute max_queue_length.



11
12
13
# File 'lib/pulse-meter/command_aggregator/async.rb', line 11

def max_queue_length
  @max_queue_length
end

Instance Method Details

#multiObject



21
22
23
24
25
26
27
# File 'lib/pulse-meter/command_aggregator/async.rb', line 21

def multi
  @in_multi = true
  yield
ensure
  @in_multi = false
  send_buffer_to_queue
end

#wait_for_pending_events(max_seconds = 1) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/pulse-meter/command_aggregator/async.rb', line 34

def wait_for_pending_events(max_seconds = 1)
  left_to_wait = max_seconds.to_f
  sleep_step = 0.01
  while has_pending_events? && left_to_wait > 0
    left_to_wait -= sleep_step
    sleep(sleep_step)
  end
end