class RackRabbit::Subscriber
Attributes
config[R]
handler[R]
lock[R]
logger[R]
rabbit[R]
Public Class Methods
new(rabbit, handler, lock, config)
click to toggle source
# File lib/rack-rabbit/subscriber.rb, line 17 def initialize(rabbit, handler, lock, config) @rabbit = rabbit @handler = handler @lock = lock @config = config @logger = config.logger end
Public Instance Methods
subscribe()
click to toggle source
# File lib/rack-rabbit/subscriber.rb, line 27 def subscribe rabbit.startup rabbit.connect rabbit.subscribe(:queue => config.queue, :exchange => config.exchange, :exchange_type => config.exchange_type, :routing_key => config.routing_key, :manual_ack => config.ack) do |message| lock.synchronize do start = Time.now response = handle(message) finish = Time.now logger.info "\"#{message.method} #{message.path}\" [#{response.status}] - #{"%.4f" % (finish - start)}" end end end
unsubscribe()
click to toggle source
# File lib/rack-rabbit/subscriber.rb, line 44 def unsubscribe rabbit.disconnect rabbit.shutdown end
Private Instance Methods
handle(message)
click to toggle source
¶ ↑
PRIVATE IMPLEMENTATION
¶ ↑
# File lib/rack-rabbit/subscriber.rb, line 55 def handle(message) response = handler.handle(message) # does all the Rack related work if message.should_reply? rabbit.publish(response.body, message.get_reply_properties(response, config)) end if config.ack && !message.acknowledged? && !message.rejected? if response.succeeded? message.ack else message.reject end end response end