Skip to content

Commit

Permalink
Cal 949 catch tombstone exceptions on re ingest2 (#857)
Browse files Browse the repository at this point in the history
* removed debug code

* fixed test that broke

* more fixes

* refactor to use exception and rescue when removing tombstones

* added Ldp::Gone for collections

* fixed collection tombstone detect and delete

* adjusted spacing

* expanded name, added comment for clarification of operation

Co-authored-by: darrowcoucla <[email protected]>
  • Loading branch information
Darrow Cole and darrowcousc authored Nov 24, 2020
1 parent 3b96f94 commit 007d758
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
37 changes: 19 additions & 18 deletions app/importers/actor_record_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,46 +60,47 @@ def update_for(existing_record:, update_record:)
# We assume the object was created as expected if the actor stack returns true.
def create_for(record:)
info_stream << "event: record_import_started, row_id: #{@row_id}, ark: #{record.ark}\n"

raise(ArgumentError, 'Title starts "DUPLICATE" – record will not be imported.') if record.mapper.title[0].to_s.start_with?('DUPLICATE')

additional_attrs = {
uploaded_files: create_upload_files(record),
depositor: @depositor.user_key
}

object_type = record.mapper.metadata["Object Type"]
created = import_type(object_type).new
created.apply_depositor_metadata(@depositor.user_key)
attrs = record.attributes.merge(additional_attrs)

attrs = attrs.merge(member_of_collections_attributes: { '0' => { id: collection_id } }) if collection_id

# Ensure nothing is passed in the files field,
# since this is reserved for Hyrax and is where uploaded_files will be attached
attrs.delete(:files)
attrs.delete(:uploaded_files)

based_near = attrs.delete(:based_near)
attrs = attrs.merge(based_near_attributes: based_near_attributes(based_near)) unless based_near.nil? || based_near.empty?

actor_env = Hyrax::Actors::Environment.new(created,
::Ability.new(@depositor),
attrs)
terminator = Hyrax::Actors::Terminator.new
middleware = Californica::IngestMiddlewareStack.build_stack.build(terminator)

if middleware.create(actor_env)
info_stream << "event: record_created, row_id: #{@row_id}, record_id: #{created.id}, ark: #{created.ark}\n"
else
error_messages = []
created.errors.each do |attr, msg|
error_stream << "event: validation_failed, row_id: #{@row_id}, attribute: #{attr.capitalize}, message: #{msg}, ark: #{attrs[:ark] ? attrs[:ark] : attrs}\n"
error_messages << msg
begin
retries ||= 0
middleware = Californica::IngestMiddlewareStack.build_stack.build(terminator)
if middleware.create(actor_env)
info_stream << "event: record_created, row_id: #{@row_id}, record_id: #{created.id}, ark: #{created.ark}\n"
else
error_messages = []
created.errors.each do |attr, msg|
error_stream << "event: validation_failed, row_id: #{@row_id}, attribute: #{attr.capitalize}, message: #{msg}, ark: #{attrs[:ark] ? attrs[:ark] : attrs}\n"
error_messages << msg
end
# Errors raised here should be rescued in the CsvRowImportJob and the
# message should be recorded on the CsvRow object for reporting in the UI
raise "Validation failed: #{error_messages.join(', ')}"
end
# Errors raised here should be rescued in the CsvRowImportJob and the
# message should be recorded on the CsvRow object for reporting in the UI
raise "Validation failed: #{error_messages.join(', ')}"
rescue Ldp::BadRequest
# get the id from the ark and the uri from the id then delete the tombstone
tombstone_uri = "#{ActiveFedora::Base.id_to_uri(Californica::IdGenerator.id_from_ark(created.ark))}/fcr:tombstone"
ActiveFedora.fedora.connection.delete(tombstone_uri)
retry if (retries += 1) < 3
end
end
end
12 changes: 9 additions & 3 deletions app/importers/collection_record_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ def import_type

def create_for(record:)
info_stream << "event: collection_import_started, batch_id: #{batch_id}, record_title: #{record.respond_to?(:title) ? record.title : record}\n"

collection = Collection.find_or_create_by_ark(record.ark)
begin
retries ||= 0
collection = Collection.find_or_create_by_ark(record.ark)
rescue Ldp::Gone
# get the id from the ark and the uri from the id then delete the tombstone
tombstone_uri = "#{ActiveFedora::Base.id_to_uri(Californica::IdGenerator.id_from_ark(record.ark))}/fcr:tombstone"
ActiveFedora.fedora.connection.delete(tombstone_uri)
retry if (retries += 1) < 3
end
collection.attributes = attributes_for(record: record)
collection.recalculate_size = false
if collection.save(update_index: false)
Expand All @@ -26,7 +33,6 @@ def create_for(record:)

def update_for(existing_record:, update_record:)
info_stream << "event: collection_update_started, batch_id: #{batch_id}, ark: #{update_record.ark}\n"

collection = existing_record
collection.attributes = attributes_for(record: update_record)
collection.recalculate_size = false
Expand Down

0 comments on commit 007d758

Please sign in to comment.