Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load config consistently everywhere #359

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/racecar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 1 addition & 20 deletions lib/racecar/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions lib/racecar/config_loader.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions lib/racecar/ctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(","),
Expand Down
Loading