Skip to content

Commit 05dbd9c

Browse files
committedMar 1, 2019
Merge branch 'jbrady42-handler_queue_config'
2 parents e8161e8 + 2ca56c8 commit 05dbd9c

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed
 

‎examples/max_retry_handler.rb

+2-12
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@
3333
#
3434
class MaxRetryWorker
3535
include Sneakers::Worker
36-
from_queue 'downloads',
37-
WORKER_OPTIONS.merge({
38-
:arguments => {
39-
:'x-dead-letter-exchange' => 'downloads-retry'
40-
},
41-
})
36+
from_queue 'downloads', WORKER_OPTIONS
4237

4338
def work(msg)
4439
logger.info("MaxRetryWorker rejecting msg: #{msg.inspect}")
@@ -52,12 +47,7 @@ def work(msg)
5247
# see the message once.
5348
class SucceedingWorker
5449
include Sneakers::Worker
55-
from_queue 'uploads',
56-
WORKER_OPTIONS.merge({
57-
:arguments => {
58-
:'x-dead-letter-exchange' => 'uploads-retry'
59-
},
60-
})
50+
from_queue 'uploads', WORKER_OPTIONS
6151

6252
def work(msg)
6353
logger.info("SucceedingWorker succeeding on msg: #{msg.inspect}")

‎lib/sneakers/handlers/maxretry.rb

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ def initialize(channel, queue, opts)
8282

8383
end
8484

85+
def self.configure_queue(name, opts)
86+
retry_name = opts.fetch(:retry_exchange, "#{name}-retry")
87+
opts.merge(arguments: { "x-dead-letter-exchange": retry_name })
88+
end
89+
8590
def acknowledge(hdr, props, msg)
8691
@channel.acknowledge(hdr.delivery_tag, false)
8792
end

‎lib/sneakers/queue.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ def subscribe(worker)
3131
routing_key = @opts[:routing_key] || @name
3232
routing_keys = [*routing_key]
3333

34-
# TODO: get the arguments from the handler? Retry handler wants this so you
35-
# don't have to line up the queue's dead letter argument with the exchange
36-
# you'll create for retry.
34+
handler_klass = worker.opts[:handler] || Sneakers::CONFIG.fetch(:handler)
35+
# Configure options if needed
36+
if handler_klass.respond_to?(:configure_queue)
37+
@opts[:queue_options] = handler_klass.configure_queue(@name, @opts[:queue_options])
38+
end
39+
3740
queue = @channel.queue(@name, @opts[:queue_options])
3841

3942
if exchange_name.length > 0
@@ -50,7 +53,6 @@ def subscribe(worker)
5053
# has the same configuration as the worker. Also pass along the exchange and
5154
# queue in case the handler requires access to them (for things like binding
5255
# retry queues, etc).
53-
handler_klass = worker.opts[:handler] || Sneakers::CONFIG.fetch(:handler)
5456
handler = handler_klass.new(@channel, queue, worker.opts)
5557

5658
@consumer = queue.subscribe(block: false, manual_ack: @opts[:ack]) do | delivery_info, metadata, msg |

0 commit comments

Comments
 (0)