Move Search::IndexJob to Sidekiq (#5620) [deploy]
This commit is contained in:
parent
8cc791150c
commit
4e5dc015b3
10 changed files with 69 additions and 15 deletions
|
|
@ -265,7 +265,7 @@ class Article < ApplicationRecord
|
||||||
return if remove
|
return if remove
|
||||||
|
|
||||||
if record.published && record.tag_list.exclude?("hiring")
|
if record.published && record.tag_list.exclude?("hiring")
|
||||||
Search::IndexJob.perform_later("Article", record.id)
|
Search::IndexWorker.perform_async("Article", record.id)
|
||||||
else
|
else
|
||||||
Search::RemoveFromIndexJob.perform_later(Article.algolia_index_name, record.id)
|
Search::RemoveFromIndexJob.perform_later(Article.algolia_index_name, record.id)
|
||||||
Search::RemoveFromIndexJob.perform_later("searchables_#{Rails.env}", record.index_id)
|
Search::RemoveFromIndexJob.perform_later("searchables_#{Rails.env}", record.index_id)
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ class Comment < ApplicationRecord
|
||||||
return if remove
|
return if remove
|
||||||
|
|
||||||
if record.deleted == false
|
if record.deleted == false
|
||||||
Search::IndexJob.perform_later("Comment", record.id)
|
Search::IndexWorker.perform_async("Comment", record.id)
|
||||||
else
|
else
|
||||||
Search::RemoveFromIndexJob.perform_later(Comment.algolia_index_name, record.index_id)
|
Search::RemoveFromIndexJob.perform_later(Comment.algolia_index_name, record.index_id)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ class PageView < ApplicationRecord
|
||||||
if remove
|
if remove
|
||||||
Search::RemoveFromIndexJob.perform_later(algolia_index_name, record.id)
|
Search::RemoveFromIndexJob.perform_later(algolia_index_name, record.id)
|
||||||
else
|
else
|
||||||
Search::IndexJob.perform_later("PageView", record.id)
|
Search::IndexWorker.perform_async("PageView", record.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -205,7 +205,7 @@ class User < ApplicationRecord
|
||||||
def self.trigger_delayed_index(record, remove)
|
def self.trigger_delayed_index(record, remove)
|
||||||
return if remove
|
return if remove
|
||||||
|
|
||||||
Search::IndexJob.perform_later("User", record.id)
|
Search::IndexWorker.perform_async("User", record.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_line
|
def tag_line
|
||||||
|
|
|
||||||
22
app/workers/search/index_worker.rb
Normal file
22
app/workers/search/index_worker.rb
Normal 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
|
||||||
|
|
@ -483,9 +483,9 @@ RSpec.describe Article, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "indexes published non-hiring article" do
|
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)
|
article.update(published: false)
|
||||||
end.to have_enqueued_job(Search::IndexJob).exactly(:once).with("Article", article.id)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "triggers auto removal from index on destroy" do
|
it "triggers auto removal from index on destroy" do
|
||||||
|
|
|
||||||
|
|
@ -339,8 +339,13 @@ RSpec.describe Comment, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "busts the comment cache" do
|
it "busts the comment cache" do
|
||||||
expect_any_instance_of(Comments::BustCacheWorker).to receive(:perform).with(comment.id)
|
# here the comment is destroyed from the test above so we re-create one
|
||||||
comment.destroy
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -355,9 +360,9 @@ RSpec.describe Comment, type: :model do
|
||||||
|
|
||||||
context "when deleted is false" do
|
context "when deleted is false" do
|
||||||
it "checks auto-indexing" 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")
|
comment.update(body_markdown: "hello")
|
||||||
end.to have_enqueued_job(Search::IndexJob).with("Comment", comment.id)
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,9 @@ RSpec.describe PageView, type: :model do
|
||||||
|
|
||||||
describe "indexing" do
|
describe "indexing" do
|
||||||
it "indexes updated records" 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: "/")
|
page_view.update(path: "/")
|
||||||
end.to have_enqueued_job(Search::IndexJob).exactly(:once).with("PageView", page_view.id)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "removes deleted records" do
|
it "removes deleted records" do
|
||||||
|
|
|
||||||
|
|
@ -620,17 +620,21 @@ RSpec.describe User, type: :model do
|
||||||
|
|
||||||
context "when indexing and deindexing" do
|
context "when indexing and deindexing" do
|
||||||
it "triggers background auto-indexing when user is saved" 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
|
end
|
||||||
|
|
||||||
it "doesn't enqueue a job on destroy" do
|
it "doesn't enqueue a job on destroy" do
|
||||||
user = build(:user)
|
user = build(:user)
|
||||||
|
|
||||||
perform_enqueued_jobs do
|
sidekiq_perform_enqueued_jobs do
|
||||||
user.save
|
user.save
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
23
spec/workers/search/index_worker_spec.rb
Normal file
23
spec/workers/search/index_worker_spec.rb
Normal 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
|
||||||
Loading…
Add table
Reference in a new issue