Skip to content

Commit 62cea58

Browse files
committed
Detect missing rabbitMQ queues when consuming from them
When consuming from a declared queue, and it is missing in rabbitMQ, mark it as a "pending" queue so it is re-declared when the consumer restarts. This requires passing the whole Broker object to the ConsumerThread, not just parameters.
1 parent 86650c0 commit 62cea58

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

dramatiq/brokers/rabbitmq.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def consume(self, queue_name: str, prefetch: int = 1, timeout: int = 5000) -> Co
264264
Consumer: A consumer that retrieves messages from RabbitMQ.
265265
"""
266266
self.declare_queue(queue_name, ensure=True)
267-
return self.consumer_class(self.parameters, queue_name, prefetch, timeout)
267+
return self.consumer_class(self, queue_name, prefetch, timeout)
268268

269269
def declare_queue(self, queue_name: str, *, ensure: bool = False) -> None:
270270
"""Declare a queue. Has no effect if a queue with the given
@@ -516,10 +516,12 @@ def filter(self, record):
516516

517517

518518
class _RabbitmqConsumer(Consumer):
519-
def __init__(self, parameters, queue_name, prefetch, timeout):
519+
def __init__(self, broker, queue_name, prefetch, timeout):
520+
self.broker = broker
521+
self.queue_name = queue_name
522+
self.logger = get_logger(__name__, type(self))
520523
try:
521-
self.logger = get_logger(__name__, type(self))
522-
self.connection = pika.BlockingConnection(parameters=parameters)
524+
self.connection = pika.BlockingConnection(parameters=self.broker.parameters)
523525
self.channel = self.connection.channel()
524526
self.channel.basic_qos(prefetch_count=prefetch)
525527
self.iterator = self.channel.consume(queue_name, inactivity_timeout=timeout / 1000)
@@ -585,6 +587,10 @@ def __next__(self):
585587
pika.exceptions.AMQPConnectionError,
586588
pika.exceptions.AMQPChannelError,
587589
) as e:
590+
# If the queue disappears, add it to the set of pending queues
591+
# so that it can be redeclared on when the consumer restarts.
592+
if getattr(e, "reply_code", None) == 404:
593+
self.broker.queues_pending.add(q_name(self.queue_name))
588594
raise ConnectionClosed(e) from None
589595

590596
try:

0 commit comments

Comments
 (0)