Class: RocketJob::CLI
- Inherits:
-
Object
- Object
- RocketJob::CLI
- Defined in:
- lib/rocket_job/cli.rb
Overview
Command Line Interface parser for RocketJob
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pidfile ⇒ Object
readonly
Returns the value of attribute pidfile.
-
#quiet ⇒ Object
readonly
Returns the value of attribute quiet.
-
#threads ⇒ Object
readonly
Returns the value of attribute threads.
Instance Method Summary collapse
-
#boot_rails ⇒ Object
Initialize the Rails environment.
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
-
#parse(argv) ⇒ Object
Parse command line options placing results in the corresponding instance variables.
-
#run ⇒ Object
Run a RocketJob::Worker from the command line.
-
#write_pidfile ⇒ Object
Create a PID file if requested.
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/rocket_job/cli.rb', line 7 def initialize(argv) @name = nil @threads = nil @quiet = false @environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' @pidfile = nil @directory = '.' parse(argv) end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
5 6 7 |
# File 'lib/rocket_job/cli.rb', line 5 def directory @directory end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
5 6 7 |
# File 'lib/rocket_job/cli.rb', line 5 def environment @environment end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/rocket_job/cli.rb', line 5 def name @name end |
#pidfile ⇒ Object (readonly)
Returns the value of attribute pidfile.
5 6 7 |
# File 'lib/rocket_job/cli.rb', line 5 def pidfile @pidfile end |
#quiet ⇒ Object (readonly)
Returns the value of attribute quiet.
5 6 7 |
# File 'lib/rocket_job/cli.rb', line 5 def quiet @quiet end |
#threads ⇒ Object (readonly)
Returns the value of attribute threads.
5 6 7 |
# File 'lib/rocket_job/cli.rb', line 5 def threads @threads end |
Instance Method Details
#boot_rails ⇒ Object
Initialize the Rails environment
31 32 33 34 35 36 37 38 39 |
# File 'lib/rocket_job/cli.rb', line 31 def boot_rails require File.("#{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! } end end end |
#parse(argv) ⇒ Object
Parse command line options placing results in the corresponding instance variables
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rocket_job/cli.rb', line 54 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('--pidfile PATH', 'Use PATH as a pidfile') { |arg| @pidfile = arg } o.on('-v', '--version', 'Print the version information') do puts "Rocket Job v#{RocketJob::VERSION}" exit 1 end end parser. = 'rocketjob <options>' parser.on_tail '-h', '--help', 'Show help' do puts parser exit 1 end parser.parse! argv end |
#run ⇒ Object
Run a RocketJob::Worker from the command line
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rocket_job/cli.rb', line 19 def run 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 Worker.run(opts) end |
#write_pidfile ⇒ Object
Create a PID file if requested
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rocket_job/cli.rb', line 42 def write_pidfile return unless pidfile pid = $$ File.open(pidfile, 'w') { |f| f.puts(pid) } # Remove pidfile on exit at_exit do File.delete(pidfile) if pid == $$ end end |