diff --git a/app/jobs/iiif_ingest_job.rb b/app/jobs/iiif_ingest_job.rb index 693280db..5ad42374 100644 --- a/app/jobs/iiif_ingest_job.rb +++ b/app/jobs/iiif_ingest_job.rb @@ -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 diff --git a/app/models/exhibit_proxy.rb b/app/models/exhibit_proxy.rb index 1c6b95ed..c963c9f4 100644 --- a/app/models/exhibit_proxy.rb +++ b/app/models/exhibit_proxy.rb @@ -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 diff --git a/app/models/iiif_resource.rb b/app/models/iiif_resource.rb index 759040c0..ed4b8827 100644 --- a/app/models/iiif_resource.rb +++ b/app/models/iiif_resource.rb @@ -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 diff --git a/spec/jobs/iiif_ingest_job_spec.rb b/spec/jobs/iiif_ingest_job_spec.rb index d48fbd1e..3584b2a5 100644 --- a/spec/jobs/iiif_ingest_job_spec.rb +++ b/spec/jobs/iiif_ingest_job_spec.rb @@ -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 diff --git a/spec/models/iiif_resource_spec.rb b/spec/models/iiif_resource_spec.rb index 43cfd4da..376c3c02 100644 --- a/spec/models/iiif_resource_spec.rb +++ b/spec/models/iiif_resource_spec.rb @@ -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