Skip to content

Commit

Permalink
rake task to update state. datacite/datacite#491
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Sep 24, 2018
1 parent 68af844 commit c6655e3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
16 changes: 16 additions & 0 deletions app/jobs/update_state_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class UpdateStateJob < ActiveJob::Base
queue_as :lupo_background

def perform(doi_id, options={})
logger = Logger.new(STDOUT)
doi = Doi.where(doi: doi_id).first

if doi.blank?
logger.info "[Update State] Error updating state for DOI " + doi_id + ": not found"
elsif doi.update_attributes(aasm_state: options[:state])
logger.info "[Update State] Successfully update state for DOI " + doi_id
else
logger.info "[Update State] Error updating state for DOI " + doi_id + ": " + errors.inspect
end
end
end
18 changes: 11 additions & 7 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,19 @@ def timestamp
updated.utc.iso8601 if updated.present?
end

# update state for all DOIs starting from from_date
# update state for all DOIs in state "undetermined" starting from from_date
def self.set_state(from_date: nil)
from_date ||= Time.zone.now - 1.day
collection = Doi.where("updated >= ?", from_date).where("updated < ?", Time.zone.now - 15.minutes).where(aasm_state: '')

collection.where(is_active: "\x00").where(minted: nil).update_all(aasm_state: "draft")
collection.where(is_active: "\x00").where.not(minted: nil).update_all(aasm_state: "registered")
collection.where(is_active: "\x01").where.not(minted: nil).update_all(aasm_state: "findable")
collection.where("doi LIKE ?", "10.5072%").where.not(aasm_state: "draft").update_all(aasm_state: "draft")
Doi.where("updated >= ?", from_date).where(aasm_state: 'undetermined').find_each do |doi|
if doi.is_test_prefix? || (doi.is_active == "\x00" && doi.minted.blank?)
state = "draft"
elsif doi.is_active == "\x00" && doi.minted.present?
state = "registered"
elsif doi.is_active == "\x01" && doi.minted.present?
state = "findable"
end
UpdateStateJob.perform_later(doi.doi, state: state)
end
rescue ActiveRecord::LockWaitTimeout => exception
Bugsnag.notify(exception)
end
Expand Down
16 changes: 16 additions & 0 deletions spec/jobs/update_state_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

describe UpdateStateJob, type: :job do
let(:doi) { create(:doi) }
subject(:job) { UpdateStateJob.perform_later(doi.doi) }

it 'queues the job' do
expect { job }.to have_enqueued_job(UpdateStateJob)
.on_queue("test_lupo_background")
end

after do
clear_enqueued_jobs
clear_performed_jobs
end
end

0 comments on commit c6655e3

Please sign in to comment.