Skip to content

Commit

Permalink
Reindex the work during derivative creation if it is the work's thumb…
Browse files Browse the repository at this point in the history
…nail
  • Loading branch information
dlpierce committed Aug 25, 2023
1 parent 168c3da commit 01e604b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
1 change: 1 addition & 0 deletions .koppie/config/initializers/hyrax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions app/jobs/valkyrie_create_derivatives_job.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# 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.
file = Hyrax.storage_adapter.find_by(id: file_metadata.file_identifier)
# 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
52 changes: 52 additions & 0 deletions spec/jobs/valkyrie_create_derivatives_job_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 01e604b

Please sign in to comment.