Module: Hutch::Config

Defined in:
lib/hutch/config.rb

Overview

Configuration settings, available everywhere

There are defaults, which can be overridden by ENV variables prefixed by HUTCH_, and each of these can be overridden using the Config.set method.

Examples:

Configuring on the command-line

HUTCH_PUBLISHER_CONFIRMS=false hutch

Constant Summary collapse

ALL_KEYS =

Set of all setting keys

@boolean_keys + @number_keys + @string_keys

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_attr(attr) ⇒ Object



251
252
253
254
255
# File 'lib/hutch/config.rb', line 251

def self.check_attr(attr)
  unless user_config.key?(attr)
    raise UnknownAttributeError, "#{attr.inspect} is not a valid config attribute"
  end
end

.convert_value(attr, value) ⇒ Object



271
272
273
274
275
276
277
278
# File 'lib/hutch/config.rb', line 271

def self.convert_value(attr, value)
  case attr
  when 'tracer'
    Kernel.const_get(value)
  else
    value
  end
end

.default_configHash

Default settings

Returns:

  • (Hash)


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/hutch/config.rb', line 173

def self.default_config
  @settings_defaults.merge({
    mq_exchange_options: {},
    mq_tls_cert: nil,
    mq_tls_key: nil,
    mq_tls_ca_certificates: nil,
    uri: nil,
    log_level: Logger::INFO,
    client_logger: nil,
    require_paths: [],
    error_handlers: [Hutch::ErrorHandlers::Logger.new],
    # note that this is not a list, it is a chain of responsibility
    # that will fall back to "nack unconditionally"
    error_acknowledgements: [],
    setup_procs: [],
    consumer_groups: {},
    tracer: Hutch::Tracers::NullTracer,
    namespace: nil,
    pidfile: nil,
    serializer: Hutch::Serializers::JSON
  })
end

.define_methodsObject



292
293
294
295
296
297
298
299
300
301
302
# File 'lib/hutch/config.rb', line 292

def self.define_methods
  @config.keys.each do |key|
    define_singleton_method(key) do
      get(key)
    end

    define_singleton_method("#{key}=") do |val|
      set(key, val)
    end
  end
end

.env_based_configHash

Override defaults with ENV variables which begin with HUTCH_

Returns:

  • (Hash)


199
200
201
202
203
204
205
# File 'lib/hutch/config.rb', line 199

def self.env_based_config
  env_keys_configured.each_with_object({}) {|attr, result|
    value = ENV[key_for(attr)]

    result[attr] = type_cast(attr, value)
  }
end

.env_keys_configuredArray<Symbol>

Returns:

  • (Array<Symbol>)


208
209
210
211
212
# File 'lib/hutch/config.rb', line 208

def self.env_keys_configured
  ALL_KEYS.each {|attr| check_attr(attr) }

  ALL_KEYS.select { |attr| ENV.key?(key_for(attr)) }
end

.get(attr) ⇒ Object Also known as: []



214
215
216
217
# File 'lib/hutch/config.rb', line 214

def self.get(attr)
  check_attr(attr.to_sym)
  user_config[attr.to_sym]
end

