Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply custom consumer tag to consumer if supplied #93

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/march_hare/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,14 @@ def basic_get(queue, auto_ack)
end
end

def basic_consume(queue, auto_ack, consumer)
def basic_consume(queue, auto_ack, consumer_tag=nil, consumer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking API change but OK, we'll bump the version as it's better than any of the alternatives I can think of.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think is it really a breaking change?

if you call it with 3 parameter the last one will be consumer - this is the same behaviour as before.
if you call it with 4 you have both consumer_tag and consumer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, in a decade with Ruby I haven't seen a method where arguments with default values weren't at the end of the argument list. That indeed works as you describe:

2.2.2 :001 > def abc(a, b, c = nil, d)
2.2.2 :002?>   [a, b, c, d]
2.2.2 :003?>   end
 => :abc
2.2.2 :004 > abc(1, 2, 3, 4)
 => [1, 2, 3, 4]
2.2.2 :005 > abc(1, 2, 4)
 => [1, 2, nil, 4]

Perfect, I'm going to QA it right away then. Thank you.

consumer.auto_ack = auto_ack
tag = converting_rjc_exceptions_to_ruby do
@delegate.basic_consume(queue, auto_ack, consumer)
if consumer_tag
@delegate.basic_consume(queue, auto_ack, consumer_tag, consumer)
else
@delegate.basic_consume(queue, auto_ack, consumer)
end
end
self.register_consumer(tag, consumer)

Expand Down
2 changes: 1 addition & 1 deletion lib/march_hare/consumers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def terminated?
def recover_from_network_failure
@terminated.set(false)
@cancelled.set(false)
@consumer_tag = @channel.basic_consume(@queue.name, @auto_ack, self)
@consumer_tag = @channel.basic_consume(@queue.name, @auto_ack, @consumer_tag, self)

@consumer_tag
end
Expand Down
2 changes: 1 addition & 1 deletion lib/march_hare/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def subscribe(opts = {}, &block)
end

def subscribe_with(consumer, opts = {})
@consumer_tag = @channel.basic_consume(@name, !(opts[:ack] || opts[:manual_ack]), consumer)
@consumer_tag = @channel.basic_consume(@name, !(opts[:ack] || opts[:manual_ack]), opts[:consumer_tag], consumer)
consumer.consumer_tag = @consumer_tag

@default_consumer = consumer
Expand Down
18 changes: 18 additions & 0 deletions spec/higher_level_api/integration/basic_consume_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@
expect(consumer).not_to be_active
expect(consumer).to be_cancelled
end

it "has a consumer_tag" do
ch = connection.create_channel
q = ch.queue("", :exclusive => true)

consumer1 = q.subscribe(:blocking => false) { |_, _| nil }

sleep(1.0)
expect(consumer1.consumer_tag).to match(/^amq.ctag/)
consumer1.cancel

custom_consumer_tag = "unique_consumer_tag_#{rand(1_000)}"
consumer2 = q.subscribe(:consumer_tag => custom_consumer_tag, :blocking => false) { |_, _| nil }

expect(consumer2.consumer_tag).to eq(custom_consumer_tag)

consumer2.cancel
end
end


Expand Down