fix: Enhance run_task method to handle existing RabbitMQ exchanges by attempting passive read before declaring new ones.

This commit is contained in:
Ari Karim 2026-01-18 14:30:07 +03:00
parent 8f8882c428
commit d553042d1c
No known key found for this signature in database
GPG Key ID: 1A5559E2E32F1805

View File

@ -79,7 +79,15 @@ module RabbitCarrots
def run_task(queue_name:, handler_class:, routing_keys:, queue_arguments: {}, exchange_name: nil, kill_to_restart_on_standard_error: false)
RabbitCarrots::Connection.instance.channel.with do |channel|
exchange = channel.topic(exchange_name || RabbitCarrots.configuration.rabbitmq_exchange_name, durable: true)
exchange_name ||= RabbitCarrots.configuration.rabbitmq_exchange_name
begin
# Try to passively read an existing exchange without declaring it
exchange = channel.topic(exchange_name, passive: true)
rescue Bunny::NotFound
# If the exchange does not exist, declare it
exchange = channel.topic(exchange_name, durable: true)
end
logger.info "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}"
queue = channel.queue(queue_name, durable: true, arguments: queue_arguments)