Skip to content

Commit

Permalink
Fix reindex progress bar. Closes #485.
Browse files Browse the repository at this point in the history
Co-authored-by: Anna Headley <anna.headley@gmail.com>
  • Loading branch information
eliotjordan and hackartisan committed Jan 10, 2019
1 parent 00e5d86 commit ce8d9fa
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
12 changes: 10 additions & 2 deletions app/jobs/iiif_ingest_job.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
class IIIFIngestJob < ActiveJob::Base
# Ingest one or more IIIF manfiest URLs. Each manifest is ingested as its
# own resource.
def perform(urls, exhibit)
def perform(urls, exhibit, log_entry)
Array.wrap(urls).each do |url|
ingest url, exhibit
end

# Lock the row to prevent multiple workers from overwriting each other's increments.
# This worked, but it's reporting complete before the objects are reindexed.
# app/models/spotlight/resource.rb:55 has a null reindexing_log_entry.
log_entry.with_lock do
previous = log_entry.items_reindexed_count || 0
log_entry.update(items_reindexed_count: previous + Array.wrap(urls).size)
end
end

# Ingest a single IIIF manifest URL as a resource.
def ingest(url, exhibit)
IIIFResource.find_or_initialize_by(url: url, exhibit_id: exhibit.id).save_and_index
IIIFResource.find_or_initialize_by(url: url, exhibit_id: exhibit.id).save_and_index_now
end
end
6 changes: 3 additions & 3 deletions app/models/exhibit_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ def initialize(exhibit)
@exhibit = exhibit
end

def reindex(*_args)
members.each_slice(50) do |slice|
IIIFIngestJob.perform_later slice, exhibit
def reindex(log_entry)
members.each do |member|
IIIFIngestJob.perform_later member, exhibit, log_entry
end
end

Expand Down
5 changes: 5 additions & 0 deletions app/models/iiif_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def noid
data["noid"]
end

def save_and_index_now(*args)
save(*args)
Spotlight::ReindexJob.perform_now(self)
end

private

def set_noid
Expand Down
18 changes: 15 additions & 3 deletions spec/jobs/iiif_ingest_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,36 @@
describe IIIFIngestJob do
let(:url1) { 'http://example.com/1/manifest' }
let(:url2) { 'http://example.com/2/manifest' }
let(:log_entry) { Spotlight::ReindexingLogEntry.new }
let(:exhibit) { Spotlight::Exhibit.new }
let(:resource) { IIIFResource.new url: nil, exhibit: exhibit }

before do
allow(exhibit).to receive(:id).and_return('exhibit1')
allow(resource).to receive(:save_and_index)
allow(resource).to receive(:save_and_index_now)
end

it 'ingests a single url' do
expect(IIIFResource).to receive(:new).with(url: url1, exhibit_id: exhibit.id).and_return(resource)

described_class.new.perform(url1, exhibit)
described_class.new.perform(url1, exhibit, log_entry)
end

it 'ingests each of an array of urls' do
expect(IIIFResource).to receive(:new).with(url: url1, exhibit_id: exhibit.id).and_return(resource)
expect(IIIFResource).to receive(:new).with(url: url2, exhibit_id: exhibit.id).and_return(resource)

described_class.new.perform([url1, url2], exhibit)
described_class.new.perform([url1, url2], exhibit, log_entry)
end

describe 'incrementing the log entry' do
before do
allow(IIIFResource).to receive(:new).and_return(resource)
end

it 'increments the exhibit log entry' do
described_class.new.perform([url1, url2], exhibit, log_entry)
expect(log_entry.items_reindexed_count).to eq 2
end
end
end
15 changes: 15 additions & 0 deletions spec/models/iiif_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,20 @@
expect { resource.save }.to raise_error(IIIFResource::InvalidIIIFManifestError, "Invalid Collection metadata found in the IIIF Manifest: #{url}")
end
end

describe '#save_and_index_now' do
let(:exhibit) { Spotlight::Exhibit.create title: 'Exhibit A' }
let(:resource) { described_class.new url: url, exhibit: exhibit }

before do
allow(Spotlight::ReindexJob).to receive(:perform_now)
allow(resource).to receive(:save)
end

it 'calls perform now on Spotlight::ReindexJob' do
resource.save_and_index_now
expect(Spotlight::ReindexJob).to have_received(:perform_now)
end
end
end
end

0 comments on commit ce8d9fa

Please sign in to comment.