Skip to content

Commit

Permalink
Merge pull request #535 from datacite/update-target-doi
Browse files Browse the repository at this point in the history
fix rake task
  • Loading branch information
Martin Fenner authored May 28, 2020
2 parents e449e33 + 63153e7 commit 8829866
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 44 deletions.
3 changes: 1 addition & 2 deletions app/jobs/target_doi_by_id_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ class TargetDoiByIdJob < ActiveJob::Base
Rails.logger.error error.message
end

def perform(id, options={})
def perform(id, options = nil)
item = Event.where(uuid: id).first
return false if item.blank?

item.set_source_and_target_doi

if item.save
Rails.logger.info "Target doi for #{item.uuid} updated."
else
Expand Down
9 changes: 0 additions & 9 deletions app/jobs/target_doi_job.rb

This file was deleted.

2 changes: 2 additions & 0 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ def query(query, options={})
filter << { terms: { registrant_id: options[:registrant_id].split(",") }} if options[:registrant_id].present?
filter << { terms: { registrant_id: options[:provider_id].split(",") }} if options[:provider_id].present?
filter << { terms: { issn: options[:issn].split(",") }} if options[:issn].present?

must_not << { exists: { field: "target_doi" }} if options[:update_target_doi].present?
elsif self.name == "Prefix"
if query.present?
must = [{ prefix: { prefix: query }}]
Expand Down
30 changes: 2 additions & 28 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,32 +379,6 @@ def self.update_crossref(options = {})
response.results.total
end

def self.update_target_doi(options = {})
size = (options[:size] || 1000).to_i
cursor = (options[:cursor] || [])
target_relation_type_id = options[:target_relation_type_id]

response = Event.query(nil, target_relation_type_id: target_relation_type_id, page: { size: 1, cursor: [] })
Rails.logger.info "[Update] #{response.results.total} events with target_relation_type_id #{target_relation_type_id.to_s}."

# walk through results using cursor
if response.results.total > 0
while response.results.results.length > 0 do
response = Event.query(nil, target_relation_type_id: target_relation_type_id, page: { size: size, cursor: cursor })
break unless response.results.results.length.positive?

Rails.logger.info "[Update] Updating #{response.results.results.length} events with target_relation_type_id #{target_relation_type_id.to_s} starting with _id #{response.results.to_a.first[:_id]}."
cursor = response.results.to_a.last[:sort]

ids = response.results.results.map(&:uuid).uniq

TargetDoiJob.perform_later(ids, options)
end
end

response.results.total
end

def self.update_datacite_crossref(options = {})
update_datacite_ra(options.merge(ra: "crossref"))
end
Expand Down Expand Up @@ -601,7 +575,7 @@ def self.label_state_event(event)
# +job_name+:: Acive Job class name of the Job that would be executed on every matched results
def self.loop_through_events(options)
size = (options[:size] || 1000).to_i
cursor = [options[:from_id] || Doi.minimum(:id).to_i, options[:until_id] || Doi.maximum(:id).to_i]
cursor = options[:cursor] || [options[:from_id] || Event.minimum(:id).to_i, options[:until_id] || Event.maximum(:id).to_i]
filter = options[:filter] || {}
label = options[:label] || ""
job_name = options[:job_name] || ""
Expand All @@ -620,7 +594,7 @@ def self.loop_through_events(options)
cursor = response.results.to_a.last[:sort]
Rails.logger.info "#{label} Cursor: #{cursor} "

ids = response.results.results.map(&:uuid)
ids = response.results.results.map(&:uuid).uniq
ids.each do |id|
Object.const_get(job_name).perform_later(id, filter)
end
Expand Down
14 changes: 9 additions & 5 deletions lib/tasks/event.rake
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ namespace :event do
Event.update_registrant(cursor: cursor, size: ENV['SIZE'])
end

desc 'update target doi'
task :update_target_doi => :environment do
cursor = ENV['CURSOR'].to_s.split(",") || [Event.minimum(:id), Event.minimum(:id)]

Event.update_target_doi(cursor: cursor, target_relation_type_id: ENV['TARGET_RELATION_TYPE_ID'], size: ENV['SIZE'])
desc "update target doi"
task update_target_doi: :environment do
options = {
cursor: ENV["CURSOR"].present? ? Base64.urlsafe_decode64(ENV["CURSOR"]).split(",", 2) : [],
filter: { update_target_doi: true },
label: "[UpdateTargetDoi] Updating",
job_name: "TargetDoiByIdJob",
}
Event.loop_through_events(options)
end
end

Expand Down

0 comments on commit 8829866

Please sign in to comment.