Class: LaunchDarkly::Integrations::FileOverrideSourceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/integrations/file_data.rb

Overview

Builder for the file-based override source. Create it with LaunchDarkly::Integrations::FileData.override_source.

Flag overrides are currently experimental and subject to change.

Constant Summary collapse

DEFAULT_POLL_INTERVAL =

The default interval, in seconds, between examinations of the files in polling mode.

1
MINIMUM_POLL_INTERVAL =

The shortest allowed polling interval, in seconds. A configured interval below this is raised to it. The minimum exists only to prevent a tight loop over the file system.

1

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FileOverrideSourceBuilder

Returns a new instance of FileOverrideSourceBuilder.

Parameters:

Raises:

  • (ArgumentError)


259
260
261
262
263
# File 'lib/ldclient-rb/integrations/file_data.rb', line 259

def initialize(options)
  raise ArgumentError, "options for the file-based override source must be a Hash" unless options.is_a?(Hash)

  @options = options
end

Instance Method Details

#build(sdk_key, config) ⇒ LaunchDarkly::Interfaces::Overrides::OverrideSource

Builds the override source. Called by the SDK when the client is created.

Parameters:

  • sdk_key (String) —

    unused

  • config (LaunchDarkly::Config) —

    the client configuration, for its logger

Returns:

Raises:

  • (ArgumentError) —

    if the options are invalid



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/ldclient-rb/integrations/file_data.rb', line 273

def build(sdk_key, config)
  unknown = @options.keys - OPTION_KEYS
  raise ArgumentError, "unknown options for the file-based override source: #{unknown.join(', ')}" unless unknown.empty?

  paths = Array(@options[:paths])
  raise ArgumentError, "no file paths were specified for the file-based override source" if paths.empty?

  invalid = paths.reject { |path| path.is_a?(String) && !path.empty? }
  unless invalid.empty?
    raise ArgumentError,
      "file paths for the file-based override source must be non-empty strings; got #{invalid.map(&:inspect).join(', ')}"
  end
  paths = Impl::FileData.absolute_paths(paths)
  # A path listed more than once would make every entry of that file a duplicate of itself, so
  # the repeats are dropped and the first occurrence keeps its place in the order. File names
  # on Windows ignore case, so there paths that differ only in case name the same file.
  unique_paths = Gem.win_platform? ? paths.uniq(&:downcase) : paths.uniq
  if unique_paths.size != paths.size
    config.logger.warn do
      "#{Impl::Integrations::FileOverrideSource::LOG_PREFIX} File paths list the same file more than once; the repeats are ignored"
    end
    paths = unique_paths
  end

  duplicate_keys_handling = @options.fetch(:duplicate_keys_handling, :fail)
  unless DUPLICATE_KEYS_HANDLING_VALUES.include?(duplicate_keys_handling)
    raise ArgumentError,
      "unrecognized duplicate keys handling #{duplicate_keys_handling.inspect} for the file-based override source"
  end

  change_detection = @options.fetch(:change_detection, :polling)
  unless CHANGE_DETECTION_VALUES.include?(change_detection)
    raise ArgumentError, "unrecognized change detection mode #{change_detection.inspect} for the file-based override source"
  end
  if change_detection == :watching && !Impl::FileData::Watcher.available?
    raise ArgumentError, "change detection mode :watching for the file-based override source requires the listen gem"
  end

  poll_interval = @options.fetch(:poll_interval, DEFAULT_POLL_INTERVAL)
  unless poll_interval.is_a?(Numeric)
    raise ArgumentError, "poll interval #{poll_interval.inspect} for the file-based override source must be a number"
  end
  if change_detection == :polling && poll_interval < MINIMUM_POLL_INTERVAL
    config.logger.warn do
      "#{Impl::Integrations::FileOverrideSource::LOG_PREFIX} Poll interval #{poll_interval}s is below the minimum; using #{MINIMUM_POLL_INTERVAL}s"
    end
    poll_interval = MINIMUM_POLL_INTERVAL
  end

  Impl::Integrations::FileOverrideSource.new(
    paths: paths,
    duplicate_keys_handling: duplicate_keys_handling,
    change_detection: change_detection,
    poll_interval: poll_interval,
    logger: config.logger
  )
end