Class: ReliableMsg::Selector

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

Overview

A message selector is used to retrieve specific messages from the queue by matching the message headers.

The selector matches messages by calling the block for each potential message. The block can access (read-only) message headers by calling methods with the same name, or using [:symbol]. It returns true if a match is found.

The following three examples are equivalent:

selector = Queue::selector { priority > 2 }
selector = queue.selector { priority > 2 }
selector = Selector.new { [:priority] > 2 }

The new function is always available and evaluates to the current time (in seconds from the Epoch).

This example uses the delivery count and message creation date/time to implement a simple retry with back-out:

MINUTE = 60
HOUR = MINUTE * 60
BACKOUT = [ 5 * MINUTE, HOUR, 4 * HOUR, 12 * HOUR ]

selector = Queue::selector { delivered == 0 || BACKOUT[delivered - 1] + created <= now }

Constant Summary collapse

ERROR_INVALID_SELECTOR_BLOCK =

:nodoc:

"Selector must be created with a block accepting no arguments"

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Selector

Create a new selector that evaluates by calling the block.

:call-seq:

Selector.new { |headers| ... } -> selector

Raises:

  • (ArgumentError)


50
51
52
53
# File 'lib/reliable-msg/selector.rb', line 50

def initialize &block
    raise ArgumentError, ERROR_INVALID_SELECTOR_BLOCK unless block && block.arity < 1
    @block = block
end

Instance Method Details

#match(headers) ⇒ Object

Matches the message headers with the selectors. Returns true if a match is made, false otherwise. May raise an error if there’s an error in the expression.

:call-seq:

selector.match(headers) -> boolean


63
64
65
66
# File 'lib/reliable-msg/selector.rb', line 63

def match headers #:nodoc:
    context = EvaluationContext.new headers
    context.instance_eval(&@block)
end