Class: Jobit::Command
- Inherits:
-
Object
- Object
- Jobit::Command
- Defined in:
- lib/jobit/command.rb
Instance Attribute Summary collapse
-
#worker_count ⇒ Object
Returns the value of attribute worker_count.
Instance Method Summary collapse
- #daemonize ⇒ Object
-
#initialize(args) ⇒ Command
constructor
A new instance of Command.
- #run(worker_name = nil) ⇒ Object
- #run_process(process_name, dir) ⇒ Object
Constructor Details
#initialize(args) ⇒ Command
Returns a new instance of Command.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/jobit/command.rb', line 9 def initialize(args) @files_to_reopen = [] @options = { :quiet => true, :pid_dir => "#{Rails.root}/tmp/pids" } @worker_count = 1 @monitor = false opts = OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} [options] start|stop|restart|run" opts.on('-h', '--help', 'Show this message') do puts opts exit 1 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 |
Instance Attribute Details
#worker_count ⇒ Object
Returns the value of attribute worker_count.
7 8 9 |
# File 'lib/jobit/command.rb', line 7 def worker_count @worker_count end |
Instance Method Details
#daemonize ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/jobit/command.rb', line 33 def daemonize #Jobit::Worker.backend.before_fork ObjectSpace.each_object(File) do |file| @files_to_reopen << file unless file.closed? end dir = @options[:pid_dir] Dir.mkdir(dir) unless File.exists?(dir) if @worker_count > 1 && @options[:identifier] raise ArgumentError, 'Cannot specify both --number-of-workers and --identifier' elsif @worker_count == 1 && @options[:identifier] process_name = "jobit.#{@options[:identifier]}" run_process(process_name, dir) else worker_count.times do |worker_index| process_name = worker_count == 1 ? "jobit" : "jobit.#{worker_index}" run_process(process_name, dir) end end end |
#run(worker_name = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/jobit/command.rb', line 63 def run(worker_name = nil) begin # releasing connection if started in production environment with caching enabled ActiveRecord::Base.connection_pool.release_connection rescue ::Exception end Dir.chdir(Rails.root) # Re-open file handles @files_to_reopen.each do |file| begin file.reopen file.path, "a+" file.sync = true rescue ::Exception end end #Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'delayed_job.log')) #Delayed::Worker.backend.after_fork worker = Jobit::Worker.new(@options) #worker.name_prefix = "#{worker_name} " worker.start rescue => e Rails.logger.fatal e STDERR.puts e. exit 1 end |
#run_process(process_name, dir) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/jobit/command.rb', line 56 def run_process(process_name, dir) Daemons.run_proc(process_name, :dir => dir, :dir_mode => :normal, :monitor => @monitor, :ARGV => @args) do |*args| $0 = File.join(@options[:prefix], process_name) if @options[:prefix] run process_name end end |