Skip to content

Commit

Permalink
wire up reindexing job logging
Browse files Browse the repository at this point in the history
* add backwards compatible log updating to ReindexJob
* use new logging functionality in Exhibit#reindex_later with optional user, update ResourceController#reindex_all to pass user into @exhibit.reindex_later
* tests
  * rubocop RSpec/SubjectStub exception for reindex_job_spec.rb
  • Loading branch information
jmartin-sul committed Jan 14, 2017
1 parent ebf78ed commit 1adb393
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ RSpec/NotToNot:
# Offense count: 42
RSpec/SubjectStub:
Exclude:
- 'spec/jobs/spotlight/reindex_job_spec.rb'
- 'spec/lib/spotlight/controller_spec.rb'
- 'spec/models/solr_document_spec.rb'
- 'spec/models/spotlight/access_controls_enforcement_search_builder_spec.rb'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/spotlight/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def monitor
end

def reindex_all
@exhibit.reindex_later
@exhibit.reindex_later current_user

redirect_to admin_exhibit_catalog_path(@exhibit), notice: t(:'spotlight.resources.reindexing_in_progress')
end
Expand Down
19 changes: 18 additions & 1 deletion app/jobs/spotlight/reindex_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ class ReindexJob < ActiveJob::Base
resource_list(job.arguments.first).each(&:waiting!)
end

def perform(exhibit_or_resources)
around_perform do |job, block|
log_entry(job).reindexing_started! if log_entry(job)

begin
block.call
rescue
log_entry(job).reindexing_failed! if log_entry(job)
raise
end

log_entry(job).reindexing_succeeded! if log_entry(job)
end

def perform(exhibit_or_resources, _log_entry = nil)
resource_list(exhibit_or_resources).each(&:reindex)
end

Expand All @@ -23,5 +36,9 @@ def resource_list(exhibit_or_resources)
Array(exhibit_or_resources)
end
end

def log_entry(job)
job.arguments.second if job.arguments.second.is_a?(Spotlight::ReindexingLogEntry)
end
end
end
8 changes: 6 additions & 2 deletions app/models/spotlight/exhibit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def solr_data
end
end

def reindex_later
Spotlight::ReindexJob.perform_later(self)
def reindex_later(user = nil)
Spotlight::ReindexJob.perform_later(self, new_reindexing_log_entry(user))
end

def uploaded_resource_fields
Expand Down Expand Up @@ -109,5 +109,9 @@ def reindex_progress
def sanitize_description
self.description = ::Rails::Html::FullSanitizer.new.sanitize(description)
end

def new_reindexing_log_entry(user = nil)
Spotlight::ReindexingLogEntry.create(exhibit: self, user: user, items_reindexed_count: resources.size)
end
end
end
25 changes: 25 additions & 0 deletions spec/jobs/spotlight/reindex_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
describe Spotlight::ReindexJob do
let(:exhibit) { FactoryGirl.create(:exhibit) }
let(:resource) { FactoryGirl.create(:resource) }
let(:user) { FactoryGirl.create(:user) }
let(:log_entry) { Spotlight::ReindexingLogEntry.create(exhibit: exhibit, user: user) }

before do
allow_any_instance_of(Spotlight::Resource).to receive(:reindex)
Expand All @@ -14,6 +16,7 @@
exhibit.resources << resource
exhibit.save
end

it 'attempts to reindex every resource in the exhibit' do
# ActiveJob will reload the collection, so we go through a little trouble:
expect_any_instance_of(Spotlight::Resource).to receive(:reindex) do |thingy|
Expand All @@ -22,6 +25,28 @@

subject.perform_now
end

context 'with a log_entry' do
subject { described_class.new(exhibit, log_entry) }

it 'marks the log entry as started' do
expect(log_entry).to receive(:reindexing_started!)
subject.perform_now
end

it 'marks the log entry as successful if there is no error' do
expect(log_entry).to receive(:reindexing_succeeded!)
subject.perform_now
end

it 'marks the log entry as failed if there is an error' do
unexpected_error = StandardError.new
# it'd be more realistic to raise on resource#reindex, but that's already stubbed above, so this'll have to do
expect(subject).to receive(:perform).with(exhibit, log_entry).and_raise unexpected_error
expect(log_entry).to receive(:reindexing_failed!)
expect { subject.perform_now }.to raise_error unexpected_error
end
end
end

context 'with a resource' do
Expand Down
24 changes: 21 additions & 3 deletions spec/models/spotlight/exhibit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,28 @@

describe '#reindex_later' do
subject { FactoryGirl.create(:exhibit) }
let(:log_entry) { Spotlight::ReindexingLogEntry.new(exhibit: subject, user: user, items_reindexed_count: subject.resources.size) }

it 'queues a reindex job for the exhibit' do
expect(Spotlight::ReindexJob).to receive(:perform_later).with(subject)
subject.reindex_later
context 'user is omitted' do
let(:user) { nil }

it 'queues a reindex job for the exhibit, with nil user for the log entry' do
expect(subject).to receive(:new_reindexing_log_entry).with(nil).and_return(log_entry)
expect(Spotlight::ReindexJob).to receive(:perform_later).with(subject, log_entry)
subject.reindex_later
expect(log_entry.user).to be nil
end
end

context 'non-nil user is provided' do
let(:user) { FactoryGirl.build(:user) }

it 'queues a reindex job for the exhibit, with actual user for the log entry' do
expect(subject).to receive(:new_reindexing_log_entry).with(user).and_return(log_entry)
expect(Spotlight::ReindexJob).to receive(:perform_later).with(subject, log_entry)
subject.reindex_later user
expect(log_entry.user).to eq user
end
end
end

Expand Down

0 comments on commit 1adb393

Please sign in to comment.