diff --git a/app/jobs/bulkrax/create_relationships_job.rb b/app/jobs/bulkrax/create_relationships_job.rb index 54ec2209..c8e5ef95 100644 --- a/app/jobs/bulkrax/create_relationships_job.rb +++ b/app/jobs/bulkrax/create_relationships_job.rb @@ -38,6 +38,7 @@ class CreateRelationshipsJob < ApplicationJob # # @see https://github.com/scientist-softserv/louisville-hyku/commit/128a9ef class_attribute :update_child_records_works_file_sets, default: false + class_attribute :max_failure_count, default: 5 include DynamicRecordLookup @@ -55,7 +56,7 @@ class CreateRelationshipsJob < ApplicationJob # is the child in the relationship, and vice versa if a child_identifier is passed. # # rubocop:disable Metrics/MethodLength - def perform(parent_identifier:, importer_run_id: nil, run_user: nil) # rubocop:disable Metrics/AbcSize + def perform(parent_identifier:, importer_run_id: nil, run_user: nil, failure_count: 0) # rubocop:disable Metrics/AbcSize importer_run = Bulkrax::ImporterRun.find(importer_run_id) if importer_run_id user = run_user || importer_run&.user ability = Ability.new(user) @@ -78,6 +79,7 @@ def perform(parent_identifier:, importer_run_id: nil, run_user: nil) # rubocop:d @parent_record_members_added = true rescue => e number_of_failures += 1 + rel.set_status_info(e, importer_run) errors << e end end @@ -107,9 +109,16 @@ def perform(parent_identifier:, importer_run_id: nil, run_user: nil) # rubocop:d # rubocop:enable Rails/SkipsModelValidations parent_entry&.set_status_info(errors.last, importer_run) - - # TODO: This can create an infinite job cycle, consider a time to live tracker. - reschedule(parent_identifier: parent_identifier, importer_run_id: importer_run_id) + failure_count += 1 + + if failure_count < max_failure_count + reschedule( + parent_identifier: parent_identifier, + importer_run_id: importer_run_id, + run_user: run_user, + failure_count: failure_count + ) + end return errors # stop current job from continuing to run after rescheduling else # rubocop:disable Rails/SkipsModelValidations @@ -184,11 +193,8 @@ def add_to_work(child_record, parent_record) ) end - def reschedule(parent_identifier:, importer_run_id:) - CreateRelationshipsJob.set(wait: 10.minutes).perform_later( - parent_identifier: parent_identifier, - importer_run_id: importer_run_id - ) + def reschedule(**kargs) + CreateRelationshipsJob.set(wait: 10.minutes).perform_later(**kargs) end end end diff --git a/app/models/bulkrax/pending_relationship.rb b/app/models/bulkrax/pending_relationship.rb index b842f1e9..a2b972a5 100644 --- a/app/models/bulkrax/pending_relationship.rb +++ b/app/models/bulkrax/pending_relationship.rb @@ -2,6 +2,8 @@ module Bulkrax class PendingRelationship < ApplicationRecord + include Bulkrax::StatusInfo + belongs_to :importer_run # Ideally we wouldn't have a column named "order", as it is a reserved SQL term. However, if we diff --git a/db/migrate/20240823173525_add_error_tracking_to_pending_relationships.rb b/db/migrate/20240823173525_add_error_tracking_to_pending_relationships.rb new file mode 100644 index 00000000..3916e769 --- /dev/null +++ b/db/migrate/20240823173525_add_error_tracking_to_pending_relationships.rb @@ -0,0 +1,5 @@ +class AddErrorTrackingToPendingRelationships < ActiveRecord::Migration[6.1] + def change + add_column :bulkrax_pending_relationships, :status_message, :string, default: 'Pending' unless column_exists?(:bulkrax_pending_relationships, :status_message) + end +end