Method: Cod::Beanstalk::Channel#try_get

Defined in:
lib/cod/beanstalk/channel.rb

#try_get {|Object, Cod::Beanstalk::Channel::Control| ... } ⇒ Object

Like #get, read next message from the channel but reserve the right to put it back. This uses beanstalkds flow control features to be able to control message flow in the case of exceptions and the like.

If the block given to this message raises an exception, the message is released unless a control command has been given. This means that other workers on the same tube will get the chance to see the message.

If the block is exited without specifying a fate for the message, it is deleted from the tube.

Examples:

All the flow control that beanstalkd allows

channel.try_get { |msg, ctl|
  if msg == 1
    ctl.release # don't handle messages of type 1
  else
    ctl.bury    # for example
  end
}

Exceptions release the message

# Will release the message and allow other connected channels to 
# #get it.
channel.try_get { |msg, ctl|
  fail "No such message handler"
}

Yields:

See Also:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cod/beanstalk/channel.rb', line 125

def try_get 
  fail "No block given to #try_get" unless block_given?
  
  id, msg = bs_reserve
  control = Control.new(id, self)
  
  begin
    retval = yield(deserialize(msg), control)
  rescue Exception
    control.release unless control.command_given?
    raise
  ensure
    control.delete unless control.command_given?
  end
  
  return retval
end