Skip to content

Commit

Permalink
Allow connection option to be callable
Browse files Browse the repository at this point in the history
This change allows :connection option to be callable and avoid calling
start on bunny sessions.
  • Loading branch information
Ivan Trubach committed Jan 24, 2025
1 parent 47e5301 commit d5cbe23
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
14 changes: 9 additions & 5 deletions lib/sneakers/publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/sneakers/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit d5cbe23

Please sign in to comment.