From 9f2f6005d476b0bc0c88b8f1bb5954a628fd3c91 Mon Sep 17 00:00:00 2001 From: Kirk Wang Date: Fri, 10 May 2024 08:09:39 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20FileSet=20csv=20rows=20for?= =?UTF-8?q?=20AF=20imports?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit will fix the functionality for adding a FileSet row in the CSV to add additional metadata to FileSets. The problem was located in `Bulkrax::ObjectFactory#create_file_set`. When it tries to find a record, it actually comes back as a `FalseClass` because of the new logic introduced recently which breaks further down. If we add raise an error in our `Bulkrax::ImportFileSetJob#find_parent_record` which is at a higher point than before, then we can requeue the job and check again. If the parent record is created by then, we will execute the rest of the job. --- app/jobs/bulkrax/import_file_set_job.rb | 35 +++++++++++++------------ 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/app/jobs/bulkrax/import_file_set_job.rb b/app/jobs/bulkrax/import_file_set_job.rb index e0bc7175..65780a6e 100644 --- a/app/jobs/bulkrax/import_file_set_job.rb +++ b/app/jobs/bulkrax/import_file_set_job.rb @@ -16,7 +16,12 @@ def perform(entry_id, importer_run_id) # e.g. "parents" or "parents_1" parent_identifier = (entry.raw_metadata[entry.related_parents_raw_mapping] || entry.raw_metadata["#{entry.related_parents_raw_mapping}_1"])&.strip - validate_parent!(parent_identifier) + begin + validate_parent!(parent_identifier) + rescue MissingParentError => e + handle_retry(entry, importer_run_id, e) + return + end entry.build if entry.succeeded? @@ -32,17 +37,6 @@ def perform(entry_id, importer_run_id) entry.save! entry.importer.current_run = ImporterRun.find(importer_run_id) entry.importer.record_status - - rescue MissingParentError => e - # try waiting for the parent record to be created - entry.import_attempts += 1 - entry.save! - if entry.import_attempts < 5 - ImportFileSetJob.set(wait: (entry.import_attempts + 1).minutes).perform_later(entry_id, importer_run_id) - else - ImporterRun.decrement_counter(:enqueued_records, importer_run_id) # rubocop:disable Rails/SkipsModelValidations - entry.set_status_info(e) - end end private @@ -54,14 +48,9 @@ def validate_parent!(parent_identifier) return if parent_identifier.blank? find_parent_record(parent_identifier) - check_parent_exists!(parent_identifier) check_parent_is_a_work!(parent_identifier) end - def check_parent_exists!(parent_identifier) - raise MissingParentError, %(Unable to find a record with the identifier "#{parent_identifier}") if parent_record.nil? - end - def check_parent_is_a_work!(parent_identifier) case parent_record when Bulkrax.collection_model_class, Bulkrax.file_model_class @@ -72,6 +61,18 @@ def check_parent_is_a_work!(parent_identifier) def find_parent_record(parent_identifier) _, @parent_record = find_record(parent_identifier, importer_run_id) + raise MissingParentError, %(Unable to find a record with the identifier "#{parent_identifier}") unless parent_record + end + + def handle_retry(entry, importer_run_id, e) + entry.import_attempts += 1 + entry.save! + if entry.import_attempts < 5 + ImportFileSetJob.set(wait: (entry.import_attempts + 1).minutes).perform_later(entry.id, importer_run_id) + else + ImporterRun.decrement_counter(:enqueued_records, importer_run_id) # rubocop:disable Rails/SkipsModelValidations + entry.set_status_info(e) + end end end end