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

Add pool name option to worker pools #1188

Open
wants to merge 1 commit 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
9 changes: 5 additions & 4 deletions lib/delayed/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ def daemonize # rubocop:disable PerceivedComplexity

def setup_pools
worker_index = 0
@worker_pools.each do |queues, worker_count|
@worker_pools.each do |queues, worker_count, pool_name|
options = @options.merge(:queues => queues)
process_name_base = pool_name ? "delayed_job.#{pool_name}" : "delayed_job"
worker_count.times do
process_name = "delayed_job.#{worker_index}"
process_name = "#{process_name_base}.#{worker_index}"
run_process(process_name, options)
worker_index += 1
end
Expand Down Expand Up @@ -147,10 +148,10 @@ def run(worker_name = nil, options = {})
def parse_worker_pool(pool)
@worker_pools ||= []

queues, worker_count = pool.split(':')
queues, worker_count, pool_name = pool.split(':')
queues = ['*', '', nil].include?(queues) ? [] : queues.split(',')
worker_count = (worker_count || 1).to_i rescue 1
@worker_pools << [queues, worker_count]
@worker_pools << [queues, worker_count, pool_name]
end

def root
Expand Down
35 changes: 21 additions & 14 deletions spec/delayed/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,46 +127,53 @@

describe 'parsing --pool argument' do
it 'should parse --pool correctly' do
command = Delayed::Command.new(['--pool=*:1', '--pool=test_queue:4', '--pool=mailers,misc:2'])
command = Delayed::Command.new(['--pool=*:1:default_pool', '--pool=test_queue:4:test_pool', '--pool=mailers,misc:2'])

expect(command.worker_pools).to eq [
[[], 1],
[['test_queue'], 4],
[%w[mailers misc], 2]
[[], 1,"default_pool"],
[['test_queue'], 4,"test_pool"],
[%w[mailers misc], 2, nil]
]
end

it 'should allow * or blank to specify any pools' do
it 'should allow * or blank to specify any queue' do
command = Delayed::Command.new(['--pool=*:4'])
expect(command.worker_pools).to eq [
[[], 4],
[[], 4, nil],
]

command = Delayed::Command.new(['--pool=:4'])
expect(command.worker_pools).to eq [
[[], 4],
[[], 4, nil],
]
end

it 'should default to one worker if not specified' do
command = Delayed::Command.new(['--pool=mailers'])
expect(command.worker_pools).to eq [
[['mailers'], 1],
[['mailers'], 1, nil],
]
end

it 'pool name should default to nil if not specified' do
command = Delayed::Command.new(['--pool=mailers'])
expect(command.worker_pools).to eq [
[['mailers'], 1, nil],
]
end
end

describe 'running worker pools defined by multiple --pool arguments' do
it 'should run the correct worker processes' do
command = Delayed::Command.new(['--pool=*:1', '--pool=test_queue:4', '--pool=mailers,misc:2'])
command = Delayed::Command.new(['--pool=*:1:default_pool', '--pool=test_queue:4:test_pool', '--pool=mailers,misc:2'])
expect(FileUtils).to receive(:mkdir_p).with('./tmp/pids').once

[
['delayed_job.0', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => []}],
['delayed_job.1', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => ['test_queue']}],
['delayed_job.2', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => ['test_queue']}],
['delayed_job.3', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => ['test_queue']}],
['delayed_job.4', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => ['test_queue']}],
['delayed_job.default_pool.0', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => []}],
['delayed_job.test_pool.1', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => ['test_queue']}],
['delayed_job.test_pool.2', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => ['test_queue']}],
['delayed_job.test_pool.3', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => ['test_queue']}],
['delayed_job.test_pool.4', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => ['test_queue']}],
['delayed_job.5', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => %w[mailers misc]}],
['delayed_job.6', {:quiet => true, :pid_dir => './tmp/pids', :log_dir => './log', :queues => %w[mailers misc]}]
].each do |args|
Expand Down