Log a warning on cache MISS

This commit is contained in:
Rohan Mitchell
2016-02-04 11:33:59 +11:00
parent d0b7b4ee50
commit ff493c21d4
2 changed files with 36 additions and 5 deletions

View File

@@ -13,6 +13,8 @@ module OpenFoodNetwork
def products_json
products_json = Rails.cache.fetch("products-json-#{@distributor.id}-#{@order_cycle.id}") do
log_warning
begin
uncached_products_json
rescue ProductsRenderer::NoProducts
@@ -28,6 +30,12 @@ module OpenFoodNetwork
private
def log_warning
if Rails.env.production? || Rails.env.staging?
Bugsnag.notify RuntimeError.new("Live server MISS on products cache for distributor: #{@distributor.id}, order cycle: #{@order_cycle.id}")
end
end
def uncached_products_json
ProductsRenderer.new(@distributor, @order_cycle).products_json
end

View File

@@ -41,6 +41,11 @@ module OpenFoodNetwork
cpr.products_json
expect(cached_json).to eq 'fresh products'
end
it "logs a warning" do
cpr.should_receive :log_warning
cpr.products_json
end
end
describe "when there are no products" do
@@ -55,13 +60,31 @@ module OpenFoodNetwork
expect(cache_present).to be
expect(cached_json).to be_nil
end
it "logs a warning" do
cpr.should_receive :log_warning
expect { cpr.products_json }.to raise_error ProductsRenderer::NoProducts
end
end
end
describe "logging a warning" do
it "logs a warning when in production" do
Rails.env.stub(:production?) { true }
expect(Bugsnag).to receive(:notify)
cpr.send(:log_warning)
end
describe "logging a warning" do
it "logs a warning when in production"
it "logs a warning when in staging"
it "does not log a warning in development"
it "does not log a warning in test"
it "logs a warning when in staging" do
Rails.env.stub(:production?) { false }
Rails.env.stub(:staging?) { true }
expect(Bugsnag).to receive(:notify)
cpr.send(:log_warning)
end
it "does not log a warning in development or test" do
expect(Bugsnag).to receive(:notify).never
cpr.send(:log_warning)
end
end