.initialize(params = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/hutch/config.rb', line 160

def self.initialize(params = {})
  unless @config
    @config = default_config
    define_methods
    @config.merge!(env_based_config)
  end
  @config.merge!(params)
  @config
end

.is_bool(attr) ⇒ Object



224
225
226
# File 'lib/hutch/config.rb', line 224

def self.is_bool(attr)
  @boolean_keys.include?(attr)
end

.is_num(attr) ⇒ Object



237
238
239
# File 'lib/hutch/config.rb', line 237

def self.is_num(attr)
  @number_keys.include?(attr)
end

.key_for(attr) ⇒ Object



219
220
221
222
# File 'lib/hutch/config.rb', line 219

def self.key_for(attr)
  key = attr.to_s.gsub('.', '__').upcase
  "HUTCH_#{key}"
end

.load_from_file(file) ⇒ Object



265
266
267
268
269
# File 'lib/hutch/config.rb', line 265

def self.load_from_file(file)
  YAML.load(ERB.new(File.read(file)).result).each do |attr, value|
    Hutch::Config.send("#{attr}=", convert_value(attr, value))
  end
end

.set(attr, value) ⇒ Object Also known as: []=



241
242
243
244
# File 'lib/hutch/config.rb', line 241

def self.set(attr, value)
  check_attr(attr.to_sym)
  user_config[attr.to_sym] = type_cast(attr, value)
end

.to_bool(value) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/hutch/config.rb', line 228

def self.to_bool(value)
  case value
  when nil, false, '', /^(false|f|no|n|0)$/i
    false
  else
    true
  end
end

.to_hashObject



261
262
263
# File 'lib/hutch/config.rb', line 261

def self.to_hash
  user_config
end

.user_configObject



257
258
259
# File 'lib/hutch/config.rb', line 257

def self.user_config
  @config ||= initialize
end

Instance Method Details

#autoload_railsObject

Should the current Rails app directory be required?



121
# File 'lib/hutch/config.rb', line 121

boolean_setting :autoload_rails, true

#automatically_recoverObject

Bunny’s enable/disable network recovery



100
# File 'lib/hutch/config.rb', line 100

boolean_setting :automatically_recover, true

#channel_prefetchObject

The basic.qos prefetch value to use.

Default: ‘0`, no limit. See Bunny and RabbitMQ documentation.



88
# File 'lib/hutch/config.rb', line 88

number_setting :channel_prefetch, 0

#connection_timeoutObject

Bunny’s socket open timeout



91
# File 'lib/hutch/config.rb', line 91

number_setting :connection_timeout, 11

#consumer_pool_abort_on_exceptionObject

Should Bunny’s consumer work pool threads abort on exception.

The option is ignored on JRuby.



147
# File 'lib/hutch/config.rb', line 147

boolean_setting :consumer_pool_abort_on_exception, false

#consumer_pool_sizeObject

Bunny consumer work pool size



109
# File 'lib/hutch/config.rb', line 109

number_setting :consumer_pool_size, 1

#consumer_tag_prefixObject

Prefix displayed on the consumers tags.



150
# File 'lib/hutch/config.rb', line 150

string_setting :consumer_tag_prefix, 'hutch'

#daemoniseObject

Should the Hutch runner process daemonise?

The option is ignored on JRuby.



126
# File 'lib/hutch/config.rb', line 126

boolean_setting :daemonise, false

#enable_http_api_useObject

Should the RabbitMQ HTTP API be used?



142
# File 'lib/hutch/config.rb', line 142

boolean_setting :enable_http_api_use, true

#force_publisher_confirmsObject

Enables publisher confirms, forces Hutch::Broker#wait_for_confirms for every publish.

**This is the safest option which also offers the lowest throughput**.



139
# File 'lib/hutch/config.rb', line 139

boolean_setting :force_publisher_confirms, false

#graceful_exit_timeoutObject

FIXME: DOCUMENT THIS



106
# File 'lib/hutch/config.rb', line 106

number_setting :graceful_exit_timeout, 11

#groupObject



155
# File 'lib/hutch/config.rb', line 155

string_setting :group, ''

#heartbeatObject

[RabbitMQ heartbeat timeout](rabbitmq.com/heartbeats.html)



83
# File 'lib/hutch/config.rb', line 83

number_setting :heartbeat, 30

#mq_api_hostObject

RabbitMQ HTTP API hostname



73
# File 'lib/hutch/config.rb', line 73

string_setting :mq_api_host, '127.0.0.1'

#mq_api_portObject

RabbitMQ HTTP API port



80
# File 'lib/hutch/config.rb', line 80

number_setting :mq_api_port, 15672

#mq_api_sslObject

Should SSL be used for the RabbitMQ API?



118
# File 'lib/hutch/config.rb', line 118

boolean_setting :mq_api_ssl, false

#mq_auth_mechanismObject

RabbitMQ Auth Mechanism



67
# File 'lib/hutch/config.rb', line 67

string_setting :mq_auth_mechanism, 'PLAIN'

#mq_exchangeObject

RabbitMQ Exchange to use for publishing



50
# File 'lib/hutch/config.rb', line 50

string_setting :mq_exchange, 'hutch'

#mq_exchange_typeObject

RabbitMQ Exchange type to use for publishing



53
# File 'lib/hutch/config.rb', line 53

string_setting :mq_exchange_type, 'topic'

#mq_hostObject

RabbitMQ hostname



47
# File 'lib/hutch/config.rb', line 47

string_setting :mq_host, '127.0.0.1'

#mq_passwordObject

RabbitMQ password



64
# File 'lib/hutch/config.rb', line 64

string_setting :mq_password, 'guest'

#mq_portObject

RabbitMQ port RabbitMQ port



77
# File 'lib/hutch/config.rb', line 77

number_setting :mq_port, 5672

#mq_tlsObject

Should TLS be used?



112
# File 'lib/hutch/config.rb', line 112

boolean_setting :mq_tls, false

#mq_usernameObject

RabbitMQ username to use.

As of RabbitMQ 3.3.0, guest can only can connect from localhost.



61
# File 'lib/hutch/config.rb', line 61

string_setting :mq_username, 'guest'

#mq_verify_peerObject

Should SSL certificate be verified?



115
# File 'lib/hutch/config.rb', line 115

boolean_setting :mq_verify_peer, true

#mq_vhostObject

RabbitMQ vhost to use



56
# File 'lib/hutch/config.rb', line 56

string_setting :mq_vhost, '/'

#namespaceObject

A namespace to help group your queues



153
# File 'lib/hutch/config.rb', line 153

string_setting :namespace, nil

#network_recovery_intervalObject

Bunny’s reconnect interval



103
# File 'lib/hutch/config.rb', line 103

number_setting :network_recovery_interval, 1

#publisher_confirmsObject

Should RabbitMQ publisher confirms be enabled?

Leaves it up to the app how they are tracked (e.g. using Hutch::Broker#confirm_select callback or Hutch::Broker#wait_for_confirms)



132
# File 'lib/hutch/config.rb', line 132

boolean_setting :publisher_confirms, false

#read_timeoutObject

Bunny’s socket read timeout



94
# File 'lib/hutch/config.rb', line 94

number_setting :read_timeout, 11

#uriObject

RabbitMQ URI (takes precedence over MQ username, password, host, port and vhost settings)



70
# File 'lib/hutch/config.rb', line 70

string_setting :uri, nil

#write_timeoutObject

Bunny’s socket write timeout



97
# File 'lib/hutch/config.rb', line 97

number_setting :write_timeout, 11