Class: Hutch::CLI

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/hutch/cli.rb

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=, setup_logger

Instance Method Details

#load_appObject



26
27
28
29
30
31
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
# File 'lib/hutch/cli.rb', line 26

def load_app
  # Try to load a Rails app in the current directory
  load_rails_app('.')
  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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hutch/cli.rb', line 59

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.expand_path(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_optionsObject



87
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
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
# File 'lib/hutch/cli.rb', line 87

def parse_options
  OptionParser.new do |opts|
    opts.banner = '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('--mq-tls', 'Set the RabbitMQ connection to use TLS') do |tls|
      Hutch::Config.mq_tls = tls
    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('--mq-api-ssl', 'Set the RabbitMQ API connection to use SSL') do |api_ssl|
      Hutch::Config.mq_api_ssl = api_ssl
    end

    opts.on('--require PATH', 'Require a Rails app or path') do |path|
      Hutch::Config.require_paths << path
    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('--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!
end

#runObject

Run a Hutch worker with the command line interface.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hutch/cli.rb', line 11

def run
  parse_options

  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_loopObject

Kick off the work loop. This method returns when the worker is shut down gracefully (with a SIGQUIT, SIGTERM or SIGINT).



77
78
79
80
81
82
83
84
85
# File 'lib/hutch/cli.rb', line 77

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.message
  :error
end