Class: LaunchDarkly::Impl::FileData::Watcher Private
- Inherits:
-
Object
- Object
- LaunchDarkly::Impl::FileData::Watcher
- Defined in:
- lib/ldclient-rb/impl/file_data/watcher.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 through file system change notifications. The
notification mechanism comes from the optional listen gem, which the SDK does not depend
on. Check Watcher.available? before constructing a watcher.
On Linux the watcher uses rb-inotify, which listen depends on, to watch the directory of
each file without descending into subdirectories. Elsewhere it uses listen itself, which
scans the whole directory tree under each watched directory.
The watcher observes the directory of each file, so a configured file that does not exist yet is picked up when it appears. Each directory is watched on its own: one that does not exist, or that is deleted while it is watched, is logged and retried once per second until it exists, while the other directories stay watched. When a retry sets up a watch, the callback runs once, so that a change made while there was no watch is not missed.
A configured file can be a symbolic link to a file in another directory. The watcher also watches the directory of the link's target for the target's name, so that an edit to the target is reported. The links are resolved again after every reported change, after a retry sets up watches, and when a directory that holds a target is deleted, as happens when a Kubernetes ConfigMap volume is updated. A link that cannot be resolved, for example because its target's directory does not exist yet or was deleted, is resolved again once per second until its target is watched, and the callback then runs once.
A change that none of those events reports is not followed: a link further along the chain
that is pointed elsewhere while its old target stays, a configured directory that is itself
a link and is pointed elsewhere, or a configured directory that is renamed and replaced.
Deployments that switch files that way, such as a current link to a release directory,
should use polling, which reads every configured path on each interval.
Constant Summary collapse
- RETRY_INTERVAL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Seconds between attempts to set up the watches after a failure.
1.0
Class Method Summary collapse
-
.available? ⇒ Boolean
private
Returns true if a change notification mechanism can be loaded.
-
.inotify_available? ⇒ Boolean
private
Returns true if
rb-inotifycan be loaded. -
.listen_available? ⇒ Boolean
private
Returns true if the
listengem can be loaded.
Instance Method Summary collapse
-
#initialize(paths, on_change, logger) ⇒ Watcher
constructor
private
Creates and starts a watcher.
-
#stop ⇒ Object
private
Stops the watcher.
Constructor Details
#initialize(paths, on_change, logger) ⇒ Watcher
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 watcher.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/ldclient-rb/impl/file_data/watcher.rb', line 112 def initialize(paths, on_change, logger) @paths = paths @on_change = on_change @logger = logger @stopped = Concurrent::AtomicBoolean.new(false) @lock = Mutex.new @retry_task = nil @last_error_message = nil # The directories of the configured paths, in configuration order. A directory is in # @missing until it is watched, and then in @watched under its real path. The lock # guards both, @retry_task, and @inotify. @directories = paths.map { |p| File.dirname(p) }.uniq @missing = Set.new(@directories) @watched = {} @inotify = nil # True while a configured link's target is not watched, because the link cannot be # resolved or its target's directory cannot be watched. The retry runs while it is true. # The lock guards it. @links_unresolved = false @last_link_message = nil try_start refresh_links schedule_retry unless @lock.synchronize { @missing.empty? } end |
Class Method Details
.available? ⇒ Boolean
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.
Returns true if a change notification mechanism can be loaded.
73 74 75 |
# File 'lib/ldclient-rb/impl/file_data/watcher.rb', line 73 def self.available? inotify_available? || listen_available? end |
.inotify_available? ⇒ Boolean
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.
Returns true if rb-inotify can be loaded. It is a dependency of listen on Linux and
does not load on other platforms.
83 84 85 86 |
# File 'lib/ldclient-rb/impl/file_data/watcher.rb', line 83 def self.inotify_available? @inotify_available = load_library("rb-inotify") if @inotify_available.nil? @inotify_available end |
.listen_available? ⇒ Boolean
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.
Returns true if the listen gem can be loaded.
93 94 95 96 |
# File 'lib/ldclient-rb/impl/file_data/watcher.rb', line 93 def self.listen_available? @listen_available = load_library("listen") if @listen_available.nil? @listen_available 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 watcher. No callback runs after this method returns, apart from one that is already in progress.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ldclient-rb/impl/file_data/watcher.rb', line 142 def stop return unless @stopped.make_true closers, retry_task, inotify = @lock.synchronize do state = [@watched.values.map(&:close), @retry_task, @inotify] @watched.clear @retry_task = nil @inotify = nil state end retry_task&.stop closers.each(&:call) inotify&.stop end |