Skip to content

Commit

Permalink
Merge pull request #4888 from sul-dlss/async_indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored Apr 19, 2024
2 parents bfc1f3f + fcf3e52 commit 66d3346
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 121 deletions.
9 changes: 3 additions & 6 deletions app/controllers/administrative_tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def create
})
render status: :conflict, plain: e.message
else
# Broadcast this update action to a topic so that it can be indexed
Notifications::ObjectUpdated.publish(model: @cocina_object)
Indexer.reindex_later(cocina_object: @cocina_object)
head :created
end

Expand All @@ -51,8 +50,7 @@ def update
})
render status: :conflict, plain: e.message
else
# Broadcast this update action to a topic so that it can be indexed
Notifications::ObjectUpdated.publish(model: @cocina_object)
Indexer.reindex_later(cocina_object: @cocina_object)
head :no_content
end

Expand All @@ -61,7 +59,6 @@ def destroy
rescue ActiveRecord::RecordNotFound => e
render status: :not_found, plain: e.message
else
# Broadcast this update action to a topic so that it can be indexed
Notifications::ObjectUpdated.publish(model: @cocina_object)
Indexer.reindex_later(cocina_object: @cocina_object)
end
end
2 changes: 1 addition & 1 deletion app/jobs/publish_items_modified_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PublishItemsModifiedJob < ApplicationJob
def perform(collection_identifier)
MemberService.for(collection_identifier).each do |druid|
cocina_object_with_metadata = CocinaObjectStore.find(druid)
Notifications::ObjectUpdated.publish(model: cocina_object_with_metadata)
Indexer.reindex_later(cocina_object: cocina_object_with_metadata)
end
end
end
18 changes: 18 additions & 0 deletions app/jobs/reindex_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# Reindexes an object.
class ReindexJob < ApplicationJob
queue_as :default

# @param [Hash] model the cocina object attributes (without metadata)
# @param [DateTime] created the time the object was created
# @param [DateTime] modified the time the object was last modified
def perform(model:, created:, modified:)
cocina_object = Cocina::Models.build(model)
cocina_object_with_metadata = Cocina::Models.with_metadata(cocina_object, 'void', created:, modified:)
Indexer.reindex(cocina_object: cocina_object_with_metadata)
rescue DorIndexing::RepositoryError => e
Rails.logger.error("Error reindexing #{model[:externalIdentifier]}: #{e.message}")
Honeybadger.notify(e, context: { druid: model[:externalIdentifier] })
end
end
9 changes: 9 additions & 0 deletions app/services/indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def self.delete(druid:)
solr.commit
end

# @param [Cocina::Models::DROWithMetadata|CollectionWithMetadata|AdminPolicyWithMetadata]
def self.reindex_later(cocina_object:)
ReindexJob.perform_later(
model: cocina_object.to_h,
created: cocina_object.created,
modified: cocina_object.modified
)
end

# Repository implementations backed by ActiveRecord
def self.administrative_tags_finder
lambda do |druid|
Expand Down
42 changes: 0 additions & 42 deletions app/services/notifications/object_updated.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/services/update_object_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def update

event_factory.create(druid:, event_type: 'update', data: { success: true, request: cocina_object_without_metadata.to_h })

# Broadcast this update action to a topic
Notifications::ObjectUpdated.publish(model: cocina_object_with_metadata)
Indexer.reindex_later(cocina_object: cocina_object_with_metadata)

# Update all items in the collection if necessary
PublishItemsModifiedJob.perform_later(druid) if update_items
Expand Down
4 changes: 2 additions & 2 deletions spec/jobs/publish_items_modified_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
before do
allow(MemberService).to receive(:for).and_return([{ 'id' => '123' }, { 'id' => '456' }])
allow(CocinaObjectStore).to receive(:find).and_return(instance_double(Cocina::Models::DRO), instance_double(Cocina::Models::DRO))
allow(Notifications::ObjectUpdated).to receive(:publish)
allow(Indexer).to receive(:reindex_later)
perform
end

it 'sends object updated notifications for each member' do
expect(Notifications::ObjectUpdated).to have_received(:publish).twice
expect(Indexer).to have_received(:reindex_later).twice
end
end
35 changes: 35 additions & 0 deletions spec/jobs/reindex_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ReindexJob do
subject(:perform) do
described_class.perform_now(model: dro.to_h, created: Time.zone.now, modified: Time.zone.now)
end

let(:dro) { build(:dro) }

context 'when no errors' do
before do
allow(Indexer).to receive(:reindex)
end

it 'invokes the Indexer' do
perform
expect(Indexer).to have_received(:reindex).with(cocina_object: an_instance_of(Cocina::Models::DROWithMetadata))
end
end

