Class: RocketJob::CLI
- Inherits:
-
Object
- Object
- RocketJob::CLI
- Includes:
- SemanticLogger::Loggable
- Defined in:
- lib/rocket_job/cli.rb
Overview
Command Line Interface parser for Rocket Job
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#exclude_filter ⇒ Object
Returns the value of attribute exclude_filter.
-
#include_filter ⇒ Object
Returns the value of attribute include_filter.
-
#log_file ⇒ Object
Returns the value of attribute log_file.
-
#log_level ⇒ Object
Returns the value of attribute log_level.
-
#mongo_config ⇒ Object
Returns the value of attribute mongo_config.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pidfile ⇒ Object
Returns the value of attribute pidfile.
-
#quiet ⇒ Object
Returns the value of attribute quiet.
-
#symmetric_encryption_config ⇒ Object
Returns the value of attribute symmetric_encryption_config.
-
#where_filter ⇒ Object
Returns the value of attribute where_filter.
-
#workers ⇒ Object
Returns the value of attribute workers.
Class Method Summary collapse
-
.eager_load_jobs(job_path = 'jobs') ⇒ Object
Eager load files in jobs folder.
Instance Method Summary collapse
-
#boot_rails ⇒ Object
Initialize the Rails environment Returns [true|false] whether Rails is present.
-
#boot_standalone ⇒ Object
In a standalone environment, explicitly load config files.
-
#build_filter ⇒ Object
Returns [Hash] a where clause filter to apply to this server.
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
-
#parse(argv) ⇒ Object
Parse command line options placing results in the corresponding instance variables.
- #rails? ⇒ Boolean
-
#run ⇒ Object
Run a RocketJob::Server from the command line.
- #setup_environment ⇒ Object
- #setup_logger ⇒ Object
-
#write_pidfile ⇒ Object
Create a PID file if requested.
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rocket_job/cli.rb', line 15 def initialize(argv) @name = nil @workers = nil @quiet = false @environment = nil @pidfile = nil @directory = '.' @log_level = nil @log_file = nil @mongo_config = nil @symmetric_encryption_config = nil @include_filter = nil @exclude_filter = nil parse(argv) end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def directory @directory end |
#environment ⇒ Object
Returns the value of attribute environment.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def environment @environment end |
#exclude_filter ⇒ Object
Returns the value of attribute exclude_filter.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def exclude_filter @exclude_filter end |
#include_filter ⇒ Object
Returns the value of attribute include_filter.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def include_filter @include_filter end |
#log_file ⇒ Object
Returns the value of attribute log_file.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def log_file @log_file end |
#log_level ⇒ Object
Returns the value of attribute log_level.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def log_level @log_level end |
#mongo_config ⇒ Object
Returns the value of attribute mongo_config.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def mongo_config @mongo_config end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def name @name end |
#pidfile ⇒ Object
Returns the value of attribute pidfile.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def pidfile @pidfile end |
#quiet ⇒ Object
Returns the value of attribute quiet.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def quiet @quiet end |
#symmetric_encryption_config ⇒ Object
Returns the value of attribute symmetric_encryption_config.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def symmetric_encryption_config @symmetric_encryption_config end |
#where_filter ⇒ Object
Returns the value of attribute where_filter.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def where_filter @where_filter end |
#workers ⇒ Object
Returns the value of attribute workers.
11 12 13 |
# File 'lib/rocket_job/cli.rb', line 11 def workers @workers end |
Class Method Details
.eager_load_jobs(job_path = 'jobs') ⇒ Object
Eager load files in jobs folder
148 149 150 151 152 153 154 |
# File 'lib/rocket_job/cli.rb', line 148 def self.eager_load_jobs(job_path = 'jobs') Pathname.glob("#{job_path}/**/*.rb").each do |path| next if path.directory? logger.debug "Loading #{path}" require path..to_s end end |
Instance Method Details
#boot_rails ⇒ Object
Initialize the Rails environment Returns [true|false] whether Rails is present
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rocket_job/cli.rb', line 62 def boot_rails logger.info "Loading Rails environment: #{environment}" RocketJob.rails! boot_file = Pathname.new(directory).join('config/environment.rb'). 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 return unless Rails.configuration.eager_load logger.measure_info('Eager loaded Rails and all Engines') do Rails.application.eager_load! Rails::Engine.subclasses.each(&:eager_load!) self.class.eager_load_jobs(File.('jobs', File.dirname(__FILE__))) end end |
#boot_standalone ⇒ Object
In a standalone environment, explicitly load config files
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rocket_job/cli.rb', line 88 def boot_standalone # Try to load bundler if present begin require 'bundler/setup' Bundler.require(environment) rescue LoadError nil end require 'rocketjob' begin require 'rocketjob_enterprise' rescue LoadError nil 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(file_name: path.to_s, formatter: :color) logger.info "Rails not detected. Running standalone: #{environment}" RocketJob::Config.load!(environment, mongo_config, symmetric_encryption_config) self.class.eager_load_jobs(File.('jobs', File.dirname(__FILE__))) self.class.eager_load_jobs end |
#build_filter ⇒ Object
Returns [Hash] a where clause filter to apply to this server. Returns nil if no filter should be applied
158 159 160 161 162 163 164 165 |
# File 'lib/rocket_job/cli.rb', line 158 def build_filter raise(ArgumentError, 'Cannot supply both a filter and an exclusion filter') if include_filter && exclude_filter filter = where_filter (filter ||= {})['_type'] = include_filter if include_filter (filter ||= {})['_type'] = {'$not' => exclude_filter} if exclude_filter filter end |
#parse(argv) ⇒ Object
Parse command line options placing results in the corresponding instance variables
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rocket_job/cli.rb', line 168 def parse(argv) parser = OptionParser.new do |o| o.on('-n', '--name NAME', 'Unique Name of this server (Default: host_name:PID)') do |arg| @name = arg end o.on('-w', '--workers COUNT', 'Number of workers (threads) to start') do |arg| @workers = arg.to_i end o.on('-t', '--threads COUNT', 'DEPRECATED') do |arg| warn '-t and --threads are deprecated, use -w or --workers' @workers = arg.to_i end o.on('-F', '--filter REGEXP', 'Limit this server to only those job classes that match this regular expression (case-insensitive). Example: "DirmonJob|WeeklyReportJob"') do |arg| @include_filter = Regexp.new(arg, true) end o.on('-E', '--exclude REGEXP', 'Prevent this server from working on any job classes that match this regular expression (case-insensitive). Example: "DirmonJob|WeeklyReportJob"') do |arg| @exclude_filter = Regexp.new(arg, true) end o.on('-W', '--where JSON', "Limit this server instance to the supplied mongo query filter. Supply as a string in JSON format. Example: '{\"priority\":{\"$lte\":25}}'") do |arg| @where_filter = JSON.parse(arg) 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('-m', '--mongo MONGO_CONFIG_FILE_NAME', 'Path and filename of config file. Default: config/mongoid.yml') do |arg| @mongo_config = arg end o.on('-s', '--symmetric-encryption SYMMETRIC_ENCRYPTION_CONFIG_FILE_NAME', 'Path and filename of Symmetric Encryption config file. Default: config/symmetric-encryption.yml') do |arg| @symmetric_encryption_config = arg end 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 |
#rails? ⇒ Boolean
53 54 55 56 57 58 |
# File 'lib/rocket_job/cli.rb', line 53 def rails? @rails ||= begin boot_file = Pathname.new(directory).join('config/environment.rb'). boot_file.file? end end |
#run ⇒ Object
Run a RocketJob::Server from the command line
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rocket_job/cli.rb', line 32 def run Thread.current.name = 'rocketjob main' RocketJob.server! setup_environment setup_logger rails? ? boot_rails : boot_standalone write_pidfile # In case Rails did not load the Mongoid Config RocketJob::Config.load!(environment, mongo_config, symmetric_encryption_config) if ::Mongoid::Config.clients.empty? filter = build_filter opts = {} opts[:name] = name if name opts[:max_workers] = workers if workers opts[:filter] = filter if filter Supervisor.run(opts) end |
#setup_environment ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/rocket_job/cli.rb', line 127 def setup_environment # Override Env vars when environment is supplied if environment ENV['RACK_ENV'] = ENV['RAILS_ENV'] = environment else self.environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' end end |
#setup_logger ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rocket_job/cli.rb', line 136 def setup_logger SemanticLogger.add_appender(io: STDOUT, formatter: :color) unless quiet SemanticLogger.default_level = log_level.to_sym if log_level # Enable SemanticLogger signal handling for this process SemanticLogger.add_signal_handler ::Mongoid.logger = SemanticLogger[::Mongoid] ::Mongo::Logger.logger = SemanticLogger[::Mongo] end |
#write_pidfile ⇒ Object
Create a PID file if requested
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rocket_job/cli.rb', line 116 def write_pidfile return unless pidfile pid = $PID File.open(pidfile, 'w') { |f| f.puts(pid) } # Remove pidfile on exit at_exit do File.delete(pidfile) if pid == $PID end end |