Class: Stooge::Handler
- Inherits:
-
Object
- Object
- Stooge::Handler
- Defined in:
- lib/stooge/handler.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#queue_name ⇒ Object
Returns the value of attribute queue_name.
-
#queue_options ⇒ Object
Returns the value of attribute queue_options.
Instance Method Summary collapse
-
#initialize(queue, options = {}) ⇒ Handler
constructor
Create a new handler object.
-
#run(payload, content_type, headers) ⇒ Object
Call the handler block.
-
#start(channel) ⇒ Object
Start subscribing to the queue that this handler corresponds to.
Constructor Details
#initialize(queue, options = {}) ⇒ Handler
Create a new handler object.
15 16 17 18 19 20 |
# File 'lib/stooge/handler.rb', line 15 def initialize(queue, = {}) @queue_name = queue @queue_options = [:queue_options] || {} @options = @block = lambda {} end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
4 5 6 |
# File 'lib/stooge/handler.rb', line 4 def block @block end |
#queue_name ⇒ Object
Returns the value of attribute queue_name.
4 5 6 |
# File 'lib/stooge/handler.rb', line 4 def queue_name @queue_name end |
#queue_options ⇒ Object
Returns the value of attribute queue_options.
4 5 6 |
# File 'lib/stooge/handler.rb', line 4 def @queue_options end |
Instance Method Details
#run(payload, content_type, headers) ⇒ Object
Call the handler block. This method will rescue any exceptions the handler block raises and pass them on to the global error handler.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/stooge/handler.rb', line 50 def run(payload, content_type, headers) Fiber.new do @block.call( decode_payload(payload, content_type), headers) end.resume rescue Object => e if Stooge.error_handler Stooge.error_handler.call(e,self,payload,headers) end end |
#start(channel) ⇒ Object
Start subscribing to the queue that this handler corresponds to. When a message arive; parse it and call the handler block with the data.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/stooge/handler.rb', line 28 def start(channel) Stooge.log "starting handler for #{@queue_name}" channel.queue(@queue_name, @queue_options) do |queue| queue.subscribe(:ack => true) do |, payload| Stooge.log "recv: #{@queue_name}" run(payload, .content_type, .headers) .ack end end end |