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



225
226
227
228
229
# File 'lib/hutch/config.rb', line 225

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



245
246
247
248
249
250
251
252
# File 'lib/hutch/config.rb', line 245

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

.default_configHash

Default settings

Returns:

  • (Hash)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/hutch/config.rb', line 146

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: [],
    tracer: Hutch::Tracers::NullTracer,
    namespace: nil,
    pidfile: nil,
    serializer: Hutch::Serializers::JSON
  })
end

.define_methodsObject



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/hutch/config.rb', line 254

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)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/hutch/config.rb', line 171

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

    case
    when is_bool(attr) || value == 'false'
      result[attr] = to_bool(value)
    when is_num(attr)
      result[attr] = value.to_i
    else
      result[attr] = value
    end
  }
end

.env_keys_configuredArray<Symbol>

Returns:

  • (Array<Symbol>)


187
188
189
190
191
# File 'lib/hutch/config.rb', line 187

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



193
194
195
196
# File 'lib/hutch/config.rb', line 193

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

.initialize(params = {}) ⇒ Object



136
137
138
139
140
141
# File 'lib/hutch/config.rb', line 136

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

.is_bool(attr) ⇒ Object



203
204
205
# File 'lib/hutch/config.rb', line 203

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

.is_num(attr) ⇒ Object



211
212
213
# File 'lib/hutch/config.rb', line 211

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

.key_for(attr) ⇒ Object



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

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

.load_from_file(file) ⇒ Object



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

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



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

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

.to_bool(value) ⇒ Object



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

def self.to_bool(value)
  !(value.nil? || value == '' || value =~ /^(false|f|no|n|0)$/i || value == false)
end

.to_hashObject



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

def self.to_hash
  user_config
end

.user_configObject



231
232
233
# File 'lib/hutch/config.rb', line 231

def self.user_config
  @config ||= initialize
end

Instance Method Details

#autoload_railsObject

Should the current Rails app directory be required?



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

boolean_setting :autoload_rails, true

#channel_prefetchObject

The basic.qos prefetch value to use.

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



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

number_setting :channel_prefetch, 0

#connection_timeoutObject

Bunny’s socket open timeout



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

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.



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

boolean_setting :consumer_pool_abort_on_exception, false

#consumer_pool_sizeObject

Bunny consumer work pool size



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

number_setting :consumer_pool_size, 1

#daemoniseObject

Should the Hutch runner process daemonise?

The option is ignored on JRuby.



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

boolean_setting :daemonise, false

#enable_http_api_useObject

Should the RabbitMQ HTTP API be used?



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

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



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

boolean_setting :force_publisher_confirms, false

#graceful_exit_timeoutObject

FIXME: DOCUMENT THIS



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

number_setting :graceful_exit_timeout, 11

#heartbeatObject

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



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

number_setting :heartbeat, 30

#mq_api_hostObject

RabbitMQ HTTP API hostname



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

string_setting :mq_api_host, '127.0.0.1'

#mq_api_portObject

RabbitMQ HTTP API port



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

number_setting :mq_api_port, 15672

#mq_api_sslObject

Should SSL be used for the RabbitMQ API?



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

boolean_setting :mq_api_ssl, false

#mq_exchangeObject

RabbitMQ Exchange to use for publishing



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

string_setting :mq_exchange, 'hutch'

#mq_hostObject

RabbitMQ hostname



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

string_setting :mq_host, '127.0.0.1'

#mq_passwordObject

RabbitMQ password



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

string_setting :mq_password, 'guest'

#mq_portObject

RabbitMQ port



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

number_setting :mq_port, 5672

#mq_tlsObject

Should TLS be used?



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

boolean_setting :mq_tls, false

#mq_usernameObject

RabbitMQ username to use.

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



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

string_setting :mq_username, 'guest'

#mq_verify_peerObject

Should SSL certificate be verified?



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

boolean_setting :mq_verify_peer, true

#mq_vhostObject

RabbitMQ vhost to use



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

string_setting :mq_vhost, '/'

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



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

boolean_setting :publisher_confirms, false

#read_timeoutObject

Bunny’s socket read timeout



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

number_setting :read_timeout, 11

#write_timeoutObject

Bunny’s socket write timeout



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

number_setting :write_timeout, 11