diff --git a/app/models/concerns/reactive_caching.rb b/app/models/concerns/reactive_caching.rb index 2589215ad19..eef9caf1c8e 100644 --- a/app/models/concerns/reactive_caching.rb +++ b/app/models/concerns/reactive_caching.rb @@ -60,13 +60,16 @@ module ReactiveCaching end def with_reactive_cache(*args, &blk) - within_reactive_cache_lifetime(*args) do + bootstrap = !within_reactive_cache_lifetime?(*args) + Rails.cache.write(alive_reactive_cache_key(*args), true, expires_in: self.class.reactive_cache_lifetime) + + if bootstrap + ReactiveCachingWorker.perform_async(self.class, id, *args) + nil + else data = Rails.cache.read(full_reactive_cache_key(*args)) yield data if data.present? end - ensure - Rails.cache.write(alive_reactive_cache_key(*args), true, expires_in: self.class.reactive_cache_lifetime) - ReactiveCachingWorker.perform_async(self.class, id, *args) end def clear_reactive_cache!(*args) @@ -75,7 +78,7 @@ module ReactiveCaching def exclusively_update_reactive_cache!(*args) locking_reactive_cache(*args) do - within_reactive_cache_lifetime(*args) do + if within_reactive_cache_lifetime?(*args) enqueuing_update(*args) do value = calculate_reactive_cache(*args) Rails.cache.write(full_reactive_cache_key(*args), value) @@ -105,8 +108,8 @@ module ReactiveCaching Gitlab::ExclusiveLease.cancel(full_reactive_cache_key(*args), uuid) end - def within_reactive_cache_lifetime(*args) - yield if Rails.cache.read(alive_reactive_cache_key(*args)) + def within_reactive_cache_lifetime?(*args) + !!Rails.cache.read(alive_reactive_cache_key(*args)) end def enqueuing_update(*args) diff --git a/changelogs/unreleased/fix-reactive-cache-retry-rate.yml b/changelogs/unreleased/fix-reactive-cache-retry-rate.yml new file mode 100644 index 00000000000..044e7fe39c0 --- /dev/null +++ b/changelogs/unreleased/fix-reactive-cache-retry-rate.yml @@ -0,0 +1,5 @@ +--- +title: Update commit status from external CI services less aggressively +merge_request: 18802 +author: +type: fixed diff --git a/spec/models/concerns/reactive_caching_spec.rb b/spec/models/concerns/reactive_caching_spec.rb index a5d505af001..4570dbb1d8e 100644 --- a/spec/models/concerns/reactive_caching_spec.rb +++ b/spec/models/concerns/reactive_caching_spec.rb @@ -29,12 +29,6 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do end end - let(:now) { Time.now.utc } - - around do |example| - Timecop.freeze(now) { example.run } - end - let(:calculation) { -> { 2 + 2 } } let(:cache_key) { "foo:666" } let(:instance) { CacheTest.new(666, &calculation) } @@ -49,13 +43,15 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do context 'when cache is empty' do it { is_expected.to be_nil } - it 'queues a background worker' do + it 'enqueues a background worker to bootstrap the cache' do expect(ReactiveCachingWorker).to receive(:perform_async).with(CacheTest, 666) go! end it 'updates the cache lifespan' do + expect(reactive_cache_alive?(instance)).to be_falsy + go! expect(reactive_cache_alive?(instance)).to be_truthy @@ -69,6 +65,18 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do it { is_expected.to eq(2) } + it 'does not enqueue a background worker' do + expect(ReactiveCachingWorker).not_to receive(:perform_async) + + go! + end + + it 'updates the cache lifespan' do + expect(Rails.cache).to receive(:write).with(alive_reactive_cache_key(instance), true, expires_in: anything) + + go! + end + context 'and expired' do before do invalidate_reactive_cache(instance)