diff --git a/lib/racecar.rb b/lib/racecar.rb index 6c8b379e..a8883824 100644 --- a/lib/racecar.rb +++ b/lib/racecar.rb @@ -54,11 +54,9 @@ def self.wait_for_delivery(&block) def self.producer Thread.current[:racecar_producer] ||= begin - if config.datadog_enabled - require "racecar/datadog" - end - Racecar::Producer.new(config: config, logger: logger, instrumenter: instrumenter) - end + ConfigLoader.load! + Racecar::Producer.new(config: config, logger: logger, instrumenter: instrumenter) + end end def self.instrumenter diff --git a/lib/racecar/cli.rb b/lib/racecar/cli.rb index 84c79f99..6d376247 100644 --- a/lib/racecar/cli.rb +++ b/lib/racecar/cli.rb @@ -25,11 +25,7 @@ def initialize(args) def run $stderr.puts "=> Starting Racecar consumer #{consumer_name}..." - RailsConfigFileLoader.load! unless config.without_rails? - - if File.exist?("config/racecar.rb") - require "./config/racecar" - end + ConfigLoader.load! # Find the consumer class by name. consumer_class = Kernel.const_get(consumer_name) @@ -48,10 +44,6 @@ def run Racecar.logger.level = Object.const_get("Logger::#{config.log_level.upcase}") end - if config.datadog_enabled - configure_datadog - end - $stderr.puts "=> Wrooooom!" if config.daemonize @@ -152,16 +144,5 @@ def build_parser end end end - - def configure_datadog - require_relative './datadog' - - Datadog.configure do |datadog| - datadog.host = config.datadog_host unless config.datadog_host.nil? - datadog.port = config.datadog_port unless config.datadog_port.nil? - datadog.namespace = config.datadog_namespace unless config.datadog_namespace.nil? - datadog.tags = config.datadog_tags unless config.datadog_tags.nil? - end - end end end diff --git a/lib/racecar/config_loader.rb b/lib/racecar/config_loader.rb new file mode 100644 index 00000000..96c9ccf6 --- /dev/null +++ b/lib/racecar/config_loader.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Racecar + module ConfigLoader + def self.load! + # This loads config from ENV vars and cli args + config = Racecar.config + + # Load generic config file + if File.exist?("config/racecar.rb") + require "./config/racecar" + end + + # Load rails config unless explictly disabled already via env var or cli args + RailsConfigFileLoader.load! unless config.without_rails? + + if config.datadog_enabled + require_relative './datadog' + + Datadog.configure do |datadog| + datadog.host = config.datadog_host unless config.datadog_host.nil? + datadog.port = config.datadog_port unless config.datadog_port.nil? + datadog.namespace = config.datadog_namespace unless config.datadog_namespace.nil? + datadog.tags = config.datadog_tags unless config.datadog_tags.nil? + end + end + end + end +end diff --git a/lib/racecar/ctl.rb b/lib/racecar/ctl.rb index a45393b1..86e134b2 100644 --- a/lib/racecar/ctl.rb +++ b/lib/racecar/ctl.rb @@ -105,9 +105,9 @@ def produce(args) raise Racecar::Error, "no message value specified" end - RailsConfigFileLoader.load! + ConfigLoader.load! - Racecar.config.validate! + config.validate! producer = Rdkafka::Config.new({ "bootstrap.servers": Racecar.config.brokers.join(","),