diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 543516094..eb8e52a8a 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -349,9 +349,8 @@ def set_minted def set_url authorize! :set_url, Doi from_date = Time.zone.now - 1.day - Doi.where(url: nil).where(aasm_state: ["registered", "findable"]).where("updated >= ?", from_date).find_each do |doi| - UrlJob.perform_later(doi) - end + Doi.set_url(from_date: from_date.strftime("%F")) + render json: { message: "Adding missing URLs queued." }.to_json, status: :ok end diff --git a/app/jobs/url_job.rb b/app/jobs/url_job.rb index a6334ff01..24a51a869 100644 --- a/app/jobs/url_job.rb +++ b/app/jobs/url_job.rb @@ -8,7 +8,11 @@ class UrlJob < ActiveJob::Base def perform(doi) logger = Logger.new(STDOUT) - logger.debug "Set URL for #{doi.doi}" - doi.send(:set_url) + + response = Maremma.head(doi.identifier, limit: 0) + if response.headers.present? + doi.update_attributes(:url, response.headers["location"]) + logger.debug "Set URL #{response.headers["location"]} for DOI #{doi.doi}" + end end end diff --git a/app/models/doi.rb b/app/models/doi.rb index b1e1ad9e8..905a65164 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -471,6 +471,15 @@ def self.register_all_urls(limit: nil) end end + def self.set_url(from_date: nil) + from_date = from_date.present? ? Date.parse(from_date) : Date.current - 1.day + Doi.where(url: nil).where(aasm_state: ["registered", "findable"]).where("updated >= ?", from_date).find_each do |doi| + UrlJob.perform_later(doi) + end + + "Queued storing missing URL in database for DOIs updated since #{from_date.strftime("%F")}." + end + # update metadata when any virtual attribute has changed def update_metadata changed_virtual_attributes = changed & %w(author title publisher date_published additional_type resource_type_general description content_size content_format) @@ -497,13 +506,4 @@ def set_defaults def update_doi_count Rails.cache.delete("cached_doi_count/#{datacentre}") end - - def set_url - response = Maremma.head(identifier, limit: 0) - if response.headers.present? - update_column(:url, response.headers["location"]) - logger = Logger.new(STDOUT) - logger.debug "Set URL #{response.headers["location"]} for DOI #{doi}" - end - end end diff --git a/lib/tasks/doi.rake b/lib/tasks/doi.rake index 01751a3ff..d4a5c273d 100644 --- a/lib/tasks/doi.rake +++ b/lib/tasks/doi.rake @@ -1,10 +1,9 @@ namespace :doi do desc 'Store handle URL' task :set_url => :environment do - from_date = ENV['FROM_DATE'] || Time.zone.now - 1.day - Doi.where(url: nil).where(aasm_state: ["registered", "findable"]).where("updated >= ?", from_date).find_each do |doi| - UrlJob.perform_later(doi) - end + from_date = ENV['FROM_DATE'] || (Time.zone.now - 1.day).strftime("%F") + response = Doi.set_url(from_date: from_date) + puts response end desc 'Index all DOIs'