Class: Racecar::Config

Inherits:
KingKonf::Config
  • Object
show all
Defined in:
lib/racecar/config.rb

Constant Summary collapse

STATISTICS_DISABLED_VALUE =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV) ⇒ Config

Returns a new instance of Config.



226
227
228
229
230
231
# File 'lib/racecar/config.rb', line 226

def initialize(env: ENV)
  super(env: env)
  @error_handler = proc {}
  @subscriptions = []
  @logger = Logger.new(STDOUT)
end

Instance Attribute Details

#consumer_classObject

Returns the value of attribute consumer_class.



272
273
274
# File 'lib/racecar/config.rb', line 272

def consumer_class
  @consumer_class
end

#error_handlerObject (readonly)

The error handler must be set directly on the object.



210
211
212
# File 'lib/racecar/config.rb', line 210

def error_handler
  @error_handler
end

#instrumenterObject



294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/racecar/config.rb', line 294

def instrumenter
  @instrumenter ||= begin
    default_payload = { client_id: client_id, group_id: group_id }

    if defined?(ActiveSupport::Notifications)
      # ActiveSupport needs `concurrent-ruby` but doesn't `require` it.
      require 'concurrent/utility/monotonic_time'
      Instrumenter.new(backend: ActiveSupport::Notifications, default_payload: default_payload)
    else
      logger.warn "ActiveSupport::Notifications not available, instrumentation is disabled"
      NullInstrumenter
    end
  end
end

#loggerObject

Returns the value of attribute logger.



212
213
214
# File 'lib/racecar/config.rb', line 212

def logger
  @logger
end

#parallel_workersObject

Returns the value of attribute parallel_workers.



212
213
214
# File 'lib/racecar/config.rb', line 212

def parallel_workers
  @parallel_workers
end

#subscriptionsObject

Returns the value of attribute subscriptions.



212
213
214
# File 'lib/racecar/config.rb', line 212

def subscriptions
  @subscriptions
end

Instance Method Details

#inspectObject



233
234
235
236
237
238
# File 'lib/racecar/config.rb', line 233

def inspect
  self.class.variables
    .map(&:name)
    .map {|key| [key, get(key).inspect].join(" = ") }
    .join("\n")
end

#install_liveness_probeObject



310
311
312
# File 'lib/racecar/config.rb', line 310

def install_liveness_probe
  liveness_probe.tap(&:install)
end

#liveness_probeObject



314
315
316
317
318
319
320
321
# File 'lib/racecar/config.rb', line 314

def liveness_probe
  require "active_support/notifications"
  @liveness_probe ||= LivenessProbe.new(
    ActiveSupport::Notifications,
    liveness_probe_file_path,
    liveness_probe_max_interval
  )
end

#load_consumer_class(consumer_class) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/racecar/config.rb', line 254

def load_consumer_class(consumer_class)
  self.consumer_class = consumer_class
  self.group_id = consumer_class.group_id || self.group_id

  self.group_id ||= [
    # Configurable and optional prefix:
    group_id_prefix,

    # MyFunnyConsumer => my-funny-consumer
    consumer_class.name.gsub(/[a-z][A-Z]/) { |str| "#{str[0]}-#{str[1]}" }.downcase,
  ].compact.join

  self.parallel_workers = consumer_class.parallel_workers
  self.subscriptions = consumer_class.subscriptions
  self.max_wait_time = consumer_class.max_wait_time || self.max_wait_time
  self.fetch_messages = consumer_class.fetch_messages || self.fetch_messages
  self.pidfile ||= "#{group_id}.pid"
end

#max_wait_time_msObject



222
223
224
# File 'lib/racecar/config.rb', line 222

def max_wait_time_ms
  max_wait_time * 1000
end

#on_error(&handler) ⇒ Object



274
275
276
# File 'lib/racecar/config.rb', line 274

def on_error(&handler)
  @error_handler = handler
end

#rdkafka_consumerObject



278
279
280
281
282
283
284
# File 'lib/racecar/config.rb', line 278

def rdkafka_consumer
  consumer_config = consumer.map do |param|
    param.split("=", 2).map(&:strip)
  end.to_h
  consumer_config.merge!(rdkafka_security_config)
  consumer_config
end

#rdkafka_producerObject



286
287
288
289
290
291
292
# File 'lib/racecar/config.rb', line 286

def rdkafka_producer
  producer_config = producer.map do |param|
    param.split("=", 2).map(&:strip)
  end.to_h
  producer_config.merge!(rdkafka_security_config)
  producer_config
end

#statistics_interval_msObject



214
215
216
217
218
219
220
# File 'lib/racecar/config.rb', line 214

def statistics_interval_ms
  if Rdkafka::Config.statistics_callback
    statistics_interval * 1000
  else
    STATISTICS_DISABLED_VALUE
  end
end

#validate!Object



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/racecar/config.rb', line 240

def validate!
  if brokers.empty?
    raise ConfigError, "`brokers` must not be empty"
  end

  if socket_timeout <= max_wait_time
    raise ConfigError, "`socket_timeout` must be longer than `max_wait_time`"
  end

  if max_pause_timeout && !pause_with_exponential_backoff?
    raise ConfigError, "`max_pause_timeout` only makes sense when `pause_with_exponential_backoff` is enabled"
  end
end