Class: Bulkrax::CreateRelationshipsJob

Inherits:
ApplicationJob
  • Object
show all
Includes:
DynamicRecordLookup
Defined in:
app/jobs/bulkrax/create_relationships_job.rb

Overview

Responsible for creating parent-child relationships between Works and Collections.

Handles three kinds of relationships:

  • Work to Collection

  • Collection to Collection

  • Work to Work

These can be established from either side of the relationship (i.e. from parent to child or from child to parent). This job only creates one relationship at a time. If a record needs multiple parents or children or both, individual jobs should be run for each of those relationships.

NOTE: In the context of this job, “record” is used to generically refer

to either an instance of a Work or an instance of a Collection.

NOTE: In the context of this job, “identifier” is used to generically refer

to either a record's ID or an Bulkrax::Entry's source_identifier.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DynamicRecordLookup

#curation_concern?, #find_record

Instance Attribute Details

#child_entryObject

Returns the value of attribute child_entry.



24
25
26
# File 'app/jobs/bulkrax/create_relationships_job.rb', line 24

def child_entry
  @child_entry
end

#child_recordsObject

Returns the value of attribute child_records.



24
25
26
# File 'app/jobs/bulkrax/create_relationships_job.rb', line 24

def child_records
  @child_records
end

#importer_run_idObject

Returns the value of attribute importer_run_id.



24
25
26
# File 'app/jobs/bulkrax/create_relationships_job.rb', line 24

def importer_run_id
  @importer_run_id
end

#parent_entryObject

Returns the value of attribute parent_entry.



24
25
26
# File 'app/jobs/bulkrax/create_relationships_job.rb', line 24

def parent_entry
  @parent_entry
end

#parent_recordObject

Returns the value of attribute parent_record.



24
25
26
# File 'app/jobs/bulkrax/create_relationships_job.rb', line 24

def parent_record
  @parent_record
end

Instance Method Details

#perform(parent_identifier:, importer_run_id:) ⇒ Object

The entry_identifier is used to lookup the @base_entry for the job (a.k.a. the entry the job was called from). The @base_entry defines the context of the relationship (e.g. “this entry (@base_entry) should have a parent”). Whether the @base_entry is the parent or the child in the relationship is determined by the presence of a parent_identifier or child_identifier param. For example, if a parent_identifier is passed, we know @base_entry is the child in the relationship, and vice versa if a child_identifier is passed.

Parameters:

  • parent_identifier (String)

    Work/Collection ID or Bulkrax::Entry source_identifiers

  • importer_run (Bulkrax::ImporterRun)

    current importer run (needed to properly update counters)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/jobs/bulkrax/create_relationships_job.rb', line 34

def perform(parent_identifier:, importer_run_id:) # rubocop:disable Metrics/AbcSize
  pending_relationships = Bulkrax::PendingRelationship.find_each.select do |rel|
    rel.importer_run_id == importer_run_id && rel.parent_id == parent_identifier
  end.sort_by(&:order)

  @importer_run_id = importer_run_id
  @parent_entry, @parent_record = find_record(parent_identifier, importer_run_id)
  @child_records = { works: [], collections: [] }
  pending_relationships.each do |rel|
    raise ::StandardError, %("#{rel}" needs either a child or a parent to create a relationship) if rel.child_id.nil? || rel.parent_id.nil?
    @child_entry, child_record = find_record(rel.child_id, importer_run_id)
    if child_record
      child_record.is_a?(::Collection) ? @child_records[:collections] << child_record : @child_records[:works] << child_record
    end
  end

  if (child_records[:collections].blank? && child_records[:works].blank?) || parent_record.nil?
    reschedule({ parent_identifier: parent_identifier, importer_run_id: importer_run_id })
    return false # stop current job from continuing to run after rescheduling
  end
  @parent_entry ||= Bulkrax::Entry.where(identifier: parent_identifier,
                                         importerexporter_id: ImporterRun.find(importer_run_id).importer_id,
                                         importerexporter_type: "Bulkrax::Importer").first
  create_relationships
  pending_relationships.each(&:destroy)
rescue ::StandardError => e
  parent_entry ? parent_entry.status_info(e) : child_entry.status_info(e)
  Bulkrax::ImporterRun.find(importer_run_id).increment!(:failed_relationships) # rubocop:disable Rails/SkipsModelValidations
end