Class: Sneakers::Queue
- Inherits:
-
Object
- Object
- Sneakers::Queue
- Defined in:
- lib/sneakers/queue.rb
Instance Attribute Summary collapse
-
#exchange ⇒ Object
readonly
Returns the value of attribute exchange.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#initialize(name, opts) ⇒ Queue
constructor
A new instance of Queue.
-
#subscribe(worker) ⇒ Object
:exchange :heartbeat_interval :prefetch :durable :ack.
- #unsubscribe ⇒ Object
Constructor Details
Instance Attribute Details
#exchange ⇒ Object (readonly)
Returns the value of attribute exchange.
3 4 5 |
# File 'lib/sneakers/queue.rb', line 3 def exchange @exchange end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/sneakers/queue.rb', line 3 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
3 4 5 |
# File 'lib/sneakers/queue.rb', line 3 def opts @opts end |
Instance Method Details
#subscribe(worker) ⇒ Object
:exchange :heartbeat_interval :prefetch :durable :ack
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sneakers/queue.rb', line 18 def subscribe(worker) #@bunny = Bunny.new(@opts[:amqp], :vhost => @opts[:vhost], :heartbeat => @opts[:heartbeat], :logger => Sneakers::logger) @bunny = @opts[:connection] @bunny ||= Bunny.new(@opts[:amqp], :vhost => @opts[:vhost], :heartbeat => @opts[:heartbeat], :logger => Sneakers::logger) @bunny.start @channel = @bunny.create_channel @channel.prefetch(@opts[:prefetch]) exchange_name = @opts[:exchange] @exchange = @channel.exchange(exchange_name, :type => @opts[:exchange_type], :durable => @opts[:durable]) routing_key = @opts[:routing_key] || @name routing_keys = [*routing_key] # TODO: get the arguments from the handler? Retry handler wants this so you # don't have to line up the queue's dead letter argument with the exchange # you'll create for retry. queue_durable = @opts[:queue_durable].nil? ? @opts[:durable] : @opts[:queue_durable] queue = @channel.queue(@name, :durable => queue_durable, :arguments => @opts[:arguments]) if exchange_name.length > 0 routing_keys.each do |key| queue.bind(@exchange, :routing_key => key) end end # NOTE: we are using the worker's options. This is necessary so the handler # has the same configuration as the worker. Also pass along the exchange and # queue in case the handler requires access to them (for things like binding # retry queues, etc). handler_klass = worker.opts[:handler] || Sneakers::CONFIG.fetch(:handler) handler = handler_klass.new(@channel, queue, worker.opts) @consumer = queue.subscribe(:block => false, :manual_ack => @opts[:ack]) do | delivery_info, , msg | worker.do_work(delivery_info, , msg, handler) end nil end |
#unsubscribe ⇒ Object
60 61 62 63 64 |
# File 'lib/sneakers/queue.rb', line 60 def unsubscribe # XXX can we cancel bunny and channel too? @consumer.cancel if @consumer @consumer = nil end |