lib/rocket_job/cli.rb in rocketjob-1.0.0 vs lib/rocket_job/cli.rb in rocketjob-1.1.0
- old
+ new
@@ -3,57 +3,56 @@
# Command Line Interface parser for RocketJob
class CLI
attr_reader :name, :threads, :environment, :pidfile, :directory, :quiet
def initialize(argv)
- @name = nil
- @threads = nil
-
- @quiet = false
- @environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
- @pidfile = nil
- @directory = '.'
+ @name = nil
+ @threads = nil
+ @quiet = false
+ @environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
+ @pidfile = nil
+ @directory = '.'
parse(argv)
end
# Run a RocketJob::Worker from the command line
def run
- SemanticLogger.add_appender(STDOUT, &SemanticLogger::Appender::Base.colorized_formatter) unless quiet
+ SemanticLogger.add_appender(STDOUT, &SemanticLogger::Appender::Base.colorized_formatter) unless quiet
boot_rails if defined?(:Rails)
write_pidfile
- opts = {}
- opts[:name] = name if name
- opts[:max_threads] = threads if threads
+ opts = {}
+ opts[:name] = name if name
+ opts[:max_threads] = threads if threads
Worker.run(opts)
end
# Initialize the Rails environment
def boot_rails
require File.expand_path("#{directory}/config/environment.rb")
if Rails.configuration.eager_load
RocketJob::Worker.logger.benchmark_info('Eager loaded Rails and all Engines') do
Rails.application.eager_load!
- Rails::Engine.subclasses.each { |engine| engine.eager_load! }
+ Rails::Engine.subclasses.each(&:eager_load!)
end
end
end
# Create a PID file if requested
def write_pidfile
return unless pidfile
- pid = $$
+ pid = $PID
File.open(pidfile, 'w') { |f| f.puts(pid) }
# Remove pidfile on exit
at_exit do
- File.delete(pidfile) if pid == $$
+ File.delete(pidfile) if pid == $PID
end
end
# Parse command line options placing results in the corresponding instance variables
def parse(argv)
- parser = OptionParser.new do |o|
+ parser = OptionParser.new do |o|
o.on('-n', '--name NAME', 'Unique Name of this worker instance (Default: hostname:PID)') { |arg| @name = arg }
o.on('-t', '--threads COUNT', 'Number of worker threads to start') { |arg| @threads = arg.to_i }
o.on('-q', '--quiet', 'Do not write to stdout, only to logfile. Necessary when running as a daemon') { @quiet = true }
o.on('-d', '--dir DIR', 'Directory containing Rails app, if not current directory') { |arg| @directory = arg }
o.on('-e', '--environment ENVIRONMENT', 'The environment to run the app on (Default: RAILS_ENV || RACK_ENV || development)') { |arg| @environment = arg }