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

[WIP] Add option for making consumer exclusive #388

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/sneakers/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Configuration
:threads => 10,
:share_threads => false,
:ack => true,
:exclusive => false,
:heartbeat => 30,
:hooks => {},
:exchange => 'sneakers',
Expand Down
2 changes: 1 addition & 1 deletion lib/sneakers/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def subscribe(worker)
# retry queues, etc).
handler = handler_klass.new(@channel, queue, worker.opts)

@consumer = queue.subscribe(block: false, manual_ack: @opts[:ack]) do | delivery_info, metadata, msg |
@consumer = queue.subscribe(block: false, manual_ack: @opts[:ack], exclusive: @opts[:exclusive]) do | delivery_info, metadata, msg |
worker.do_work(delivery_info, metadata, msg, handler)
end
nil
Expand Down
22 changes: 17 additions & 5 deletions spec/sneakers/queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{
:prefetch => 25,
:ack => true,
:exclusive => false,
:heartbeat => 2,
:vhost => '/',
:exchange => "sneakers",
Expand Down Expand Up @@ -57,7 +58,18 @@
q = Sneakers::Queue.new("downloads", queue_vars)

mock(@mkqueue).bind(@mkex, :routing_key => "downloads")
mock(@mkqueue).subscribe(:block => false, :manual_ack => true)
mock(@mkqueue).subscribe(:block => false, :manual_ack => true, exclusive: false)

q.subscribe(@mkworker)
end

it "supports exclusive consumers" do
mock(@mkchan).queue("downloads", :durable => true) { @mkqueue }
q = Sneakers::Queue.new("downloads",
queue_vars.merge(:exclusive => true))

mock(@mkqueue).bind(@mkex, :routing_key => "downloads")
mock(@mkqueue).subscribe(:block => false, :manual_ack => true, exclusive: true)

q.subscribe(@mkworker)
end
Expand All @@ -69,7 +81,7 @@

mock(@mkqueue).bind(@mkex, :routing_key => "alpha")
mock(@mkqueue).bind(@mkex, :routing_key => "beta")
mock(@mkqueue).subscribe(:block => false, :manual_ack => true)
mock(@mkqueue).subscribe(:block => false, :manual_ack => true, exclusive: false)

q.subscribe(@mkworker)
end
Expand All @@ -80,7 +92,7 @@
queue_vars.merge(:bind_arguments => { "os" => "linux", "cores" => 8 }))

mock(@mkqueue).bind(@mkex, :routing_key => "downloads", :arguments => { "os" => "linux", "cores" => 8 })
mock(@mkqueue).subscribe(:block => false, :manual_ack => true)
mock(@mkqueue).subscribe(:block => false, :manual_ack => true, exclusive: false)

q.subscribe(@mkworker)
end
Expand All @@ -104,7 +116,7 @@
q = Sneakers::Queue.new("test_nondurable", queue_vars)

mock(@mkqueue_nondurable).bind(@mkex, :routing_key => "test_nondurable")
mock(@mkqueue_nondurable).subscribe(:block => false, :manual_ack => true)
mock(@mkqueue_nondurable).subscribe(:block => false, :manual_ack => true, exclusive: false)

q.subscribe(@mkworker)
myqueue = q.instance_variable_get(:@queue)
Expand Down Expand Up @@ -153,7 +165,7 @@
queue_name = 'foo'
mock(@mkchan).queue(queue_name, :durable => true) { @mkqueue }
mock(@mkqueue).bind(@mkex, :routing_key => queue_name)
mock(@mkqueue).subscribe(:block => false, :manual_ack => true)
mock(@mkqueue).subscribe(:block => false, :manual_ack => true, exclusive: false)

my_vars = queue_vars.merge(:connection => @external_connection)
@q = Sneakers::Queue.new(queue_name, my_vars)
Expand Down
4 changes: 4 additions & 0 deletions spec/sneakers/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DummyWorker
:arguments => { 'x-arg' => 'value' }
},
:ack => false,
:exclusive => true,
:threads => 50,
:prefetch => 40,
:exchange => 'dummy',
Expand Down Expand Up @@ -177,6 +178,7 @@ def work(msg)
:threads => 10,
:share_threads => false,
:ack => true,
:exclusive => false,
:amqp => "amqp://guest:guest@localhost:5672",
:vhost => "/",
:exchange => "sneakers",
Expand Down Expand Up @@ -215,6 +217,7 @@ def work(msg)
:threads => 50,
:share_threads => false,
:ack => false,
:exclusive => true,
:amqp => "amqp://guest:guest@localhost:5672",
:vhost => "/",
:exchange => "dummy",
Expand Down Expand Up @@ -253,6 +256,7 @@ def work(msg)
:threads => 10,
:share_threads => false,
:ack => true,
:exclusive => false,
:amqp => "amqp://guest:guest@localhost:5672",
:vhost => "/",
:exchange => "sneakers",
Expand Down