From 01e604beb84abff73aae2e5983670ed9c84e1945 Mon Sep 17 00:00:00 2001 From: Daniel Pierce Date: Thu, 24 Aug 2023 16:38:00 -0400 Subject: [PATCH] Reindex the work during derivative creation if it is the work's thumbnail --- .koppie/config/initializers/hyrax.rb | 1 + app/jobs/valkyrie_create_derivatives_job.rb | 15 +++--- .../valkyrie_create_derivatives_job_spec.rb | 52 +++++++++++++++++++ 3 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 spec/jobs/valkyrie_create_derivatives_job_spec.rb diff --git a/.koppie/config/initializers/hyrax.rb b/.koppie/config/initializers/hyrax.rb index 281008ce1c..3e63d4128f 100644 --- a/.koppie/config/initializers/hyrax.rb +++ b/.koppie/config/initializers/hyrax.rb @@ -322,6 +322,7 @@ Hyrax::CustomQueries::Navigators::ParentCollectionsNavigator, Hyrax::CustomQueries::Navigators::ChildFileSetsNavigator, Hyrax::CustomQueries::Navigators::ChildWorksNavigator, + Hyrax::CustomQueries::Navigators::ParentWorkNavigator, Hyrax::CustomQueries::Navigators::FindFiles, Hyrax::CustomQueries::FindAccessControl, Hyrax::CustomQueries::FindCollectionsByType, diff --git a/app/jobs/valkyrie_create_derivatives_job.rb b/app/jobs/valkyrie_create_derivatives_job.rb index d41686d61d..8bc078709e 100644 --- a/app/jobs/valkyrie_create_derivatives_job.rb +++ b/app/jobs/valkyrie_create_derivatives_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class ValkyrieCreateDerivativesJob < Hyrax::ApplicationJob queue_as Hyrax.config.ingest_queue_name - def perform(_file_set_id, file_id, _filepath = nil) + def perform(file_set_id, file_id, _filepath = nil) file_metadata = Hyrax.custom_queries.find_file_metadata_by(id: file_id) return if file_metadata.video? && !Hyrax.config.enable_ffmpeg # Get file into a local path. @@ -9,15 +9,16 @@ def perform(_file_set_id, file_id, _filepath = nil) # Call derivatives with the file_set. derivative_service = Hyrax::DerivativeService.for(file_metadata) derivative_service.create_derivatives(file.disk_path) + reindex_parent(file_set_id) end private - def query_service - Hyrax.query_service - end - - def storage_adapter - Hyrax.storage_adapter + def reindex_parent(file_set_id) + file_set = Hyrax.query_service.find_by(id: file_set_id) + parent = Hyrax.custom_queries.find_parent_work(resource: file_set) + return unless parent.thumbnail_id == file_set.id + Hyrax.logger.debug { "Reindexing #{parent.id} due to creation of thumbnail derivatives." } + Hyrax.index_adapter.save(resource: parent) end end diff --git a/spec/jobs/valkyrie_create_derivatives_job_spec.rb b/spec/jobs/valkyrie_create_derivatives_job_spec.rb new file mode 100644 index 0000000000..ad8791428a --- /dev/null +++ b/spec/jobs/valkyrie_create_derivatives_job_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'hyrax/specs/spy_listener' + +RSpec.describe ValkyrieCreateDerivativesJob, perform_enqueued: true do + # Create a work with two files, the first will be the work's thumbnail + let(:work) { FactoryBot.valkyrie_create(:hyrax_work, :with_member_file_sets, :with_thumbnail) } + let(:upload_0) { FactoryBot.create(:uploaded_file, file_set_uri: file_sets[0].id, file: File.open('spec/fixtures/image.png')) } + let(:upload_1) { FactoryBot.create(:uploaded_file, file_set_uri: file_sets[1].id, file: File.open('spec/fixtures/world.png')) } + let(:file_id_0) { file_sets[0].file_ids.first } + let(:file_id_1) { file_sets[1].file_ids.first } + + let(:characterizer) { double(characterize: fits_response) } + let(:fits_response) { IO.read('spec/fixtures/png_fits.xml') } + + let(:derivatives_service) { instance_double("Hyrax::FileSetDerivativeService") } + + def file_sets + Hyrax.custom_queries.find_child_file_sets(resource: work).to_a + end + + before do + # stub out characterization to avoid system calls. It's important some + # amount of characterization happens so listeners fire. + allow(Hydra::FileCharacterization).to receive(:characterize).and_return(fits_response) + + # This job depends on routes existing for the work. SimpleWork doesn't here, so skip it. + allow(ContentUpdateEventJob).to receive(:perform_later) + + # Using ValkyrieIngestJob here is slow and runs the job this spec is supposed to test. + # It should instead be handled by factories once real files can be attached + # in the Hyrax::Work and Hyrax::FileSet factories. + ValkyrieIngestJob.perform_now(upload_0) + ValkyrieIngestJob.perform_now(upload_1) + end + + describe '.perform_now' do + context 'with files including the work thumbnail' do + before do + allow(Hyrax::DerivativeService).to receive(:for).and_return derivatives_service + allow(Hyrax.index_adapter).to receive(:save).and_call_original + end + + it 'creates derivatives and reindexes the work once' do + expect(derivatives_service).to receive(:create_derivatives).twice + expect(Hyrax.index_adapter).to receive(:save).with(resource: work).once + described_class.perform_now(file_sets[1].id, file_id_1) + described_class.perform_now(file_sets[0].id, file_id_0) + end + end + end +end