Skip to content

Commit

Permalink
Use multiple --pool options, instead of one --pools option.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndbroadbent committed Jul 8, 2014
1 parent 0584b1f commit ca8ede6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ You can then do the following:
RAILS_ENV=production script/delayed_job --queue=tracking start
RAILS_ENV=production script/delayed_job --queues=mailers,tasks start

# Set the --pools option to start multiple worker pools, with different queues and numbers of workers.
# Use the --pool option to specify a worker pool. You can use this option multiple times to start different numbers of workers for different queues.
# The following command will start 1 worker for the tracking queue,
# 2 workers for the mailers and tasks queues, and 2 workers for any available jobs
RAILS_ENV=production script/delayed_job --pools=tracking:1/mailers,tasks:2/*:2 start
# 2 workers for the mailers and tasks queues, and 2 workers for any jobs:
RAILS_ENV=production script/delayed_job --pool=tracking --pool=mailers,tasks:2 --pool=*:2 start

# Runs all available jobs and then exits
RAILS_ENV=production script/delayed_job start --exit-on-complete
Expand Down
24 changes: 12 additions & 12 deletions lib/delayed/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def initialize(args)
opt.on('--queue=queue', 'Specify which queue DJ must look up for jobs') do |queue|
@options[:queues] = queue.split(',')
end
opt.on('--pools=queue1,queue2:workers/queue3:workers', "Specify queues and number of workers for multiple worker pools") do |pools|
parse_worker_pools(pools)
opt.on('--pool=queue1[,queue2][:worker_count]', "Specify queues and number of workers for a worker pool") do |pool|
parse_worker_pool(pool)
end
opt.on('--exit-on-complete', 'Exit when no more jobs are available to run. This will exit if all jobs are scheduled to run in the future.') do
@options[:exit_on_complete] = true
Expand Down Expand Up @@ -129,17 +129,17 @@ def run(worker_name = nil, options = {})

private

def parse_worker_pools(pools)
@worker_pools = pools.split('/').map do |pool|
queues, worker_count = pool.split(':')
if ['*', '', nil].include?(queues)
queues = []
else
queues = queues.split(',')
end
worker_count = worker_count.to_i rescue 1
[queues, worker_count]
def parse_worker_pool(pool)
@worker_pools ||= []

queues, worker_count = pool.split(':')
if ['*', '', nil].include?(queues)
queues = []
else
queues = queues.split(',')
end
worker_count = (worker_count || 1).to_i rescue 1
@worker_pools << [queues, worker_count]
end
end
end
27 changes: 17 additions & 10 deletions spec/delayed/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
require 'delayed/command'

describe Delayed::Command do
describe "parsing --pools argument" do
it "should parse --pools correctly" do
command = Delayed::Command.new(['--pools=*:1/test_queue:4/mailers,misc:2'])
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'])

expect(command.worker_pools).to eq [
[ [], 1 ],
Expand All @@ -13,24 +13,31 @@
]
end

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

command = Delayed::Command.new(['--pools=:4'])
command = Delayed::Command.new(['--pool=:4'])
expect(command.worker_pools).to eq [
[ [], 4 ],
]
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 ],
]
end
end

describe "running worker pools defined by --pools" do
describe "running worker pools defined by multiple --pool arguments" do
it "should run the correct worker processes" do
command = Delayed::Command.new(['--pools=*:1/test_queue:4/mailers,misc:2'])
command = Delayed::Command.new(['--pool=*:1', '--pool=test_queue:4', '--pool=mailers,misc:2'])

Dir.should_receive(:mkdir).with('./tmp/pids').once
expect(Dir).to receive(:mkdir).with('./tmp/pids').once

[
["delayed_job.0", {:quiet=>true, :pid_dir=>"./tmp/pids", :queues=>[]}],
Expand All @@ -41,7 +48,7 @@
["delayed_job.5", {:quiet=>true, :pid_dir=>"./tmp/pids", :queues=>["mailers", "misc"]}],
["delayed_job.6", {:quiet=>true, :pid_dir=>"./tmp/pids", :queues=>["mailers", "misc"]}]
].each do |args|
command.should_receive(:run_process).with(*args).once
expect(command).to receive(:run_process).with(*args).once
end

command.daemonize
Expand Down

0 comments on commit ca8ede6

Please sign in to comment.