context 'when an error' do
before do
allow(Indexer).to receive(:reindex).and_raise(DorIndexing::RepositoryError)
allow(Honeybadger).to receive(:notify)
end

it 'Honeybadger alerts' do
perform
expect(Indexer).to have_received(:reindex).with(cocina_object: an_instance_of(Cocina::Models::DROWithMetadata))
expect(Honeybadger).to have_received(:notify)
end
end
end
16 changes: 8 additions & 8 deletions spec/requests/administrative_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
describe '#create' do
before do
allow(AdministrativeTags).to receive(:create)
allow(Notifications::ObjectUpdated).to receive(:publish)
allow(Indexer).to receive(:reindex_later)
end

context 'when happy path (without replacement)' do
Expand All @@ -45,7 +45,7 @@
headers: { 'Authorization' => "Bearer #{jwt}", 'Content-Type' => 'application/json' }
expect(AdministrativeTags).to have_received(:create)
.with(identifier: druid, tags:, replace: nil)
expect(Notifications::ObjectUpdated).to have_received(:publish)
expect(Indexer).to have_received(:reindex_later)
expect(response).to have_http_status(:created)
end
end
Expand All @@ -57,7 +57,7 @@
headers: { 'Authorization' => "Bearer #{jwt}", 'Content-Type' => 'application/json' }
expect(AdministrativeTags).to have_received(:create)
.with(identifier: druid, tags:, replace: true)
expect(Notifications::ObjectUpdated).to have_received(:publish)
expect(Indexer).to have_received(:reindex_later)
expect(response).to have_http_status(:created)
end
end
Expand All @@ -74,7 +74,7 @@
headers: { 'Authorization' => "Bearer #{jwt}", 'Content-Type' => 'application/json' }
expect(response).to have_http_status(:not_found)
expect(response.body).to eq('Unable to find \'druid:mx123qw2323\' in repository. See logger for details.')
expect(Notifications::ObjectUpdated).not_to have_received(:publish)
expect(Indexer).not_to have_received(:reindex_later)
end
end

Expand Down Expand Up @@ -119,7 +119,7 @@

before do
allow(AdministrativeTags).to receive(:update)
allow(Notifications::ObjectUpdated).to receive(:publish)
allow(Indexer).to receive(:reindex_later)
end

context 'when happy path' do
Expand All @@ -129,7 +129,7 @@
headers: { 'Authorization' => "Bearer #{jwt}", 'Content-Type' => 'application/json' }
expect(AdministrativeTags).to have_received(:update)
.with(identifier: druid, current: current_tag, new: new_tag)
expect(Notifications::ObjectUpdated).to have_received(:publish)
expect(Indexer).to have_received(:reindex_later)
expect(response).to have_http_status(:no_content)
end
end
Expand Down Expand Up @@ -253,7 +253,7 @@ def full_messages

before do
allow(AdministrativeTags).to receive(:destroy)
allow(Notifications::ObjectUpdated).to receive(:publish)
allow(Indexer).to receive(:reindex_later)
end

context 'when happy path' do
Expand All @@ -262,7 +262,7 @@ def full_messages
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(AdministrativeTags).to have_received(:destroy)
.with(identifier: druid, tag:)
expect(Notifications::ObjectUpdated).to have_received(:publish)
expect(Indexer).to have_received(:reindex_later)
expect(response).to have_http_status(:no_content)
end
end
Expand Down
17 changes: 16 additions & 1 deletion spec/services/indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe Indexer do
let(:cocina_object) { instance_double(Cocina::Models::DRO, externalIdentifier: druid) }
let(:cocina_object) { build(:dro_with_metadata, id: druid) }
let(:druid) { 'druid:bc123df4567' }

let(:solr_doc) { instance_double(Hash) }
Expand Down Expand Up @@ -40,6 +40,21 @@
end
end

describe '#reindex_later' do
before do
allow(ReindexJob).to receive(:perform_later)
end

it 'reindexes the object later' do
described_class.reindex_later(cocina_object:)
expect(ReindexJob).to have_received(:perform_later).with(
model: cocina_object.to_h,
created: cocina_object.created,
modified: cocina_object.modified
)
end
end

describe '#cocina_finder' do
before do
allow(CocinaObjectStore).to receive(:find).and_return(cocina_object)
Expand Down
59 changes: 0 additions & 59 deletions spec/services/notifications/object_updated_spec.rb

This file was deleted.

1 change: 1 addition & 0 deletions spec/services/update_object_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
before do
allow(Cocina::ObjectValidator).to receive(:validate)
allow(VersionService).to receive(:open?).and_return(open)
allow(Indexer).to receive(:reindex_later)
end

context 'when object is a DRO' do
Expand Down

0 comments on commit 66d3346

Please sign in to comment.