Skip to content
Browse files

Added ability to run multiple workers concurrently.

  • Loading branch information...
1 parent e45a3c1 commit 17ddfb53f597e45bb59bd54376791437b452fcbc @patio11 patio11 committed with bkeepers
Showing with 15 additions and 4 deletions.
  1. +2 −1 README.textile
  2. +13 −3 lib/delayed/command.rb
View
3 README.textile
@@ -78,7 +78,8 @@ h2. Running the jobs
Run @script/generate delayed_job@ to add @script/delayed_job@. This script can then be used to manage a process which will start working off jobs.
- $ ruby script/delayed_job -e production start
+ # Runs two workers in separate processes.
+ $ ruby script/delayed_job -e production -n 2 start
$ ruby script/delayed_job -e production stop
You can invoke @rake jobs:work@ which will start working off jobs. You can cancel the rake task with @CTRL-C@.
View
16 lib/delayed/command.rb
@@ -4,8 +4,11 @@
module Delayed
class Command
+ attr_accessor :worker_count
+
def initialize(args)
@options = {:quiet => true}
+ @worker_count = 1
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run"
@@ -23,17 +26,23 @@ def initialize(args)
opts.on('--max-priority N', 'Maximum priority of jobs to run.') do |n|
@options[:max_priority] = n
end
+ opts.on('-n', '--number_of_workers=workers', "Number of unique workers to spawn") do |worker_count|
+ @worker_count = worker_count.to_i rescue 1
+ end
end
@args = opts.parse!(args)
end
def daemonize
- Daemons.run_proc('delayed_job', :dir => "#{RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
- run
+ worker_count.times do |worker_index|
+ process_name = worker_count == 1 ? "delayed_job" : "delayed_job.#{worker_index}"
+ Daemons.run_proc(process_name, :dir => "#{RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
+ run process_name
+ end
end
end
- def run
+ def run(worker_name = nil)
require File.join(RAILS_ROOT, 'config', 'environment')
# Replace the default logger
@@ -42,6 +51,7 @@ def run
ActiveRecord::Base.logger = logger
ActiveRecord::Base.clear_active_connections!
Delayed::Worker.logger = logger
+ Delayed::Job.worker_name = "#{worker_name} #{Delayed::Job.worker_name}"
Delayed::Worker.new(@options).start
rescue => e

0 comments on commit 17ddfb5

Please sign in to comment.
Something went wrong with that request. Please try again.