Skip to content

Commit

Permalink
multiple-queues-per-worker Add multiple queues per worker support
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Cantero committed Dec 13, 2015
1 parent 4a124b2 commit b6f9d5c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
24 changes: 17 additions & 7 deletions lib/shoryuken/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ def server_middleware

def shoryuken_options(opts = {})
@shoryuken_options = get_shoryuken_options.merge(stringify_keys(opts || {}))
queue = @shoryuken_options['queue']
if queue.respond_to? :call
queue = queue.call
@shoryuken_options['queue'] = queue
end

Shoryuken.register_worker(queue, self)
normalize_worker_queue!
end

def auto_visibility_timeout?
Expand All @@ -64,6 +58,22 @@ def stringify_keys(hash) # :nodoc:
end
hash
end

private

def normalize_worker_queue!
queue = @shoryuken_options['queue']
if queue.respond_to? :call
queue = queue.call
@shoryuken_options['queue'] = queue
end

[@shoryuken_options['queue']].flatten.compact.each(&method(:register_worker))
end

def register_worker(queue)
Shoryuken.register_worker(queue, self)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/integration/launcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'shoryuken/manager'
require 'shoryuken/launcher'

describe Shoryuken::Launcher do
RSpec.describe Shoryuken::Launcher do
describe 'Consuming messages', slow: :true do
before do
Shoryuken.options[:aws][:receive_message] = { wait_time_seconds: 5 }
Expand Down
16 changes: 14 additions & 2 deletions spec/shoryuken/worker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe 'Shoryuken::Worker' do
RSpec.describe 'Shoryuken::Worker' do
let(:sqs_queue) { double 'SQS Queue' }
let(:queue) { 'default' }

Expand Down Expand Up @@ -103,7 +103,7 @@
expect(Shoryuken.worker_registry.workers('default')).to eq([TestWorker])
end

it 'accepts a block as queue name' do
it 'accepts a block as a queue' do
$queue_prefix = 'production'

class NewTestWorker
Expand All @@ -116,6 +116,18 @@ class NewTestWorker
expect(NewTestWorker.get_shoryuken_options['queue']).to eq 'production_default'
end

it 'accepts an array as a queue' do
class WorkerMultipleQueues
include Shoryuken::Worker

shoryuken_options queue: %w[queue1 queue2 queue3]
end

expect(Shoryuken.worker_registry.workers('queue1')).to eq([WorkerMultipleQueues])
expect(Shoryuken.worker_registry.workers('queue2')).to eq([WorkerMultipleQueues])
expect(Shoryuken.worker_registry.workers('queue3')).to eq([WorkerMultipleQueues])
end

it 'is possible to configure the global defaults' do
queue = SecureRandom.uuid
Shoryuken.default_worker_options['queue'] = queue
Expand Down

0 comments on commit b6f9d5c

Please sign in to comment.