From da259107bd35ec0960fd8e33c9545c51bce4656d Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 25 Feb 2020 11:06:42 -0800 Subject: [PATCH] Refactor logic to index to Elasticsearch (#6286) [deploy] * Refactor logic to index to Elasticsearch * Rename worker argument to object_class * Change to .constantize for better error messaging --- app/models/chat_channel_membership.rb | 1 - app/models/classified_listing.rb | 1 - app/models/concerns/searchable.rb | 2 +- app/models/tag.rb | 1 - .../search/index_to_elasticsearch_worker.rb | 12 ++++++++++++ spec/models/chat_channel_membership_spec.rb | 4 ++-- spec/models/classified_listing_spec.rb | 4 ++-- spec/models/tag_spec.rb | 4 ++-- .../index_to_elasticsearch_worker_spec.rb | 19 +++++++++++++++++++ 9 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 app/workers/search/index_to_elasticsearch_worker.rb create mode 100644 spec/workers/search/index_to_elasticsearch_worker_spec.rb diff --git a/app/models/chat_channel_membership.rb b/app/models/chat_channel_membership.rb index 11c0967c0..67dadf2fe 100644 --- a/app/models/chat_channel_membership.rb +++ b/app/models/chat_channel_membership.rb @@ -2,7 +2,6 @@ class ChatChannelMembership < ApplicationRecord include AlgoliaSearch include Searchable - SEARCH_INDEX_WORKER = Search::ChatChannelMembershipEsIndexWorker SEARCH_SERIALIZER = Search::ChatChannelMembershipSerializer SEARCH_CLASS = Search::ChatChannelMembership diff --git a/app/models/classified_listing.rb b/app/models/classified_listing.rb index e8f0306e2..f0b258d22 100644 --- a/app/models/classified_listing.rb +++ b/app/models/classified_listing.rb @@ -2,7 +2,6 @@ class ClassifiedListing < ApplicationRecord include AlgoliaSearch include Searchable - SEARCH_INDEX_WORKER = Search::ClassifiedListingEsIndexWorker SEARCH_SERIALIZER = Search::ClassifiedListingSerializer SEARCH_CLASS = Search::ClassifiedListing diff --git a/app/models/concerns/searchable.rb b/app/models/concerns/searchable.rb index 4d0e07305..8155df73e 100644 --- a/app/models/concerns/searchable.rb +++ b/app/models/concerns/searchable.rb @@ -1,6 +1,6 @@ module Searchable def index_to_elasticsearch - self.class::SEARCH_INDEX_WORKER.perform_async(id) + Search::IndexToElasticsearchWorker.perform_async(self.class.name, id) end def index_to_elasticsearch_inline diff --git a/app/models/tag.rb b/app/models/tag.rb index 06b23d1f5..64456c6ad 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -31,7 +31,6 @@ class Tag < ActsAsTaggableOn::Tag before_save :mark_as_updated include Searchable - SEARCH_INDEX_WORKER = Search::TagEsIndexWorker SEARCH_SERIALIZER = Search::TagSerializer SEARCH_CLASS = Search::Tag diff --git a/app/workers/search/index_to_elasticsearch_worker.rb b/app/workers/search/index_to_elasticsearch_worker.rb new file mode 100644 index 000000000..2e0cc79b8 --- /dev/null +++ b/app/workers/search/index_to_elasticsearch_worker.rb @@ -0,0 +1,12 @@ +module Search + class IndexToElasticsearchWorker + include Sidekiq::Worker + + sidekiq_options queue: :high_priority + + def perform(object_class, id) + object = object_class.constantize.find(id) + object.index_to_elasticsearch_inline + end + end +end diff --git a/spec/models/chat_channel_membership_spec.rb b/spec/models/chat_channel_membership_spec.rb index edca39fbc..9044ab3ee 100644 --- a/spec/models/chat_channel_membership_spec.rb +++ b/spec/models/chat_channel_membership_spec.rb @@ -5,7 +5,7 @@ RSpec.describe ChatChannelMembership, type: :model do describe "#index_to_elasticsearch" do it "enqueues job to index tag to elasticsearch" do - sidekiq_assert_enqueued_with(job: described_class::SEARCH_INDEX_WORKER, args: [chat_channel_membership.id]) do + sidekiq_assert_enqueued_with(job: Search::IndexToElasticsearchWorker, args: [described_class.to_s, chat_channel_membership.id]) do chat_channel_membership.index_to_elasticsearch end end @@ -22,7 +22,7 @@ RSpec.describe ChatChannelMembership, type: :model do describe "#after_commit" do it "on update enqueues job to index chat_channel_membership to elasticsearch" do chat_channel_membership.save - sidekiq_assert_enqueued_with(job: described_class::SEARCH_INDEX_WORKER, args: [chat_channel_membership.id]) do + sidekiq_assert_enqueued_with(job: Search::IndexToElasticsearchWorker, args: [described_class.to_s, chat_channel_membership.id]) do chat_channel_membership.save end end diff --git a/spec/models/classified_listing_spec.rb b/spec/models/classified_listing_spec.rb index b7d706a61..b99aaad54 100644 --- a/spec/models/classified_listing_spec.rb +++ b/spec/models/classified_listing_spec.rb @@ -74,7 +74,7 @@ RSpec.describe ClassifiedListing, type: :model do describe "#index_to_elasticsearch" do it "enqueues job to index classified_listing to elasticsearch" do - sidekiq_assert_enqueued_with(job: described_class::SEARCH_INDEX_WORKER, args: [classified_listing.id]) do + sidekiq_assert_enqueued_with(job: Search::IndexToElasticsearchWorker, args: [described_class.to_s, classified_listing.id]) do classified_listing.index_to_elasticsearch end end @@ -92,7 +92,7 @@ RSpec.describe ClassifiedListing, type: :model do it "on update enqueues worker to index tag to elasticsearch" do classified_listing.save - sidekiq_assert_enqueued_with(job: described_class::SEARCH_INDEX_WORKER, args: [classified_listing.id]) do + sidekiq_assert_enqueued_with(job: Search::IndexToElasticsearchWorker, args: [described_class.to_s, classified_listing.id]) do classified_listing.save end end diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 2840839e1..965894d11 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -78,7 +78,7 @@ RSpec.describe Tag, type: :model do describe "#index_to_elasticsearch" do it "enqueues job to index tag to elasticsearch" do - sidekiq_assert_enqueued_with(job: described_class::SEARCH_INDEX_WORKER, args: [tag.id]) do + sidekiq_assert_enqueued_with(job: Search::IndexToElasticsearchWorker, args: [described_class.to_s, tag.id]) do tag.index_to_elasticsearch end end @@ -95,7 +95,7 @@ RSpec.describe Tag, type: :model do describe "#after_commit" do it "on update enqueues job to index tag to elasticsearch" do tag.save - sidekiq_assert_enqueued_with(job: described_class::SEARCH_INDEX_WORKER, args: [tag.id]) do + sidekiq_assert_enqueued_with(job: Search::IndexToElasticsearchWorker, args: [described_class.to_s, tag.id]) do tag.save end end diff --git a/spec/workers/search/index_to_elasticsearch_worker_spec.rb b/spec/workers/search/index_to_elasticsearch_worker_spec.rb new file mode 100644 index 000000000..b39edb04a --- /dev/null +++ b/spec/workers/search/index_to_elasticsearch_worker_spec.rb @@ -0,0 +1,19 @@ +require "rails_helper" + +RSpec.describe Search::IndexToElasticsearchWorker, type: :worker, elasticsearch: true do + let(:worker) { subject } + + include_examples "#enqueues_on_correct_queue", "high_priority", ["Tag", 1] + + it "raises an error if record is not found" do + expect { worker.perform("Tag", 1234) }.to raise_error(ActiveRecord::RecordNotFound) + end + + it "indexes document" do + tag = FactoryBot.create(:tag) + expect { tag.elasticsearch_doc }.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound) + worker.perform(tag.class.name, tag.id) + + expect(tag.elasticsearch_doc.dig("_source", "id")).to eql(tag.id) + end +end