[deploy] Remove Algolia from Reaction and Algolia Workers (#7571)

This commit is contained in:
Molly Struve 2020-04-28 15:10:56 -05:00 committed by GitHub
parent 131d11aa73
commit 566a3e30e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 0 additions and 115 deletions

View file

@ -1,5 +1,4 @@
class Reaction < ApplicationRecord
include AlgoliaSearch
include Searchable
SEARCH_SERIALIZER = Search::ReactionSerializer
@ -36,28 +35,10 @@ class Reaction < ApplicationRecord
after_commit :bust_reactable_cache, :update_reactable, on: %i[create update]
after_commit :index_to_elasticsearch, if: :indexable?, on: %i[create update]
after_commit :remove_from_elasticsearch, if: :indexable?, on: [:destroy]
after_save :index_to_algolia
after_save :touch_user
before_destroy :update_reactable_without_delay, unless: :destroyed_by_association
before_destroy :bust_reactable_cache_without_delay
before_destroy :remove_algolia
algoliasearch index_name: "SecuredReactions_#{Rails.env}", auto_index: false, auto_remove: false do
attribute :id, :reactable_user, :searchable_reactable_title, :searchable_reactable_path, :status, :reading_time,
:searchable_reactable_text, :searchable_reactable_tags, :viewable_by, :reactable_tags, :reactable_published_date
searchableAttributes %i[searchable_reactable_title searchable_reactable_text
searchable_reactable_tags reactable_user]
tags do
reactable_tags
end
attributesForFaceting ["filterOnly(viewable_by)", "filterOnly(status)"]
customRanking ["desc(id)"]
end
def index_to_algolia
index! if category == "readinglist" && reactable && reactable.published
end
class << self
def count_for_article(id)
@ -144,10 +125,6 @@ class Reaction < ApplicationRecord
reactable.reading_time if category == "readinglist"
end
def remove_from_index
remove_from_index!
end
def reactable_user
return unless category == "readinglist"
@ -211,10 +188,6 @@ class Reaction < ApplicationRecord
negative? && !user.trusted
end
def remove_algolia
remove_from_index!
end
def record_field_test_event
Users::RecordFieldTestEventWorker.perform_async(user_id, :user_home_feed, "user_creates_reaction")
end

View file

@ -1,22 +0,0 @@
module Search
class IndexWorker
include Sidekiq::Worker
sidekiq_options queue: :medium_priority, retry: 10
VALID_RECORD_TYPES = %w[Article User].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

@ -1,11 +0,0 @@
module Search
class RemoveFromIndexWorker
include Sidekiq::Worker
sidekiq_options queue: :medium_priority, retry: 10
def perform(index, key)
Algolia::Index.new(index).delete_object(key)
end
end
end

View file

@ -1,23 +0,0 @@
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("User", -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

View file

@ -1,32 +0,0 @@
require "rails_helper"
RSpec.describe Search::RemoveFromIndexWorker, type: :worker do
let(:worker) { subject }
include_examples "#enqueues_on_correct_queue", "medium_priority", ["searchables_#{Rails.env}", "users-456"]
describe "#perform" do
let(:algolia_index) { instance_double(Algolia::Index) }
before do
allow(Algolia::Index).to receive(:new).and_return(algolia_index)
allow(algolia_index).to receive(:delete_object)
end
it "calls the service" do
worker.perform("searchables_#{Rails.env}", "users-456")
expect(algolia_index).to have_received(:delete_object).with("users-456").once
end
it "doesn't raise an error if key is missing" do
# key is nil
expect { worker.perform("searchables_#{Rails.env}", nil) }.not_to raise_error
end
it "doesn't raise an error if index is missing" do
# index is nil
expect { worker.perform(nil, "users-456") }.not_to raise_error
end
end
end