From d5cbe23174e93beee6a0d101e0eaf8d0d21c26d7 Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Fri, 24 Jan 2025 14:25:11 +0300 Subject: [PATCH] Allow connection option to be callable This change allows :connection option to be callable and avoid calling start on bunny sessions. --- lib/sneakers/publisher.rb | 14 +++++++++----- lib/sneakers/queue.rb | 8 ++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/sneakers/publisher.rb b/lib/sneakers/publisher.rb index e778362..64ab6cf 100644 --- a/lib/sneakers/publisher.rb +++ b/lib/sneakers/publisher.rb @@ -6,9 +6,6 @@ class Publisher def initialize(opts = {}) @mutex = Mutex.new @opts = Sneakers::CONFIG.merge(opts) - # 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] end def publish(msg, options = {}) @@ -29,8 +26,15 @@ def ensure_connection! private def connect! - @bunny ||= create_bunny_connection - @bunny.start + # 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] + if @bunny.respond_to?(:call) + @bunny = @bunny.call + else + @bunny ||= create_bunny_connection + @bunny.start + end @channel = @bunny.create_channel @exchange = @channel.exchange(@opts[:exchange], **@opts[:exchange_options]) end diff --git a/lib/sneakers/queue.rb b/lib/sneakers/queue.rb index 3ed83ee..0bba10d 100644 --- a/lib/sneakers/queue.rb +++ b/lib/sneakers/queue.rb @@ -19,8 +19,12 @@ 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 + if @bunny.respond_to?(:call) + @bunny = @bunny.call + else + @bunny ||= create_bunny_connection + @bunny.start + end @channel = @bunny.create_channel @channel.prefetch(@opts[:prefetch])