Skip to content

Commit

Permalink
Add Eears#setup_consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
brerx authored and johannesluedke committed Oct 26, 2023
1 parent 8d1c02f commit 13a401b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
--require spec_helper
--format doc
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Metrics/ClassLength:

Metrics/MethodLength:
Enabled: true
Max: 12
Exclude:
- 'spec/**/*.rb'
- 'test/**/*.rb'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ PLATFORMS
arm64-darwin-20
arm64-darwin-21
arm64-darwin-22
ruby
x86_64-darwin-20
x86_64-darwin-21
x86_64-darwin-22
Expand Down
5 changes: 5 additions & 0 deletions lib/ears.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def setup(&block)
Ears::Setup.new.instance_eval(&block)
end

# Quick setup your consumers (including exchanges and queues).
def setup_consumers(*consumer_classes)
Ears::Setup.new.setup_consumers(*consumer_classes)
end

# Blocks the calling thread until +SIGTERM+ or +SIGINT+ is received.
# Used to keep the process alive while processing messages.
def run!
Expand Down
18 changes: 18 additions & 0 deletions lib/ears/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ def consumer(queue, consumer_class, threads = 1, args = {})
end
end

# Sets up consumers, including bindings to exchanges and queues.
#
# @param [Array<Class<Ears::Consumer>>] consumer_classes An array of subclasses of {Ears::Consumer} that call {Ears::Consumer#configure} in their class definition.
def setup_consumers(*consumer_classes)
consumer_classes.each do |consumer_class|
exchange =
exchange(
consumer_class.exchange,
consumer_class.exchange_type,
durable: consumer_class.durable_exchange,
)
configured_queue =
queue(consumer_class.queue, consumer_class.queue_options)
configured_queue.bind(exchange, routing_key: consumer_class.routing_key)
consumer(configured_queue, consumer_class)
end
end

private

def queue_options(bunny_opts, retry_arguments)
Expand Down
4 changes: 2 additions & 2 deletions spec/ears/consumer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
it 'provides a default for durable_exchange' do
custom_consumer_class.configure(mandatory_options)

expect(custom_consumer_class.durable_exchange).to eq(true)
expect(custom_consumer_class.durable_exchange).to be(true)
end

it 'provides a default for exchange_type' do
Expand Down Expand Up @@ -119,7 +119,7 @@
mandatory_options.merge({ durable_exchange: false }),
)

expect(custom_consumer_class.durable_exchange).to eq(false)
expect(custom_consumer_class.durable_exchange).to be(false)
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/ears_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@
end
end

describe '.setup_consumers' do
let(:klasses) { %i[first_class second_class] }

it 'calls the setup_consumers' do
setup = instance_double(Ears::Setup, setup_consumers: nil)
allow(Ears::Setup).to receive(:new).and_return(setup)
described_class.setup_consumers(*klasses)
expect(setup).to have_received(:setup_consumers).with(*klasses)
end
end

describe '.stop!' do
let(:bunny_session) do
instance_double(
Expand Down

0 comments on commit 13a401b

Please sign in to comment.