From bc34d04c31b4e280ce23c35cb764039eb6dfb1e1 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 6 Mar 2019 15:57:18 +0100 Subject: [PATCH] Do not retry when refreshing cache on deleted OC --- app/jobs/refresh_products_cache_job.rb | 2 ++ spec/jobs/refresh_products_cache_job_spec.rb | 30 +++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/jobs/refresh_products_cache_job.rb b/app/jobs/refresh_products_cache_job.rb index 2662237dc1..b08148b280 100644 --- a/app/jobs/refresh_products_cache_job.rb +++ b/app/jobs/refresh_products_cache_job.rb @@ -3,6 +3,8 @@ require 'open_food_network/products_renderer' RefreshProductsCacheJob = Struct.new(:distributor_id, :order_cycle_id) do def perform Rails.cache.write(key, products_json) + rescue ActiveRecord::RecordNotFound + true end private diff --git a/spec/jobs/refresh_products_cache_job_spec.rb b/spec/jobs/refresh_products_cache_job_spec.rb index d20efe5fad..c1db2f2732 100644 --- a/spec/jobs/refresh_products_cache_job_spec.rb +++ b/spec/jobs/refresh_products_cache_job_spec.rb @@ -5,12 +5,34 @@ describe RefreshProductsCacheJob do let(:distributor) { create(:distributor_enterprise) } let(:order_cycle) { create(:simple_order_cycle) } - it "renders products and writes them to cache" do - RefreshProductsCacheJob.any_instance.stub(:products_json) { 'products' } + context 'when the enterprise and the order cycle exist' do + it "renders products and writes them to cache" do + RefreshProductsCacheJob.any_instance.stub(:products_json) { 'products' } - run_job RefreshProductsCacheJob.new distributor.id, order_cycle.id + run_job RefreshProductsCacheJob.new distributor.id, order_cycle.id - expect(Rails.cache.read("products-json-#{distributor.id}-#{order_cycle.id}")).to eq 'products' + expect(Rails.cache.read("products-json-#{distributor.id}-#{order_cycle.id}")).to eq 'products' + end + end + + context 'when the order cycle does not exist' do + before do + allow(OrderCycle) + .to receive(:find) + .with(order_cycle.id) + .and_raise(ActiveRecord::RecordNotFound) + end + + it 'does not raise' do + expect { + run_job RefreshProductsCacheJob.new(distributor.id, order_cycle.id) + }.not_to raise_error(/ActiveRecord::RecordNotFound/) + end + + it 'returns true' do + refresh_products_cache_job = RefreshProductsCacheJob.new(distributor.id, order_cycle.id) + expect(refresh_products_cache_job.perform).to eq(true) + end end describe "fetching products JSON" do