Skip to content

Commit e5841c5

Browse files
committed
fix: resolve Cloud sync settings when building adapters
1 parent 0320f41 commit e5841c5

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

lib/flipper/cloud.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ def self.set_default(instrumenter: nil)
4747
configuration = Flipper.configuration
4848
context = default_context(configuration)
4949
local_memory = context.fetch(:memory)
50-
webhook_sync = !ENV.fetch("FLIPPER_CLOUD_SYNC_SECRET", "").empty?
51-
sync_interval = [
52-
Flipper::Typecast.to_float(ENV.fetch("FLIPPER_CLOUD_SYNC_INTERVAL", 10)),
53-
Flipper::Poller::MINIMUM_POLL_INTERVAL,
54-
].max
5550
Flipper.configure do |config|
5651
config.wrap_adapter_store(:flipper_cloud_memory) do |persistent_adapter|
5752
context.fetch(:state).lock.synchronize do
@@ -60,7 +55,11 @@ def self.set_default(instrumenter: nil)
6055
context[:loaded] = true
6156
end
6257
end
63-
if webhook_sync && !memory_store?(persistent_adapter)
58+
if !ENV.fetch("FLIPPER_CLOUD_SYNC_SECRET", "").empty? && !memory_store?(persistent_adapter)
59+
sync_interval = [
60+
Flipper::Typecast.to_float(ENV.fetch("FLIPPER_CLOUD_SYNC_INTERVAL", 10)),
61+
Flipper::Poller::MINIMUM_POLL_INTERVAL,
62+
].max
6463
Flipper::Adapters::Sync.new(
6564
local_memory,
6665
persistent_adapter,

spec/flipper/cloud_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,41 @@
251251
persistent_file&.unlink
252252
end
253253

254+
it 'refreshes persistence when Rails loads the webhook secret after initial setup' do
255+
original_token = ENV['FLIPPER_CLOUD_TOKEN']
256+
original_secret = ENV['FLIPPER_CLOUD_SYNC_SECRET']
257+
original_interval = ENV['FLIPPER_CLOUD_SYNC_INTERVAL']
258+
ENV['FLIPPER_CLOUD_TOKEN'] = 'asdf'
259+
ENV.delete('FLIPPER_CLOUD_SYNC_SECRET')
260+
ENV['FLIPPER_CLOUD_SYNC_INTERVAL'] = '10'
261+
persistent_file = Tempfile.new("flipper-cloud")
262+
persistent_file.close
263+
persistent = Flipper::Adapters::PStore.new(persistent_file.path)
264+
Flipper.new(persistent).disable(:search)
265+
Flipper.configure { |config| config.adapter { persistent } }
266+
267+
described_class.set_default
268+
ENV['FLIPPER_CLOUD_SYNC_SECRET'] = 'secret'
269+
ENV['FLIPPER_CLOUD_SYNC_INTERVAL'] = '30'
270+
described_class.set_default(instrumenter: Flipper::Instrumenters::Memory.new)
271+
expect(Flipper::Poller).not_to receive(:get)
272+
instance = Flipper.configuration.default
273+
expect(instance.enabled?(:search)).to be(false)
274+
Flipper.new(persistent).enable(:search)
275+
276+
now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
277+
allow(Process).to receive(:clock_gettime).and_call_original
278+
allow(Process).to receive(:clock_gettime).with(Process::CLOCK_MONOTONIC, :second).and_return(now + 20)
279+
expect(instance.enabled?(:search)).to be(false)
280+
allow(Process).to receive(:clock_gettime).with(Process::CLOCK_MONOTONIC, :second).and_return(now + 31)
281+
expect(instance.enabled?(:search)).to be(true)
282+
ensure
283+
ENV['FLIPPER_CLOUD_TOKEN'] = original_token
284+
ENV['FLIPPER_CLOUD_SYNC_SECRET'] = original_secret
285+
ENV['FLIPPER_CLOUD_SYNC_INTERVAL'] = original_interval
286+
persistent_file&.unlink
287+
end
288+
254289
it 'keeps configured behavioral adapters outside memory reads' do
255290
original_token = ENV['FLIPPER_CLOUD_TOKEN']
256291
ENV['FLIPPER_CLOUD_TOKEN'] = 'asdf'

0 commit comments

Comments
 (0)