Class: Stooge::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/stooge/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue, options = {}) ⇒ Handler

Create a new handler object.

Parameters:

  • queue (String)

    the name of the queue that this handler will pick jobs from.

  • options (Hash) (defaults to: {})

    handler options.

Options Hash (options):

  • :queue_options (Hash)

    Options to use when creating the queue.



15
16
17
18
19
20
# File 'lib/stooge/handler.rb', line 15

def initialize(queue, options = {})
  @queue_name = queue
  @queue_options = options[:queue_options] || {}
  @options = options
  @block = lambda {}
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



4
5
6
# File 'lib/stooge/handler.rb', line 4

def block
  @block
end

#queue_nameObject

Returns the value of attribute queue_name.



4
5
6
# File 'lib/stooge/handler.rb', line 4

def queue_name
  @queue_name
end

#queue_optionsObject

Returns the value of attribute queue_options.



4
5
6
# File 'lib/stooge/handler.rb', line 4

def queue_options
  @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.

Parameters:

  • payload (String)

    the message payload

  • content_type (String)

    the MIME content type of the message payload

  • headers (Hash)

    the message headers

Returns:

  • (Object)

    the return value of the handler block



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.

Parameters:

  • channel (AMQP::Channel)

    an open AMQP channel



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