-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathfile_data_source.rb
More file actions
230 lines (207 loc) · 7.81 KB
/
file_data_source.rb
File metadata and controls
230 lines (207 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
require 'ldclient-rb/in_memory_store'
require 'ldclient-rb/util'
require 'concurrent/atomics'
require 'json'
require 'yaml'
require 'pathname'
module LaunchDarkly
module Impl
module Integrations
class FileDataSourceImpl
# To avoid pulling in 'listen' and its transitive dependencies for people who aren't using the
# file data source or who don't need auto-updating, we only enable auto-update if the 'listen'
# gem has been provided by the host app.
@@have_listen = false
begin
require 'listen'
@@have_listen = true
rescue LoadError
# Ignored
end
#
# @param data_store [LaunchDarkly::Interfaces::FeatureStore]
# @param data_source_update_sink [LaunchDarkly::Interfaces::DataSource::UpdateSink, nil] Might be nil for backwards compatibility reasons.
# @param logger [Logger]
# @param options [Hash]
#
def initialize(data_store, data_source_update_sink, logger, options={})
@data_store = data_source_update_sink || data_store
@data_source_update_sink = data_source_update_sink
@logger = logger
@paths = options[:paths] || []
if @paths.is_a? String
@paths = [ @paths ]
end
@allow_duplicates = options[:allow_duplicates] || false
@auto_update = options[:auto_update]
@use_listen = @auto_update && @@have_listen && !options[:force_polling]
@poll_interval = options[:poll_interval] || 1
@initialized = Concurrent::AtomicBoolean.new(false)
@ready = Concurrent::Event.new
@version_lock = Mutex.new
@last_version = 1
end
def initialized?
@initialized.value
end
def start
ready = Concurrent::Event.new
# We will return immediately regardless of whether the file load succeeded or failed -
# the difference can be detected by checking "initialized?"
ready.set
load_all
if @auto_update
# If we're going to watch files, then the start event will be set the first time we get
# a successful load.
@listener = start_listener
end
ready
end
def stop
@listener.stop unless @listener.nil?
end
private
def load_all
all_data = {
FEATURES => {},
SEGMENTS => {},
}
@paths.each do |path|
begin
load_file(path, all_data)
rescue => exn
LaunchDarkly::Util.log_exception(@logger, "Unable to load flag data from \"#{path}\"", exn)
@data_source_update_sink&.update_status(
LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED,
LaunchDarkly::Interfaces::DataSource::ErrorInfo.new(LaunchDarkly::Interfaces::DataSource::ErrorInfo::INVALID_DATA, 0, exn.to_s, Time.now)
)
return
end
end
@data_store.init(all_data)
@data_source_update_sink&.update_status(LaunchDarkly::Interfaces::DataSource::Status::VALID, nil)
@initialized.make_true
end
def load_file(path, all_data)
version = 1
@version_lock.synchronize {
version = @last_version
@last_version += 1
}
parsed = parse_content(IO.read(path))
(parsed[:flags] || {}).each do |key, flag|
flag[:version] = version
add_item(all_data, FEATURES, flag)
end
(parsed[:flagValues] || {}).each do |key, value|
add_item(all_data, FEATURES, make_flag_with_value(key.to_s, value, version))
end
(parsed[:segments] || {}).each do |key, segment|
segment[:version] = version
add_item(all_data, SEGMENTS, segment)
end
end
def parse_content(content)
# We can use the Ruby YAML parser for both YAML and JSON (JSON is a subset of YAML and while
# not all YAML parsers handle it correctly, we have verified that the Ruby one does, at least
# for all the samples of actual flag data that we've tested).
symbolize_all_keys(YAML.safe_load(content))
end
def symbolize_all_keys(value)
# This is necessary because YAML.load doesn't have an option for parsing keys as symbols, and
# the SDK expects all objects to be formatted that way.
if value.is_a?(Hash)
value.map{ |k, v| [k.to_sym, symbolize_all_keys(v)] }.to_h
elsif value.is_a?(Array)
value.map{ |v| symbolize_all_keys(v) }
else
value
end
end
def add_item(all_data, kind, item)
items = all_data[kind]
raise ArgumentError, "Received unknown item kind #{kind[:namespace]} in add_data" if items.nil? # shouldn't be possible since we preinitialize the hash
key = item[:key].to_sym
unless items[key].nil? || @allow_duplicates
raise ArgumentError, "#{kind[:namespace]} key \"#{item[:key]}\" was used more than once"
end
items[key] = Model.deserialize(kind, item)
end
def make_flag_with_value(key, value, version)
{
key: key,
on: true,
version: version,
fallthrough: { variation: 0 },
variations: [ value ],
}
end
def start_listener
resolved_paths = @paths.map { |p| Pathname.new(File.absolute_path(p)).realpath.to_s }
if @use_listen
start_listener_with_listen_gem(resolved_paths)
else
FileDataSourcePoller.new(resolved_paths, @poll_interval, self.method(:load_all), @logger)
end
end
def start_listener_with_listen_gem(resolved_paths)
path_set = resolved_paths.to_set
dir_paths = resolved_paths.map{ |p| File.dirname(p) }.uniq
opts = { latency: @poll_interval }
l = Listen.to(*dir_paths, opts) do |modified, added, removed|
paths = modified + added + removed
if paths.any? { |p| path_set.include?(p) }
load_all
end
end
l.start
l
end
#
# Used internally by FileDataSource to track data file changes if the 'listen' gem is not available.
#
class FileDataSourcePoller
def initialize(resolved_paths, interval, reloader, logger)
@stopped = Concurrent::AtomicBoolean.new(false)
get_file_times = Proc.new 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
while true
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
reloader.call if changed
rescue => exn
LaunchDarkly::Util.log_exception(logger, "Unexpected exception in FileDataSourcePoller", exn)
end
end
end
end
def stop
@stopped.make_true
@thread.run # wakes it up if it's sleeping
end
end
end
end
end
end