Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated command to use Delayed::Worker

  • Loading branch information...
commit 8d51757e8fba570df0fa62c8547446692cbb6a02 1 parent 28a0028
@bkeepers bkeepers authored
Showing with 27 additions and 41 deletions.
  1. +1 −1  generators/delayed_job/templates/script
  2. +26 −40 lib/delayed/command.rb
View
2  generators/delayed_job/templates/script
@@ -4,4 +4,4 @@
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require File.join(File.dirname(__FILE__), *%w(.. vendor plugins delayed_job lib delayed command))
-Delayed::Command.new(ARGV).run
+Delayed::Command.new(ARGV).daemonize
View
66 lib/delayed/command.rb
@@ -1,12 +1,11 @@
require 'rubygems'
require 'daemons'
require 'optparse'
-require 'ostruct'
module Delayed
class Command
def initialize(args)
- @options = OpenStruct.new(:sleep => 5)
+ @options = {:quiet => true}
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run"
@@ -18,51 +17,38 @@ def initialize(args)
opts.on('-e', '--environment=NAME', 'Specifies the environment to run this delayed jobs under (test/development/production).') do |e|
ENV['RAILS_ENV'] = e
end
- opts.on('-s', '--sleep=seconds', "Number of seconds between checking for new jobs") do |secs|
- @options.sleep = secs
+ opts.on('--min-priority N', 'Minimum priority of jobs to run.') do |n|
+ @options[:min_priority] = n
+ end
+ opts.on('--max-priority N', 'Maximum priority of jobs to run.') do |n|
+ @options[:max_priority] = n
end
end
@args = opts.parse!(args)
end
- def run
+ def daemonize
Daemons.run_proc('delayed_job', :dir => "#{RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
- begin
- require File.join(RAILS_ROOT, 'config', 'environment')
-
- # Replace the default logger
- logger = Logger.new(File.join(RAILS_ROOT, 'log', 'delayed_job.log'))
- logger.level = ActiveRecord::Base.logger.level
- ActiveRecord::Base.logger = logger
- ActiveRecord::Base.clear_active_connections!
-
- logger.info "*** Starting job worker #{Delayed::Job.worker_name}"
-
- trap('TERM') { puts 'Exiting...'; $exit = true }
- trap('INT') { puts 'Exiting...'; $exit = true }
-
- loop do
- result = nil
- realtime = Benchmark.realtime { result = Delayed::Job.work_off }
- count = result.sum
-
- break if $exit
-
- if count.zero?
- sleep @options.sleep
- logger.debug 'Waiting for more jobs...'
- else
- logger.info "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last]
- end
-
- break if $exit
- end
- rescue => e
- logger.fatal e
- STDERR.puts e.message
- exit 1
- end
+ run
end
end
+
+ def run
+ require File.join(RAILS_ROOT, 'config', 'environment')
+
+ # Replace the default logger
+ logger = Logger.new(File.join(RAILS_ROOT, 'log', 'delayed_job.log'))
+ logger.level = ActiveRecord::Base.logger.level
+ ActiveRecord::Base.logger = logger
+ ActiveRecord::Base.clear_active_connections!
+ Delayed::Worker.logger = logger
+
+ Delayed::Worker.new(@options).start
+ rescue => e
+ logger.fatal e
+ STDERR.puts e.message
+ exit 1
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.