class Airbrake::RemoteSettings

RemoteSettings polls the remote config of the passed project at fixed intervals. The fetched config is yielded as a callback parameter so that the invoker can define read config values. Supports proxies.

@example Disable/enable error notifications based on the remote value

RemoteSettings.poll do |data|
  config.error_notifications = data.error_notifications?
end

@since v5.0.0 @api private

Constants

HTTP_OK

@return [String]

QUERY_PARAMS

@return [Hash{Symbol=>String}] metadata to be attached to every GET

request

Public Class Methods

new(project_id, host, &block) click to toggle source

@param [Integer] project_id @yield [data] @yieldparam data [Airbrake::RemoteSettings::SettingsData]

# File lib/airbrake-ruby/remote_settings.rb, line 42
def initialize(project_id, host, &block)
  @data = SettingsData.new(project_id, {})
  @host = host
  @block = block
  @config = Airbrake::Config.instance
  @poll = nil
end
poll(project_id, host, &block) click to toggle source

Polls remote config of the given project.

@param [Integer] project_id @param [String] host @yield [data] @yieldparam data [Airbrake::RemoteSettings::SettingsData] @return [Airbrake::RemoteSettings]

# File lib/airbrake-ruby/remote_settings.rb, line 35
def self.poll(project_id, host, &block)
  new(project_id, host, &block).poll
end

Public Instance Methods

poll() click to toggle source

Polls remote config of the given project in background.

@return [self]

# File lib/airbrake-ruby/remote_settings.rb, line 53
def poll
  @poll ||= Thread.new do
    @block.call(@data)

    loop do
      @block.call(@data.merge!(fetch_config))
      sleep(@data.interval)
    end
  end

  self
end
stop_polling() click to toggle source

Stops the background poller thread.

@return [void]

# File lib/airbrake-ruby/remote_settings.rb, line 69
def stop_polling
  @poll.kill if @poll
end

Private Instance Methods

build_config_uri() click to toggle source
# File lib/airbrake-ruby/remote_settings.rb, line 105
def build_config_uri
  uri = URI(@data.config_route(@host))
  uri.query = QUERY_PARAMS
  uri
end
build_https(uri) click to toggle source
# File lib/airbrake-ruby/remote_settings.rb, line 111
def build_https(uri)
  Net::HTTP.new(uri.host, uri.port, *proxy_params).tap do |https|
    https.use_ssl = uri.is_a?(URI::HTTPS)
    if @config.timeout
      https.open_timeout = @config.timeout
      https.read_timeout = @config.timeout
    end
  end
end
fetch_config() click to toggle source
# File lib/airbrake-ruby/remote_settings.rb, line 75
def fetch_config
  uri = build_config_uri
  https = build_https(uri)
  req = Net::HTTP::Get.new(uri.request_uri)
  response = nil

  begin
    response = https.request(req)
  rescue StandardError => ex
    reason = "#{LOG_LABEL} HTTP error: #{ex}"
    logger.error(reason)
    return {}
  end

  unless response.code == HTTP_OK
    logger.error(response.body)
    return {}
  end

  json = nil
  begin
    json = JSON.parse(response.body)
  rescue JSON::ParserError => ex
    logger.error(ex)
    return {}
  end

  json
end
proxy_params() click to toggle source
# File lib/airbrake-ruby/remote_settings.rb, line 121
def proxy_params
  return unless @config.proxy.key?(:host)

  [@config.proxy[:host], @config.proxy[:port], @config.proxy[:user],
   @config.proxy[:password]]
end