Move Search::IndexJob to Sidekiq (#5620) [deploy]

This commit is contained in:
Alex 2020-01-22 05:24:53 -08:00 committed by Molly Struve
parent 8cc791150c
commit 4e5dc015b3
10 changed files with 69 additions and 15 deletions

View file

@ -265,7 +265,7 @@ class Article < ApplicationRecord
return if remove
if record.published && record.tag_list.exclude?("hiring")
Search::IndexJob.perform_later("Article", record.id)
Search::IndexWorker.perform_async("Article", record.id)
else
Search::RemoveFromIndexJob.perform_later(Article.algolia_index_name, record.id)
Search::RemoveFromIndexJob.perform_later("searchables_#{Rails.env}", record.index_id)

View file

@ -115,7 +115,7 @@ class Comment < ApplicationRecord
return if remove
if record.deleted == false
Search::IndexJob.perform_later("Comment", record.id)
Search::IndexWorker.perform_async("Comment", record.id)
else
Search::RemoveFromIndexJob.perform_later(Comment.algolia_index_name, record.index_id)
end

View file

@ -50,7 +50,7 @@ class PageView < ApplicationRecord
if remove
Search::RemoveFromIndexJob.perform_later(algolia_index_name, record.id)
else
Search::IndexJob.perform_later("PageView", record.id)
Search::IndexWorker.perform_async("PageView", record.id)
end
end

View file

@ -205,7 +205,7 @@ class User < ApplicationRecord
def self.trigger_delayed_index(record, remove)
return if remove
Search::IndexJob.perform_later("User", record.id)
Search::IndexWorker.perform_async("User", record.id)
end
def tag_line

View file

@ -0,0 +1,22 @@
module Search
class IndexWorker
include Sidekiq::Worker
sidekiq_options queue: :medium_priority, retry: 10
VALID_RECORD_TYPES = %w[Comment Article User PageView].freeze
def perform(record_type, record_id)
unless VALID_RECORD_TYPES.include?(record_type)
raise InvalidRecordType, "Invalid class: #{record_type}. Valid and indexable classes are #{VALID_RECORD_TYPES.join(', ')}"
end
record = record_type.constantize.find_by(id: record_id)
return unless record
record.index!
end
end
class InvalidRecordType < StandardError; end
end

View file

@ -483,9 +483,9 @@ RSpec.describe Article, type: :model do
end
it "indexes published non-hiring article" do
expect do
sidekiq_assert_enqueued_with(job: Search::IndexWorker, args: ["Article", article.id]) do
article.update(published: false)
end.to have_enqueued_job(Search::IndexJob).exactly(:once).with("Article", article.id)
end
end
it "triggers auto removal from index on destroy" do

View file

@ -339,8 +339,13 @@ RSpec.describe Comment, type: :model do
end
it "busts the comment cache" do
expect_any_instance_of(Comments::BustCacheWorker).to receive(:perform).with(comment.id)
comment.destroy
# here the comment is destroyed from the test above so we re-create one
new_comment = create(:comment, commentable: article)
# this replaces the use of expect_any_instance_of which is a RuboCop violation
sidekiq_assert_enqueued_with(job: Comments::BustCacheWorker, args: [new_comment.id]) do
new_comment.destroy
end
end
end
@ -355,9 +360,9 @@ RSpec.describe Comment, type: :model do
context "when deleted is false" do
it "checks auto-indexing" do
expect do
sidekiq_assert_enqueued_with(job: Search::IndexWorker, args: ["Comment", comment.id]) do
comment.update(body_markdown: "hello")
end.to have_enqueued_job(Search::IndexJob).with("Comment", comment.id)
end
end
end

View file

@ -22,9 +22,9 @@ RSpec.describe PageView, type: :model do
describe "indexing" do
it "indexes updated records" do
expect do
sidekiq_assert_enqueued_with(job: Search::IndexWorker, args: ["PageView", page_view.id]) do
page_view.update(path: "/")
end.to have_enqueued_job(Search::IndexJob).exactly(:once).with("PageView", page_view.id)
end
end
it "removes deleted records" do

View file

@ -620,17 +620,21 @@ RSpec.describe User, type: :model do
context "when indexing and deindexing" do
it "triggers background auto-indexing when user is saved" do
expect { user.save }.to have_enqueued_job(Search::IndexJob).with("User", user.id)
sidekiq_assert_enqueued_with(job: Search::IndexWorker, args: ["User", user.id]) do
user.save
end
end
it "doesn't enqueue a job on destroy" do
user = build(:user)
perform_enqueued_jobs do
sidekiq_perform_enqueued_jobs do
user.save
end
expect { user.destroy }.not_to have_enqueued_job(Search::IndexJob)
sidekiq_assert_no_enqueued_jobs(only: Search::IndexWorker) do
user.destroy
end
end
end

View file

@ -0,0 +1,23 @@
require "rails_helper"
RSpec.describe Search::IndexWorker, type: :worker do
let(:worker) { subject }
include_examples "#enqueues_on_correct_queue", "medium_priority", ["User", 1]
it "does nothing if there is wrong record type is passed" do
expect { worker.perform("SuperUser", 1) }.to raise_error(Search::InvalidRecordType)
end
it "doesn't fail if a record is not found" do
expect { worker.perform("Comment", -1) }.not_to raise_error
end
it "indexes a record if everything is fine" do
user = double
allow(user).to receive(:index!)
allow(User).to receive(:find_by).and_return(user)
worker.perform("User", 1)
expect(user).to have_received(:index!)
end
end