From c013079359d215e334df5b59e95a5794f70356c8 Mon Sep 17 00:00:00 2001 From: Alain Mauri Date: Thu, 9 Jan 2020 22:23:23 +0000 Subject: [PATCH] Move Classified Listing Cache Buster to Sidekiq (#5408) [deploy] * Created the worker to bust cache for classified listings * Created the related tests * Removed the old job --- .../concerns/classified_listings_toolkit.rb | 2 +- .../classified_listings/bust_cache_job.rb | 12 ------ .../classified_listings/bust_cache_worker.rb | 14 +++++++ .../bust_cache_job_spec.rb | 39 ------------------- .../internal/classified_listings_spec.rb | 1 + .../bust_cache_worker_spec.rb | 39 +++++++++++++++++++ 6 files changed, 55 insertions(+), 52 deletions(-) delete mode 100644 app/jobs/classified_listings/bust_cache_job.rb create mode 100644 app/workers/classified_listings/bust_cache_worker.rb delete mode 100644 spec/jobs/classified_listings/bust_cache_job_spec.rb create mode 100644 spec/workers/classified_listings/bust_cache_worker_spec.rb diff --git a/app/controllers/concerns/classified_listings_toolkit.rb b/app/controllers/concerns/classified_listings_toolkit.rb index fc9d5b650..9ba4b44bb 100644 --- a/app/controllers/concerns/classified_listings_toolkit.rb +++ b/app/controllers/concerns/classified_listings_toolkit.rb @@ -34,7 +34,7 @@ module ClassifiedListingsToolkit end def clear_listings_cache - ClassifiedListings::BustCacheJob.perform_now(@classified_listing.id) + ClassifiedListings::BustCacheWorker.perform_async(@classified_listing.id) end def set_classified_listing diff --git a/app/jobs/classified_listings/bust_cache_job.rb b/app/jobs/classified_listings/bust_cache_job.rb deleted file mode 100644 index f6f883eed..000000000 --- a/app/jobs/classified_listings/bust_cache_job.rb +++ /dev/null @@ -1,12 +0,0 @@ -module ClassifiedListings - class BustCacheJob < ApplicationJob - queue_as :classified_listings_bust_cache - - def perform(classified_listing_id, cache_buster = CacheBuster) - classified_listing = ClassifiedListing.find_by(id: classified_listing_id) - return unless classified_listing - - cache_buster.bust_classified_listings(classified_listing) - end - end -end diff --git a/app/workers/classified_listings/bust_cache_worker.rb b/app/workers/classified_listings/bust_cache_worker.rb new file mode 100644 index 000000000..70b0d2370 --- /dev/null +++ b/app/workers/classified_listings/bust_cache_worker.rb @@ -0,0 +1,14 @@ +module ClassifiedListings + class BustCacheWorker + include Sidekiq::Worker + + sidekiq_options queue: :high_priority, retry: 10 + + def perform(classified_listing_id) + classified_listing = ClassifiedListing.find_by(id: classified_listing_id) + return unless classified_listing + + CacheBuster.bust_classified_listings(classified_listing) + end + end +end diff --git a/spec/jobs/classified_listings/bust_cache_job_spec.rb b/spec/jobs/classified_listings/bust_cache_job_spec.rb deleted file mode 100644 index 0588b56b8..000000000 --- a/spec/jobs/classified_listings/bust_cache_job_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -require "rails_helper" - -RSpec.describe ClassifiedListings::BustCacheJob, type: :job do - include_examples "#enqueues_job", "classified_listings_bust_cache", 1 - - describe "#perform_now" do - let(:cache_buster) { double } - - before do - allow(cache_buster).to receive(:bust_classified_listings) - end - - context "with listing" do - let_it_be(:listing) { double } - let_it_be(:listing_id) { 1 } - - before do - allow(ClassifiedListing).to receive(:find_by).with(id: listing_id).and_return(listing) - end - - it "busts cache" do - described_class.perform_now(listing_id, cache_buster) - - expect(cache_buster).to have_received(:bust_classified_listings).with(listing) - end - end - - describe "when no listing is found" do - it "does not error" do - expect { described_class.perform_now(nil, cache_buster) }.not_to raise_error - end - - it "does not bust cache" do - described_class.perform_now(nil, cache_buster) - expect(cache_buster).not_to have_received(:bust_classified_listings) - end - end - end -end diff --git a/spec/requests/internal/classified_listings_spec.rb b/spec/requests/internal/classified_listings_spec.rb index f0be48ca7..81b736b50 100644 --- a/spec/requests/internal/classified_listings_spec.rb +++ b/spec/requests/internal/classified_listings_spec.rb @@ -14,6 +14,7 @@ RSpec.describe "/internal/listings", type: :request do put "/internal/listings/#{classified_listing.id}", params: { classified_listing: { title: "updated" } } + sidekiq_perform_enqueued_jobs expect(CacheBuster).to have_received(:bust_classified_listings) end end diff --git a/spec/workers/classified_listings/bust_cache_worker_spec.rb b/spec/workers/classified_listings/bust_cache_worker_spec.rb new file mode 100644 index 000000000..c6b89c9b2 --- /dev/null +++ b/spec/workers/classified_listings/bust_cache_worker_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +RSpec.describe ClassifiedListings::BustCacheWorker, type: :worker do + include_examples "#enqueues_on_correct_queue", "high_priority", 1 + + describe "#perform" do + let(:worker) { subject } + + before do + allow(CacheBuster).to receive(:bust_classified_listings) + end + + context "with listing" do + let(:listing) { double } + let(:listing_id) { 1 } + + before do + allow(ClassifiedListing).to receive(:find_by).with(id: listing_id).and_return(listing) + end + + it "busts cache" do + worker.perform(listing_id) + + expect(CacheBuster).to have_received(:bust_classified_listings).with(listing) + end + end + + describe "when no listing is found" do + it "does not error" do + expect { worker.perform(nil) }.not_to raise_error + end + + it "does not bust cache" do + worker.perform(nil) + expect(CacheBuster).not_to have_received(:bust_classified_listings) + end + end + end +end