Skip to content

Commit

Permalink
statuses and max run for pending relationships (#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
orangewolf authored Aug 23, 2024
1 parent 189b95c commit 8e5a3ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
24 changes: 15 additions & 9 deletions app/jobs/bulkrax/create_relationships_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions app/models/bulkrax/pending_relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8e5a3ba

Please sign in to comment.