Class: LaunchDarkly::Impl::Integrations::FileDataSourcePollerV2 Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Used internally by FileDataSourceV2 to track data file changes if the 'listen' gem is not available.

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(resolved_paths, interval, on_change_callback, logger) ⇒ FileDataSourcePollerV2

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the file data poller.

Parameters:

  • resolved_paths (Array<String>)

    resolved file paths to watch

  • interval (Float)

    polling interval in seconds

  • on_change_callback (Proc)

    callback to invoke when files change

  • logger (Logger)

    the logger

Since:

  • 5.5.0



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/ldclient-rb/impl/integrations/file_data_source_v2.rb', line 410

def initialize(resolved_paths, interval, on_change_callback, logger)
  @stopped = Concurrent::AtomicBoolean.new(false)
  @on_change = on_change_callback
  @logger = logger

  get_file_times = proc do
    ret = {}
    resolved_paths.each do |path|
      begin
        ret[path] = File.mtime(path)
      rescue Errno::ENOENT
        ret[path] = nil
      end
    end
    ret
  end

  last_times = get_file_times.call
  @thread = Thread.new do
    loop do
      sleep interval
      break if @stopped.value

      begin
        new_times = get_file_times.call
        changed = false
        last_times.each do |path, old_time|
          new_time = new_times[path]
          if !new_time.nil? && new_time != old_time
            changed = true
            break
          end
        end
        last_times = new_times
        @on_change.call if changed
      rescue => e
        Impl::Util.log_exception(@logger, "Unexpected exception in FileDataSourcePollerV2", e)
      end
    end
  end
  @thread.name = "LD/FileDataSourceV2"
end

Instance Method Details

#stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



453
454
455
456
# File 'lib/ldclient-rb/impl/integrations/file_data_source_v2.rb', line 453

def stop
  @stopped.make_true
  @thread.run # wakes it up if it's sleeping
end