Class: Delayed::Command
- Inherits:
-
Object
- Object
- Delayed::Command
- Defined in:
- lib/delayed_on_steroids/command.rb
Overview
Used by script/delayed_job: parses options, sets logger, invokes Worker.
Instance Method Summary collapse
-
#initialize ⇒ Command
constructor
A new instance of Command.
- #run ⇒ Object
- #setup_logger ⇒ Object
- #spawn_workers ⇒ Object
- #write_pid ⇒ Object
Constructor Details
#initialize ⇒ Command
Returns a new instance of Command.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/delayed_on_steroids/command.rb', line 8 def initialize @worker_count = 1 @run_as_daemon = false ARGV.clone. do |opts| opts.separator "Options:" opts.on('--worker-name=name', String, 'Worker name. Default is auto-generated.') { |n| Delayed::Worker.name = n } opts.on('--min-priority=number', Integer, 'Minimum priority of jobs to run.') { |n| Delayed::Worker.min_priority = n } opts.on('--max-priority=number', Integer, 'Maximum priority of jobs to run.') { |n| Delayed::Worker.max_priority = n } opts.on('--job-types=types', String, 'Type of jobs to run.') { |t| Delayed::Worker.job_types = t.split(',') } opts.on('--keep-failed-jobs', 'Do not remove failed jobs from database.') { Delayed::Worker.destroy_failed_jobs = false } opts.on('--pid-file=file', String, 'Write PID to file.') { |f| @pid_file = f } opts.on('--log-file=file', String, 'Use specified file to log instead of Rails default logger.') do |f| Delayed::Worker.logger = ActiveSupport::BufferedLogger.new(f) end opts.on("-q", "--quiet", "Be quieter.") { @quiet = true } opts.on("-d", "--daemon", "Make worker run as a Daemon.") { @run_as_daemon = true } opts.on('-n', '--number-of-workers=number', Integer, "Number of unique workers to spawn. Implies -d option if number > 1.") do |n| @worker_count = ([n, 1].max rescue 1) @run_as_daemon ||= (@worker_count > 1) end opts.on("-e", "--environment=name", String, "Specifies the environment to run this worker under (test/development/production/etc).") do |e| ENV["RAILS_ENV"] = e RAILS_ENV.replace(e) end opts.on("-h", "--help", "Show this help message.") { puts opts; exit } opts.parse! end end |
Instance Method Details
#run ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/delayed_on_steroids/command.rb', line 86 def run warn "Running in #{RAILS_ENV} environment!" if RAILS_ENV.include?("dev") or RAILS_ENV.include?("test") # Saves memory with Ruby Enterprise Edition if GC.respond_to?(:copy_on_write_friendly=) GC.copy_on_write_friendly = true end spawn_workers Dir.chdir(RAILS_ROOT) write_pid setup_logger ActiveRecord::Base.connection.reconnect! Delayed::Worker.instance.start rescue => e Delayed::Worker.logger.fatal(e) STDERR.puts(e.) exit 1 end |
#setup_logger ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/delayed_on_steroids/command.rb', line 70 def setup_logger if Delayed::Worker.logger.respond_to?(:auto_flushing=) Delayed::Worker.logger.auto_flushing = true end if @quiet and Delayed::Worker.logger.respond_to?(:level=) if Delayed::Worker.logger.kind_of?(Logger) Delayed::Worker.logger.level = Logger::Severity::INFO elsif Delayed::Worker.logger.kind_of?(ActiveSupport::BufferedLogger) Delayed::Worker.logger.level = ActiveSupport::BufferedLogger::Severity::INFO end end ActiveRecord::Base.logger = Delayed::Worker.logger end |
#spawn_workers ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/delayed_on_steroids/command.rb', line 40 def spawn_workers # fork children if needed worker_no = nil if @worker_count > 1 it_is_parent = true @worker_count.times do |no| it_is_parent = fork worker_no = no break unless it_is_parent end exit 0 if it_is_parent end Process.daemon if @run_as_daemon if Delayed::Worker.name.nil? Delayed::Worker.name = ("host:#{Socket.gethostname} " rescue "") + "pid:#{Process.pid}" else Delayed::Worker.name += worker_no.to_s end end |
#write_pid ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/delayed_on_steroids/command.rb', line 62 def write_pid @pid_file ||= "#{RAILS_ROOT}/tmp/pids/dj_#{Delayed::Worker.name.parameterize('_')}.pid" dir = File.dirname(@pid_file) Dir.mkdir(dir) unless File.exists?(dir) File.open(@pid_file, 'w') { |f| f.write(Process.pid) } at_exit { File.delete(@pid_file) if File.exist?(@pid_file) } end |