Class: LaunchDarkly::Impl::FileData::Poller Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/file_data/poller.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.

Detects changes to a set of files by examining them on a fixed interval. Use it where file system change notifications are not available or not reliable. A change to the modification time or the size of any file invokes the callback. A file that appears or disappears is also a change. A file that cannot be examined counts as absent.

The poller samples the files once per interval and compares only modification time and size. A rewrite that keeps both values is not detected.

Detection is generous. The callback can run for a change that does not alter the effective data. Feed it into a Reloader, whose debouncing and skip-unchanged handling absorb the excess.

Since:

  • 5.5.0

Defined Under Namespace

Classes: FileState

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths, interval, on_change, logger) ⇒ Poller

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.

Creates and starts a poller. It examines the files once before it returns, so only later changes invoke the callback. Call #stop to stop it.

Parameters:

  • paths (Array<String>) —

    absolute paths of the files to examine

  • interval (Numeric) —

    seconds between examinations

  • on_change (#call) —

    invoked with no arguments when a change is detected

  • logger (Logger)

Since:

  • 5.5.0



39
40
41
42
43
44
45
# File 'lib/ldclient-rb/impl/file_data/poller.rb', line 39

def initialize(paths, interval, on_change, logger)
  @paths = paths
  @on_change = on_change
  @last = Poller.observe_all(paths)
  @task = RepeatingTask.new(interval, interval, method(:examine), logger, "LD/FileDataPoller")
  @task.start
end

Class Method Details

.observe(path) ⇒ FileState

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.

Examines one file. The state is that of the file a symbolic link leads to.

Parameters:

  • path (String)

Returns:

Since:

  • 5.5.0



71
72
73
74
75
76
# File 'lib/ldclient-rb/impl/file_data/poller.rb', line 71

def self.observe(path)
  stat = File.stat(path)
  FileState.new(true, stat.mtime, stat.size)
rescue SystemCallError
  ABSENT
end

.observe_all(paths) ⇒ Array<FileState>

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.

Examines every file and returns the observed states, in path order.

Parameters:

  • paths (Array<String>)

Returns:

Since:

  • 5.5.0



61
62
63
# File 'lib/ldclient-rb/impl/file_data/poller.rb', line 61

def self.observe_all(paths)
  paths.map { |path| observe(path) }
end

Instance Method Details

#stop ⇒ Object

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.

Stops the poller and waits for the worker thread to finish. A callback that is already running completes first.

Since:

  • 5.5.0



51
52
53
# File 'lib/ldclient-rb/impl/file_data/poller.rb', line 51

def stop
  @task.stop
end