Class: Sneakers::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/sneakers/queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts) ⇒ Queue

Returns a new instance of Queue.



5
6
7
8
9
# File 'lib/sneakers/queue.rb', line 5

def initialize(name, opts)
  @name = name
  @opts = opts
  @handler_klass = Sneakers::CONFIG[:handler]
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



3
4
5
# File 'lib/sneakers/queue.rb', line 3

def channel
  @channel
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



3
4
5
# File 'lib/sneakers/queue.rb', line 3

def exchange
  @exchange
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/sneakers/queue.rb', line 3

def name
  @name
end

#optsObject (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
59
60
61
62
63
# File 'lib/sneakers/queue.rb', line 18

def subscribe(worker)
  # If we've already got a bunny object, use it.  This allows people to
  # specify all kinds of options we don't need to know about (e.g. for ssl).
  @bunny = @opts[:connection]
  @bunny ||= create_bunny_connection
  @bunny.start

  @channel = @bunny.create_channel
  @channel.prefetch(@opts[:prefetch])

  exchange_name = @opts[:exchange]
  @exchange = @channel.exchange(exchange_name, **@opts[:exchange_options])

  routing_key = @opts[:routing_key] || @name
  routing_keys = [*routing_key]

  handler_klass = worker.opts[:handler] || Sneakers::CONFIG.fetch(:handler)
  # Configure options if needed
  if handler_klass.respond_to?(:configure_queue)
    @opts[:queue_options] = handler_klass.configure_queue(@name, @opts)
  end

  queue = @channel.queue(@name, **@opts[:queue_options])

  should_bind = @opts.fetch(:bind, true)
  if should_bind && exchange_name.length > 0
    routing_keys.each do |key|
      if @opts[:bind_arguments]
        queue.bind(@exchange, routing_key: key, arguments: @opts[:bind_arguments])
      else
        queue.bind(@exchange, routing_key: key)
      end
    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 = 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

#unsubscribeObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sneakers/queue.rb', line 65

def unsubscribe
  return unless @consumer

  # TODO: should we simply close the channel here?
  Sneakers.logger.info("Queue: will try to cancel consumer #{@consumer.inspect}")
  cancel_ok = @consumer.cancel
  if cancel_ok
    Sneakers.logger.info "Queue: consumer #{cancel_ok.consumer_tag} cancelled"
    @consumer = nil
  else
    Sneakers.logger.warn "Queue: could not cancel consumer #{@consumer.inspect}"
    sleep(1)
    unsubscribe
  end
end