Skip to content

Commit

Permalink
set-url for doi as unified background job. datacite/datacite#535
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Oct 6, 2018
1 parent dffeff1 commit ebf5611
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
5 changes: 2 additions & 3 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions app/jobs/url_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
7 changes: 3 additions & 4 deletions lib/tasks/doi.rake
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit ebf5611

Please sign in to comment.