Skip to content

Commit

Permalink
rake task to index one doi. #413
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 10, 2020
1 parent 18f3d31 commit 5335401
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
22 changes: 19 additions & 3 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5335401

Please sign in to comment.