| Class | Delayed::Command |
| In: |
lib/delayed/command.rb
|
| Parent: | Object |
| worker_count | [RW] |
# File lib/delayed/command.rb, line 12
12: def initialize(args)
13: @options = {
14: :quiet => true,
15: :pid_dir => "#{Rails.root}/tmp/pids"
16: }
17:
18: @worker_count = 1
19: @monitor = false
20:
21: opts = OptionParser.new do |opts|
22: opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run"
23:
24: opts.on('-h', '--help', 'Show this message') do
25: puts opts
26: exit 1
27: end
28: opts.on('-e', '--environment=NAME', 'Specifies the environment to run this delayed jobs under (test/development/production).') do |e|
29: STDERR.puts "The -e/--environment option has been deprecated and has no effect. Use RAILS_ENV and see http://github.com/collectiveidea/delayed_job/issues/#issue/7"
30: end
31: opts.on('--min-priority N', 'Minimum priority of jobs to run.') do |n|
32: @options[:min_priority] = n
33: end
34: opts.on('--max-priority N', 'Maximum priority of jobs to run.') do |n|
35: @options[:max_priority] = n
36: end
37: opts.on('-n', '--number_of_workers=workers', "Number of unique workers to spawn") do |worker_count|
38: @worker_count = worker_count.to_i rescue 1
39: end
40: opts.on('--pid-dir=DIR', 'Specifies an alternate directory in which to store the process ids.') do |dir|
41: @options[:pid_dir] = dir
42: end
43: opts.on('-i', '--identifier=n', 'A numeric identifier for the worker.') do |n|
44: @options[:identifier] = n
45: end
46: opts.on('-m', '--monitor', 'Start monitor process.') do
47: @monitor = true
48: end
49: opts.on('--sleep-delay N', "Amount of time to sleep when no jobs are found") do |n|
50: @options[:sleep_delay] = n
51: end
52: opts.on('--read-ahead N', "Number of jobs from the queue to consider") do |n|
53: @options[:read_ahead] = n
54: end
55: opts.on('-p', '--prefix NAME', "String to be prefixed to worker process names") do |prefix|
56: @options[:prefix] = prefix
57: end
58: opts.on('--queues=queues', "Specify which queue DJ must look up for jobs") do |queues|
59: @options[:queues] = queues.split(',')
60: end
61: opts.on('--queue=queue', "Specify which queue DJ must look up for jobs") do |queue|
62: @options[:queues] = queue.split(',')
63: end
64: end
65: @args = opts.parse!(args)
66: end
# File lib/delayed/command.rb, line 68
68: def daemonize
69: dir = @options[:pid_dir]
70: Dir.mkdir(dir) unless File.exists?(dir)
71:
72: if @worker_count > 1 && @options[:identifier]
73: raise ArgumentError, 'Cannot specify both --number-of-workers and --identifier'
74: elsif @worker_count == 1 && @options[:identifier]
75: process_name = "delayed_job.#{@options[:identifier]}"
76: run_process(process_name, dir)
77: else
78: worker_count.times do |worker_index|
79: process_name = worker_count == 1 ? "delayed_job" : "delayed_job.#{worker_index}"
80: run_process(process_name, dir)
81: end
82: end
83: end
# File lib/delayed/command.rb, line 93
93: def run(worker_name = nil)
94: Dir.chdir(Rails.root)
95:
96: Delayed::Worker.after_fork
97: Delayed::Worker.logger ||= Logger.new(File.join(Rails.root, 'log', 'delayed_job.log'))
98:
99: worker = Delayed::Worker.new(@options)
100: worker.name_prefix = "#{worker_name} "
101: worker.start
102: rescue => e
103: Rails.logger.fatal e
104: STDERR.puts e.message
105: exit 1
106: end
# File lib/delayed/command.rb, line 85
85: def run_process(process_name, dir)
86: Delayed::Worker.before_fork
87: Daemons.run_proc(process_name, :dir => dir, :dir_mode => :normal, :monitor => @monitor, :ARGV => @args) do |*args|
88: $0 = File.join(@options[:prefix], process_name) if @options[:prefix]
89: run process_name
90: end
91: end