Class: ReliableMsg::CachedHeaders

Inherits:
Object
  • Object
show all
Defined in:
lib/reliable-msg/queue.rb

Overview

Locally cached headers for a queue. Used with the Selector object to retrieve the headers once, and share them. This effectively acts as a cursor into the queue, and saves I/O by retrieving a new list only when it’s empty.

Instance Method Summary collapse

Constructor Details

#initializeCachedHeaders

:nodoc:



310
311
312
313
# File 'lib/reliable-msg/queue.rb', line 310

def initialize
    @list = nil
    @mutex = Mutex.new
end

Instance Method Details

#next(selector, &block) ⇒ Object

Find the next matching message in the queue based on the selector. The argument is a Selector object which filters out messages. The block is called to load a list of headers from the queue manager, returning an Array of headers (Hash). Returns the identifier of the first message found.

:call-seq:

obj.next(selector) { } -> id or nil


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/reliable-msg/queue.rb', line 325

def next selector, &block
    load = false
    @mutex.synchronize do
        load ||= (@list.nil? || @list.empty?)
        @list = block.call() if load
        @list.each_with_index do |headers, idx|
            if selector.match headers
                @list.delete_at idx
                return headers[:id]
            end
        end
        unless load
            load = true
            retry
        end
    end
    return nil
end