From c6655e34d16cbdef915023fdb3e958c1250daaf9 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Mon, 24 Sep 2018 23:56:25 +0200 Subject: [PATCH] rake task to update state. datacite/datacite#491 --- app/jobs/update_state_job.rb | 16 ++++++++++++++++ app/models/doi.rb | 18 +++++++++++------- spec/jobs/update_state_job.rb | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 app/jobs/update_state_job.rb create mode 100644 spec/jobs/update_state_job.rb diff --git a/app/jobs/update_state_job.rb b/app/jobs/update_state_job.rb new file mode 100644 index 000000000..2b1a6966f --- /dev/null +++ b/app/jobs/update_state_job.rb @@ -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 \ No newline at end of file diff --git a/app/models/doi.rb b/app/models/doi.rb index 2db69b662..bc4dd3f46 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -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 diff --git a/spec/jobs/update_state_job.rb b/spec/jobs/update_state_job.rb new file mode 100644 index 000000000..11c681b42 --- /dev/null +++ b/spec/jobs/update_state_job.rb @@ -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 \ No newline at end of file