module QueueWrangler::Wrangler

Attributes

queue[RW]

Public Instance Methods

dead?() click to toggle source

Determine whether the runloop is dead or not

# File lib/queuewrangler/wrangler.rb, line 24
def dead?
  @should_die
end
die!() click to toggle source

Instruct the runloop to die and flush our internal queue of events

# File lib/queuewrangler/wrangler.rb, line 29
def die!
  @should_die = true
  process_queue
end
enqueue(method, *args) click to toggle source
# File lib/queuewrangler/wrangler.rb, line 68
def enqueue(method, *args)
  @queue << [method, [*args]]
end
initialize_queuewrangler(enabled, logger=nil) click to toggle source

Initializes the queue and performs other prepatory work required before queue processing may begin.

@param [Boolean] enabled True if the queue should be processed @param [Logger] logger Where to log or no logging if not specified

# File lib/queuewrangler/wrangler.rb, line 12
def initialize_queuewrangler(enabled, logger=nil)
  @enabled = enabled
  @queue = Queue.new
  @should_die = false
  @logger = logger || Logger.new(nil)
end
log() click to toggle source
# File lib/queuewrangler/wrangler.rb, line 19
def log
  @logger
end
process_queue() click to toggle source

Flush the outstanding events in the events queue

# File lib/queuewrangler/wrangler.rb, line 35
def process_queue
  until @queue.empty?
    method, args = @queue.pop

    unless method.nil?
      log.info("process_queue - handling #{method} with #{args.inspect}")
      if @enabled
        processed = send(method, *args)

        unless processed
          log.info "Failed to process #{method}"
          enqueue(method, *args)
          # Arbitrary sleep to make sure we don't spin when we cannot
          # process a message infinitly
          sleep 1
        end
      end
    end
  end
end
run!() click to toggle source
# File lib/queuewrangler/wrangler.rb, line 56
def run!
  id = self.class.name.split('::').last
  log.info "#{id}#runloop - starting the runloop, processing: #{@enabled}"
  until dead?
    process_queue
    # Arbitrary sleep just to make sure we don't spin on
    # #process_queue when the queue is completely empty
    sleep 0.5
  end
  log.info "#{id}#runloop - ending the runloop"
end