class Conrad::EmitterQueue

Centralized event emission queue across threads

Attributes

background[R]

Boolean that determines whether events will be emitted inline or in a

background thread
logger[RW]

Logger object used for sending log events

Public Class Methods

new() click to toggle source
# File lib/conrad/emitter_queue.rb, line 13
def initialize
  @thread = nil
  @queue = Queue.new
  @logger ||= Logger.new(STDOUT)
end

Public Instance Methods

background=(value) click to toggle source

bakground setter. Will start/stop the background thread @param value [Boolean] assigns whether events should be processed inline

or in a separate thread.
# File lib/conrad/emitter_queue.rb, line 22
def background=(value)
  @background = value
  value ? start_thread : @thread = nil
end
enqueue() { || ... } click to toggle source

Enqueues a block @yield block to execute. Will either run inline or separately depending on

whether the queue is backgrounded
# File lib/conrad/emitter_queue.rb, line 30
def enqueue
  @queue.push -> { yield }

  # if it's backgounded we can break out of here, as the background
  #   queue will pick it up. otherwise, we need to explicitly process it
  emit! unless @background
end

Private Instance Methods

emit!() click to toggle source
# File lib/conrad/emitter_queue.rb, line 54
def emit!
  @queue.pop.call until @queue.empty?
end
start_thread() click to toggle source
# File lib/conrad/emitter_queue.rb, line 40
def start_thread
  @thread ||= Thread.new do
    Thread.current.abort_on_exception = true
    loop do
      emit!
      break unless @background
    end
  end
rescue e
  logger.error(e)
  @thread = nil
  start_thread
end