lib/rocket_job/cli.rb in rocketjob-1.3.0 vs lib/rocket_job/cli.rb in rocketjob-2.0.0.rc1
- old
+ new
@@ -1,59 +1,94 @@
require 'optparse'
require 'yaml'
+require 'semantic_logger'
module RocketJob
# Command Line Interface parser for RocketJob
class CLI
include SemanticLogger::Loggable
- attr_accessor :name, :threads, :environment, :pidfile, :directory, :quiet, :log_level
+ attr_accessor :name, :threads, :environment, :pidfile, :directory, :quiet, :log_level, :log_file
def initialize(argv)
@name = nil
@threads = nil
@quiet = false
@environment = nil
@pidfile = nil
@directory = '.'
@log_level = nil
+ @log_file = nil
parse(argv)
end
# Run a RocketJob::Worker from the command line
def run
+ Thread.current.name = 'rocketjob main'
setup_environment
setup_logger
- boot_standalone unless boot_rails
+ rails? ? boot_rails : boot_standalone
write_pidfile
opts = {}
opts[:name] = name if name
opts[:max_threads] = threads if threads
Worker.run(opts)
end
+ def rails?
+ @rails ||= begin
+ boot_file = Pathname.new(directory).join('config/environment.rb').expand_path
+ boot_file.file?
+ end
+ end
+
# Initialize the Rails environment
# Returns [true|false] whether Rails is present
def boot_rails
+ logger.info "Loading Rails environment: #{environment}"
+
boot_file = Pathname.new(directory).join('config/environment.rb').expand_path
- return false unless boot_file.file?
+ require(boot_file.to_s)
- logger.info 'Booting Rails'
- require boot_file.to_s
+ begin
+ require 'rails_semantic_logger'
+ rescue LoadError
+ raise "Add the following line to your Gemfile when running rails:\n gem 'rails_semantic_logger'"
+ end
+
+ # Override Rails log level if command line option was supplied
+ SemanticLogger.default_level = log_level.to_sym if log_level
+
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(&:eager_load!)
end
end
-
- self.class.load_config(Rails.env)
- true
end
+ # In a standalone environment, explicitly load config files
def boot_standalone
- logger.info 'Rails not detected. Running standalone.'
- self.class.load_config(environment)
+ # Try to load bundler if present
+ begin
+ require 'bundler/setup'
+ Bundler.require(environment)
+ rescue LoadError
+ end
+
+ require 'rocketjob'
+ begin
+ require 'rocketjob_pro'
+ rescue LoadError
+ end
+
+ # Log to file except when booting rails, when it will add the log file path
+ path = log_file ? Pathname.new(log_file) : Pathname.pwd.join("log/#{environment}.log")
+ path.dirname.mkpath
+ SemanticLogger.add_appender(path.to_s, &SemanticLogger::Appender::Base.colorized_formatter)
+
+ logger.info "Rails not detected. Running standalone: #{environment}"
+ RocketJob::Config.load!(environment)
self.class.eager_load_jobs
end
# Create a PID file if requested
def write_pidfile
@@ -77,25 +112,13 @@
end
def setup_logger
SemanticLogger.add_appender(STDOUT, &SemanticLogger::Appender::Base.colorized_formatter) unless quiet
SemanticLogger.default_level = log_level.to_sym if log_level
- end
- # Configure MongoMapper if it has not already been configured
- def self.load_config(environment='development', file_name=nil)
- return false if MongoMapper.config
-
- config_file = file_name ? Pathname.new(file_name) : Pathname.pwd.join('config/mongo.yml')
- if config_file.file?
- config = YAML.load(ERB.new(config_file.read).result)
- log = SemanticLogger::DebugAsTraceLogger.new('Mongo')
- MongoMapper.setup(config, environment, logger: log)
- true
- else
- raise(ArgumentError, "Mongo Configuration file: #{config_file.to_s} not found")
- end
+ # Enable SemanticLogger signal handling for this process
+ SemanticLogger.add_signal_handler
end
# Eager load files in jobs folder
def self.eager_load_jobs(path = 'jobs')
Pathname.glob("#{path}/**/*.rb").each do |path|
@@ -106,16 +129,33 @@
end
# Parse command line options placing results in the corresponding instance variables
def parse(argv)
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 }
- o.on('-l', '--log_level trace|debug|info|warn|error|fatal', 'The log level to use') { |arg| @log_level = arg }
- o.on('--pidfile PATH', 'Use PATH as a pidfile') { |arg| @pidfile = arg }
+ o.on('-n', '--name NAME', 'Unique Name of this worker instance (Default: hostname:PID)') do |arg|
+ @name = arg
+ end
+ o.on('-t', '--threads COUNT', 'Number of worker threads to start') do |arg|
+ @threads = arg.to_i
+ end
+ o.on('-q', '--quiet', 'Do not write to stdout, only to logfile. Necessary when running as a daemon') do
+ @quiet = true
+ end
+ o.on('-d', '--dir DIR', 'Directory containing Rails app, if not current directory') do |arg|
+ @directory = arg
+ end
+ o.on('-e', '--environment ENVIRONMENT', 'The environment to run the app on (Default: RAILS_ENV || RACK_ENV || development)') do |arg|
+ @environment = arg
+ end
+ o.on('-l', '--log_level trace|debug|info|warn|error|fatal', 'The log level to use') do |arg|
+ @log_level = arg
+ end
+ o.on('-f', '--log_file FILE_NAME', 'The log file to write to. Default: log/<environment>.log') do |arg|
+ @log_file = arg
+ end
+ o.on('--pidfile PATH', 'Use PATH as a pidfile') do |arg|
+ @pidfile = arg
+ end
o.on('-v', '--version', 'Print the version information') do
puts "Rocket Job v#{RocketJob::VERSION}"
exit 1
end
end