Change BustReactableCacheJob to BustReactableCacheWorker and move to sidekiq (#5582) [deploy]

This commit is contained in:
Lucas Hiago 2020-02-12 13:50:40 -03:00 committed by GitHub
parent 6b481166fe
commit d0b4ba2648
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 9 deletions

View file

@ -21,9 +21,9 @@ class Reaction < ApplicationRecord
validate :permissions
before_save :assign_points
after_commit :async_bust
after_commit :async_bust, :bust_reactable_cache
after_save :index_to_algolia
after_save :update_reactable, :bust_reactable_cache, :touch_user
after_save :update_reactable, :touch_user
before_destroy :update_reactable_without_delay, unless: :destroyed_by_association
before_destroy :bust_reactable_cache_without_delay
before_destroy :remove_algolia
@ -84,7 +84,7 @@ class Reaction < ApplicationRecord
end
def bust_reactable_cache
Reactions::BustReactableCacheJob.perform_later(id)
Reactions::BustReactableCacheWorker.perform_async(id)
end
def async_bust
@ -92,7 +92,7 @@ class Reaction < ApplicationRecord
end
def bust_reactable_cache_without_delay
Reactions::BustReactableCacheJob.perform_now(id)
Reactions::BustReactableCacheWorker.new.perform(id)
end
def update_reactable_without_delay

View file

@ -0,0 +1,20 @@
module Reactions
class BustReactableCacheWorker
include Sidekiq::Worker
sidekiq_options queue: :high_priority, retry: 10
def perform(reaction_id)
reaction = Reaction.find_by(id: reaction_id)
return unless reaction&.reactable
CacheBuster.bust reaction.user.path
if reaction.reactable_type == "Article"
CacheBuster.bust "/reactions?article_id=#{reaction.reactable_id}"
elsif reaction.reactable_type == "Comment"
path = "/reactions?commentable_id=#{reaction.reactable.commentable_id}&commentable_type=#{reaction.reactable.commentable_type}"
CacheBuster.bust(path)
end
end
end
end

View file

@ -118,14 +118,21 @@ RSpec.describe Reaction, type: :model do
expect do
reaction.save
end.to(
have_enqueued_job(Reactions::UpdateReactableJob).exactly(:once).
and(have_enqueued_job(Reactions::BustReactableCacheJob).exactly(:once)),
have_enqueued_job(Reactions::UpdateReactableJob).exactly(:once),
)
end
it "enqueues the correct workers" do
sidekiq_assert_enqueued_with(job: Reactions::BustHomepageCacheWorker) do
reaction.save
describe "enqueues the correct worker" do
it "BustReactableCacheWorker" do
sidekiq_assert_enqueued_with(job: Reactions::BustReactableCacheWorker) do
reaction.save
end
end
it "BustHomepageCacheWorker" do
sidekiq_assert_enqueued_with(job: Reactions::BustHomepageCacheWorker) do
reaction.save
end
end
end

View file

@ -0,0 +1,34 @@
require "rails_helper"
RSpec.describe Reactions::BustReactableCacheWorker, type: :worker do
describe "#perform" do
let(:user) { create(:user) }
let(:article) { create(:article) }
let(:reaction) { create(:reaction, reactable: article, user: user) }
let(:comment) { create(:comment, commentable: article) }
let(:comment_reaction) { create(:reaction, reactable: comment, user: user) }
let(:worker) { subject }
before do
allow(CacheBuster).to receive(:bust)
end
it "busts the reactable article cache" do
worker.perform(reaction.id)
expect(CacheBuster).to have_received(:bust).with(user.path).once
expect(CacheBuster).to have_received(:bust).with("/reactions?article_id=#{article.id}").once
end
it "busts the reactable comment cache" do
worker.perform(comment_reaction.id)
expect(CacheBuster).to have_received(:bust).with(user.path).once
expect(CacheBuster).to have_received(:bust).with("/reactions?commentable_id=#{article.id}&commentable_type=Article").once
end
it "doesn't fail if a reaction doesn't exist" do
expect do
worker.perform(Reaction.maximum(:id).to_i + 1)
end.not_to raise_error
end
end
end