From 533540193808d433044024fe546d6c1877d03671 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Mon, 10 Feb 2020 22:24:50 +0100 Subject: [PATCH] rake task to index one doi. #413 --- app/models/doi.rb | 22 +++++++++++++++++++--- lib/tasks/doi.rake | 10 ++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/models/doi.rb b/app/models/doi.rb index 7a6f3bab2..60ea106df 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -81,7 +81,9 @@ class Doi < ActiveRecord::Base has_many :versions, -> { where source_relation_type_id: "versions" }, class_name: "Event", primary_key: :doi, foreign_key: :source_doi, dependent: :destroy has_many :version_of, -> { where target_relation_type_id: "version_of" }, class_name: "Event", primary_key: :doi, foreign_key: :target_doi, dependent: :destroy has_many :activities, foreign_key: :auditable_id, dependent: :destroy - + has_many :source_events, class_name: "Event", primary_key: :doi, foreign_key: :source_doi, dependent: :destroy + has_many :target_events, class_name: "Event", primary_key: :doi, foreign_key: :target_doi, dependent: :destroy + delegate :provider, to: :client, allow_nil: true delegate :consortium_id, to: :provider, allow_nil: true @@ -812,15 +814,29 @@ def self.query(query, options={}) response end + self.index_one(doi_id: nil) + doi = Doi.where(doi: doi_id).first + if doi.nil? + Rails.logger.error "[MySQL] DOI " + doi_id + " not found." + return nil + end + + doi.source_events.each { |event| IndexJob.perform_later(event) } + doi.target_events.each { |event| IndexJob.perform_later(event) } + sleep 1 + + IndexJob.perform_later(doi) + end + def self.import_one(doi_id: nil) doi = Doi.where(doi: doi_id).first - unless doi.present? + if doi.nil? Rails.logger.error "[MySQL] DOI " + doi_id + " not found." return nil end string = doi.current_metadata.present? ? doi.clean_xml(doi.current_metadata.xml) : nil - unless string.present? + if string.blank? Rails.logger.error "[MySQL] No metadata for DOI " + doi.doi + " found: " + doi.current_metadata.inspect return nil end diff --git a/lib/tasks/doi.rake b/lib/tasks/doi.rake index e8c8ef2af..1c38dc4db 100644 --- a/lib/tasks/doi.rake +++ b/lib/tasks/doi.rake @@ -64,6 +64,16 @@ namespace :doi do Doi.import_one(doi_id: ENV['DOI']) end + desc 'Index one DOI' + task :index_one => :environment do + if ENV['DOI'].nil? + puts "ENV['DOI'] is required" + exit + end + + Doi.index_one(doi_id: ENV['DOI']) + end + desc 'Store handle URL' task :set_url => :environment do Doi.set_url