From d0b4ba2648e1df8a572e00a34a876767e29431e9 Mon Sep 17 00:00:00 2001 From: Lucas Hiago Date: Wed, 12 Feb 2020 13:50:40 -0300 Subject: [PATCH] Change BustReactableCacheJob to BustReactableCacheWorker and move to sidekiq (#5582) [deploy] --- app/models/reaction.rb | 8 ++--- .../reactions/bust_reactable_cache_worker.rb | 20 +++++++++++ spec/models/reaction_spec.rb | 17 +++++++--- .../bust_reactable_cache_worker_spec.rb | 34 +++++++++++++++++++ 4 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 app/workers/reactions/bust_reactable_cache_worker.rb create mode 100644 spec/workers/reactions/bust_reactable_cache_worker_spec.rb diff --git a/app/models/reaction.rb b/app/models/reaction.rb index 2668e48bb..35284d162 100644 --- a/app/models/reaction.rb +++ b/app/models/reaction.rb @@ -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 diff --git a/app/workers/reactions/bust_reactable_cache_worker.rb b/app/workers/reactions/bust_reactable_cache_worker.rb new file mode 100644 index 000000000..d62042832 --- /dev/null +++ b/app/workers/reactions/bust_reactable_cache_worker.rb @@ -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 diff --git a/spec/models/reaction_spec.rb b/spec/models/reaction_spec.rb index c3935c364..e1843168d 100644 --- a/spec/models/reaction_spec.rb +++ b/spec/models/reaction_spec.rb @@ -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 diff --git a/spec/workers/reactions/bust_reactable_cache_worker_spec.rb b/spec/workers/reactions/bust_reactable_cache_worker_spec.rb new file mode 100644 index 000000000..84099786f --- /dev/null +++ b/spec/workers/reactions/bust_reactable_cache_worker_spec.rb @@ -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