Create new BustCacheWorker and start sending jobs to it (#5302) [deploy]
This commit is contained in:
parent
df796f7e07
commit
8650f5c5aa
5 changed files with 72 additions and 1 deletions
|
|
@ -647,6 +647,6 @@ class Article < ApplicationRecord
|
|||
end
|
||||
|
||||
def async_bust
|
||||
Articles::BustCacheJob.perform_later(id)
|
||||
Articles::BustCacheWorker.perform_async(id)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
13
app/workers/articles/bust_cache_worker.rb
Normal file
13
app/workers/articles/bust_cache_worker.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
module Articles
|
||||
class BustCacheWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
sidekiq_options queue: :high_priority, retry: 10
|
||||
|
||||
def perform(article_id, cache_buster = "CacheBuster")
|
||||
article = Article.find_by(id: article_id)
|
||||
|
||||
cache_buster.constantize.bust_article(article) if article
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -33,6 +33,7 @@ Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f }
|
|||
Dir[Rails.root.join("spec/system/shared_examples/**/*.rb")].sort.each { |f| require f }
|
||||
Dir[Rails.root.join("spec/models/shared_examples/**/*.rb")].sort.each { |f| require f }
|
||||
Dir[Rails.root.join("spec/jobs/shared_examples/**/*.rb")].sort.each { |f| require f }
|
||||
Dir[Rails.root.join("spec/workers/shared_examples/**/*.rb")].sort.each { |f| require f }
|
||||
|
||||
# Checks for pending migrations before tests are run.
|
||||
# If you are not using ActiveRecord, you can remove this line.
|
||||
|
|
|
|||
46
spec/workers/articles/bust_cache_worker_spec.rb
Normal file
46
spec/workers/articles/bust_cache_worker_spec.rb
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
require "rails_helper"
|
||||
|
||||
class NewBuster
|
||||
def self.bust_article(*); end
|
||||
end
|
||||
|
||||
RSpec.describe Articles::BustCacheWorker, type: :worker do
|
||||
include_examples "#enqueues_on_correct_queue", "high_priority", 1
|
||||
|
||||
describe "#perform" do
|
||||
let(:worker) { subject }
|
||||
|
||||
context "with article" do
|
||||
let(:article) { double }
|
||||
let(:article_id) { 1 }
|
||||
|
||||
before do
|
||||
allow(Article).to receive(:find_by).with(id: article_id).and_return(article)
|
||||
end
|
||||
|
||||
it "with cache buster defined busts cache with defined buster" do
|
||||
allow(NewBuster).to receive(:bust_article)
|
||||
worker.perform(article_id, "NewBuster")
|
||||
expect(NewBuster).to have_received(:bust_article).with(article)
|
||||
end
|
||||
|
||||
it "without cache buster defined busts cache with default" do
|
||||
allow(CacheBuster).to receive(:bust_article)
|
||||
worker.perform(article_id)
|
||||
expect(CacheBuster).to have_received(:bust_article).with(article)
|
||||
end
|
||||
end
|
||||
|
||||
context "without article" do
|
||||
it "does not error" do
|
||||
expect { worker.perform(nil, "CacheBuster") }.not_to raise_error
|
||||
end
|
||||
|
||||
it "does not bust cache" do
|
||||
allow(CacheBuster).to receive(:bust_article)
|
||||
worker.perform(nil)
|
||||
expect(CacheBuster).not_to have_received(:bust_article)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
spec/workers/shared_examples/enqueues_on_correct_queue.rb
Normal file
11
spec/workers/shared_examples/enqueues_on_correct_queue.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
RSpec.shared_examples "#enqueues_on_correct_queue" do |queue_name, args|
|
||||
describe "#perform_async" do
|
||||
it "enqueues the job" do
|
||||
Sidekiq::Testing.fake!
|
||||
|
||||
expect do
|
||||
described_class.perform_async(args)
|
||||
end.to change { Sidekiq::Queues[queue_name].size }.by(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue