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
This commit is contained in:
parent
a86371e772
commit
c013079359
6 changed files with 55 additions and 52 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
14
app/workers/classified_listings/bust_cache_worker.rb
Normal file
14
app/workers/classified_listings/bust_cache_worker.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
39
spec/workers/classified_listings/bust_cache_worker_spec.rb
Normal file
39
spec/workers/classified_listings/bust_cache_worker_spec.rb
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue