Skip to content

Add SyncTapasXqMetadataJob and after_update callbacks to keep tapas-xq in sync #56

@candyhazlett

Description

@candyhazlett

Background

When a CoreFile is updated in Rails (title, authors, contributors, visibility, collections), the MODS and TFE records stored in BaseX become stale. Neither POST .../mods nor POST .../tfe is called after initial upload.

Work required

app/jobs/sync_tapas_xq_metadata_job.rb

Create the job with a sync: keyword argument accepting :mods, :tfe, or :both.

  • Skips if TapasXq.configuration.disabled? or core_file.tapas_xq_doc_id is blank
  • For :mods or :both: POSTs to /#{project_id}/#{doc_id}/mods with title, authors, contributors; saves returned MODS XML to core_file.mods_xml via update_columns
  • For :tfe or :both: POSTs to /#{project_id}/#{doc_id}/tfe with collections (comma-separated IDs) and is-public
  • Swallows TapasXq::NotFoundError with a warning log
  • Retries on TimeoutError (3×, 5s) and ConnectionError (3×, 10s)

app/models/core_file.rb

Add after_update :enqueue_tapas_xq_metadata_sync, if: :tapas_xq_uploaded? and:

MODS_FIELDS = %w[title tei_authors tei_contributors].freeze
TFE_FIELDS  = %w[is_public].freeze

def tapas_xq_uploaded?
  tapas_xq_doc_id.present?
end

def enqueue_tapas_xq_metadata_sync
  mods_changed = MODS_FIELDS.any? { |f| saved_change_to_attribute?(f) }
  tfe_changed  = TFE_FIELDS.any?  { |f| saved_change_to_attribute?(f) }

  sync = if mods_changed && tfe_changed then :both
         elsif mods_changed              then :mods
         elsif tfe_changed               then :tfe
         end

  SyncTapasXqMetadataJob.perform_later(id, sync: sync) if sync
end

app/models/collection_core_file.rb

Collection changes go through the join model and do not fire CoreFile#after_update. Add callbacks here:

after_create  :sync_tfe_with_tapas_xq
after_destroy :sync_tfe_with_tapas_xq

private

def sync_tfe_with_tapas_xq
  return unless core_file.tapas_xq_doc_id.present?
  SyncTapasXqMetadataJob.perform_later(core_file_id, sync: :tfe)
end

Dependencies

  • No new services required — uses TapasXq::Client directly or can be extracted into a service later

Tests

See spec/jobs/sync_tapas_xq_metadata_job_spec.rb, additions to spec/models/core_file_spec.rb, and spec/requests/core_files_spec.rb in the testing plan doc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions