Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 2 files changed
  • 0 comments
  • 1 contributor
8  README.textile
Source Rendered
@@ -62,3 +62,11 @@ Run @script/generate delayed_job@ to add @script/delayed_job@. This script can t
62 62
 
63 63
   $ ruby script/delayed_job -e production start
64 64
   $ ruby script/delayed_job -e production stop
  65
+
  66
+h3. Additional options
  67
+
  68
+  $ ruby script/delayed_job -e production -n 2 start
  69
+  (Runs two workers in separate processes.)
  70
+
  71
+  $ ruby script/delayed_job -e production -s 2 start
  72
+  (Sleeps 2 seconds, instead of the default 5, between successive checks of the job queue.)
80  lib/delayed/command.rb
@@ -21,46 +21,60 @@ def initialize(args)
21 21
         opts.on('-s', '--sleep=seconds', "Number of seconds between checking for new jobs") do |secs|
22 22
           @options.sleep = secs
23 23
         end
  24
+        opts.on('-n', '--number_of_workers=workers', "Number of unique workers to spawn") do |worker_count|
  25
+          @options.worker_count = worker_count.to_i rescue 1
  26
+        end
24 27
       end
25 28
       @args = opts.parse!(args)
26 29
     end
27 30
   
28 31
     def run
29  
-      Daemons.run_proc('delayed_job', :dir => "#{RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
30  
-        begin
31  
-          require File.join(RAILS_ROOT, 'config', 'environment')
32  
-          
33  
-          # Replace the default logger
34  
-          logger = Logger.new(File.join(RAILS_ROOT, 'log', 'delayed_job.log'))
35  
-          logger.level = ActiveRecord::Base.logger.level
36  
-          ActiveRecord::Base.logger = logger
37  
-          ActiveRecord::Base.clear_active_connections!
38  
-          
39  
-          logger.info "*** Starting job worker #{Delayed::Job.worker_name}"
40  
-          
41  
-          trap('TERM') { puts 'Exiting...'; $exit = true }
42  
-          trap('INT')  { puts 'Exiting...'; $exit = true }
43  
-          
44  
-          loop do      
45  
-            result = nil
46  
-            realtime = Benchmark.realtime { result = Delayed::Job.work_off }
47  
-            count = result.sum
48  
-    
49  
-            break if $exit
50  
-    
51  
-            if count.zero? 
52  
-              sleep @options.sleep
53  
-              logger.debug 'Waiting for more jobs...'
54  
-            else
55  
-              logger.info "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last]
  32
+      worker_count = @options.worker_count || 1
  33
+
  34
+      1.upto(worker_count) do |worker_index|
  35
+        if (worker_count == 1)
  36
+          process_name = "delayed_job"
  37
+        else
  38
+          process_name = "delayed_job.#{worker_index}"
  39
+        end
  40
+        Daemons.run_proc(process_name, :dir => "#{RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
  41
+          begin
  42
+            require File.join(RAILS_ROOT, 'config', 'environment')
  43
+
  44
+            # Replace the default logger
  45
+            logger = Logger.new(File.join(RAILS_ROOT, 'log', 'delayed_job.log'))
  46
+            logger.level = ActiveRecord::Base.logger.level
  47
+            ActiveRecord::Base.logger = logger
  48
+            ActiveRecord::Base.clear_active_connections!
  49
+            if (worker_count != 1)
  50
+              Delayed::Job.worker_name = Delayed::Job.worker_name + " worker: #{worker_index}"
56 51
             end
57  
-    
58  
-            break if $exit
  52
+            logger.info "*** Starting job worker #{Delayed::Job.worker_name}"
  53
+
  54
+            trap('TERM') { puts 'Exiting...'; $exit = true }
  55
+            trap('INT')  { puts 'Exiting...'; $exit = true }
  56
+
  57
+            loop do
  58
+              result = nil
  59
+              realtime = Benchmark.realtime { result = Delayed::Job.work_off }
  60
+              count = result.sum
  61
+
  62
+              break if $exit
  63
+
  64
+              if count.zero?
  65
+                sleep @options.sleep
  66
+                logger.debug 'Waiting for more jobs...'
  67
+              else
  68
+                logger.info "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last]
  69
+              end
  70
+
  71
+              break if $exit
  72
+            end
  73
+          rescue => e
  74
+            logger.fatal e
  75
+            STDERR.puts e.message
  76
+            exit 1
59 77
           end
60  
-        rescue => e
61  
-          logger.fatal e
62  
-          STDERR.puts e.message
63  
-          exit 1
64 78
         end
65 79
       end
66 80
     end

No commit comments for this range

Something went wrong with that request. Please try again.