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



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

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



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

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

.default_configHash

Default settings

Returns:

  • (Hash)


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

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



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

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)


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

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>)


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

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: []



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

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

.initialize(params = {}) ⇒ Object



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

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



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

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

.is_num(attr) ⇒ Object



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

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

.key_for(attr) ⇒ Object



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

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

.load_from_file(file) ⇒ Object



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

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: []=



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

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

.to_bool(value) ⇒ Object



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

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

.to_hashObject



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

def self.to_hash
  user_config
end

.user_configObject



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

def self.user_config
  @config ||= initialize
end

Instance Method Details

#autoload_railsObject

Should the current Rails app directory be required?



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

boolean_setting :autoload_rails, true

#automatically_recoverObject

Bunny’s enable/disable network recovery



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

boolean_setting :automatically_recover, true

#channel_prefetchObject

The basic.qos prefetch value to use.

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



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

number_setting :channel_prefetch, 0

#connection_timeoutObject

Bunny’s socket open timeout



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

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.



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

boolean_setting :consumer_pool_abort_on_exception, false

#consumer_pool_sizeObject

Bunny consumer work pool size



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

number_setting :consumer_pool_size, 1

#consumer_tag_prefixObject

Prefix displayed on the consumers tags.



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

string_setting :consumer_tag_prefix, 'hutch'

#daemoniseObject

Should the Hutch runner process daemonise?

The option is ignored on JRuby.



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

boolean_setting :daemonise, false

#enable_http_api_useObject

Should the RabbitMQ HTTP API be used?



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

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**.



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

boolean_setting :force_publisher_confirms, false

#graceful_exit_timeoutObject

FIXME: DOCUMENT THIS



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

number_setting :graceful_exit_timeout, 11

#groupObject



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

string_setting :group, ''

#heartbeatObject

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



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

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



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

number_setting :mq_api_port, 15672

#mq_api_sslObject

Should SSL be used for the RabbitMQ API?



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

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



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

number_setting :mq_port, 5672

#mq_tlsObject

Should TLS be used?



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

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?



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

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



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

string_setting :namespace, nil

#network_recovery_intervalObject

Bunny’s reconnect interval



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

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)



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

boolean_setting :publisher_confirms, false

#read_timeoutObject

Bunny’s socket read timeout



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

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



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

number_setting :write_timeout, 11