Class: Hutch::CLI
Instance Method Summary collapse
- #load_app ⇒ Object
- #load_rails_app(path) ⇒ Object
- #parse_options(args = ARGV) ⇒ Object
-
#run(argv = ARGV) ⇒ Object
Run a Hutch worker with the command line interface.
-
#start_work_loop ⇒ Object
Kick off the work loop.
- #write_pid ⇒ Object
Methods included from Logging
#logger, logger, logger=, setup_logger
Instance Method Details
#load_app ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/hutch/cli.rb', line 32 def load_app # Try to load a Rails app in the current directory load_rails_app('.') if Hutch::Config.autoload_rails Hutch::Config.require_paths.each do |path| # See if each path is a Rails app. If so, try to load it. next if load_rails_app(path) # Given path is not a Rails app, try requiring it as a file logger.info "requiring '#{path}'" begin # Need to add '.' to load path for relative requires $LOAD_PATH << '.' require path rescue LoadError logger.fatal "could not load file '#{path}'" return false ensure # Clean up load path $LOAD_PATH.pop end end # Because of the order things are required when we run the Hutch binary # in hutch/bin, the Sentry Raven gem gets required **after** the error # handlers are set up. Due to this, we never got any Sentry notifications # when an error occurred in any of the consumers. if defined?(Raven) Hutch::Config[:error_handlers] << Hutch::ErrorHandlers::Sentry.new end true end |
#load_rails_app(path) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hutch/cli.rb', line 65 def load_rails_app(path) # path should point to the app's top level directory if File.directory?(path) # Smells like a Rails app if it's got a config/environment.rb file rails_path = File.(File.join(path, 'config/environment.rb')) if File.exists?(rails_path) logger.info "found rails project (#{path}), booting app" ENV['RACK_ENV'] ||= ENV['RAILS_ENV'] || 'development' require rails_path ::Rails.application.eager_load! return true end end false end |
#parse_options(args = ARGV) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 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 |
# File 'lib/hutch/cli.rb', line 93 def (args = ARGV) OptionParser.new do |opts| opts. = 'usage: hutch [options]' opts.on('--mq-host HOST', 'Set the RabbitMQ host') do |host| Hutch::Config.mq_host = host end opts.on('--mq-port PORT', 'Set the RabbitMQ port') do |port| Hutch::Config.mq_port = port end opts.on("-t", "--[no-]mq-tls", 'Use TLS for the AMQP connection') do |tls| Hutch::Config.mq_tls = tls end opts.on('--mq-tls-cert FILE', 'Certificate for TLS client verification') do |file| abort "Certificate file '#{file}' not found" unless File.exists?(file) Hutch::Config.mq_tls_cert = file end opts.on('--mq-tls-key FILE', 'Private key for TLS client verification') do |file| abort "Private key file '#{file}' not found" unless File.exists?(file) Hutch::Config.mq_tls_key = file end opts.on('--mq-exchange EXCHANGE', 'Set the RabbitMQ exchange') do |exchange| Hutch::Config.mq_exchange = exchange end opts.on('--mq-vhost VHOST', 'Set the RabbitMQ vhost') do |vhost| Hutch::Config.mq_vhost = vhost end opts.on('--mq-username USERNAME', 'Set the RabbitMQ username') do |username| Hutch::Config.mq_username = username end opts.on('--mq-password PASSWORD', 'Set the RabbitMQ password') do |password| Hutch::Config.mq_password = password end opts.on('--mq-api-host HOST', 'Set the RabbitMQ API host') do |host| Hutch::Config.mq_api_host = host end opts.on('--mq-api-port PORT', 'Set the RabbitMQ API port') do |port| Hutch::Config.mq_api_port = port end opts.on("-s", "--[no-]mq-api-ssl", 'Use SSL for the RabbitMQ API') do |api_ssl| Hutch::Config.mq_api_ssl = api_ssl end opts.on('--config FILE', 'Load Hutch configuration from a file') do |file| begin File.open(file) { |fp| Hutch::Config.load_from_file(fp) } rescue Errno::ENOENT abort "Config file '#{file}' not found" end end opts.on('--require PATH', 'Require a Rails app or path') do |path| Hutch::Config.require_paths << path end opts.on('--[no-]autoload-rails', 'Require the current rails app directory') do |autoload_rails| Hutch::Config.autoload_rails = autoload_rails end opts.on('-q', '--quiet', 'Quiet logging') do Hutch::Config.log_level = Logger::WARN end opts.on('-v', '--verbose', 'Verbose logging') do Hutch::Config.log_level = Logger::DEBUG end opts.on('--namespace NAMESPACE', 'Queue namespace') do |namespace| Hutch::Config.namespace = namespace end opts.on('-d', '--daemonise', 'Daemonise') do |daemonise| Hutch::Config.daemonise = daemonise end opts.on('--pidfile PIDFILE', 'Pidfile') do |pidfile| Hutch::Config.pidfile = pidfile end opts.on('--version', 'Print the version and exit') do puts "hutch v#{VERSION}" exit 0 end opts.on('-h', '--help', 'Show this message and exit') do puts opts exit 0 end end.parse!(args) end |
#run(argv = ARGV) ⇒ Object
Run a Hutch worker with the command line interface.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hutch/cli.rb', line 13 def run(argv = ARGV) (argv) ::Process.daemon(true) if Hutch::Config.daemonise write_pid if Hutch::Config.pidfile Hutch.logger.info "hutch booted with pid #{::Process.pid}" if load_app && start_work_loop == :success # If we got here, the worker was shut down nicely Hutch.logger.info 'hutch shut down gracefully' exit 0 else Hutch.logger.info 'hutch terminated due to an error' exit 1 end end |
#start_work_loop ⇒ Object
Kick off the work loop. This method returns when the worker is shut down gracefully (with a SIGQUIT, SIGTERM or SIGINT).
83 84 85 86 87 88 89 90 91 |
# File 'lib/hutch/cli.rb', line 83 def start_work_loop Hutch.connect @worker = Hutch::Worker.new(Hutch.broker, Hutch.consumers) @worker.run :success rescue ConnectionError, AuthenticationError, WorkerSetupError => ex logger.fatal ex. :error end |