From 30b600dae82332596c5cace01d4c5a29016df930 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 19 Dec 2020 18:42:23 +0100 Subject: [PATCH] background job of reindexing. datacite/datacite#965 --- app/jobs/doi_not_indexed_job.rb | 9 +++++++++ app/models/client.rb | 13 ++----------- app/models/doi.rb | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 app/jobs/doi_not_indexed_job.rb diff --git a/app/jobs/doi_not_indexed_job.rb b/app/jobs/doi_not_indexed_job.rb new file mode 100644 index 000000000..80b6d19fc --- /dev/null +++ b/app/jobs/doi_not_indexed_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class DoiNotIndexedJob < ApplicationJob + queue_as :lupo_background + + def perform(client_id, _options = {}) + Doi.import_by_client(client_id: client_id) + end +end diff --git a/app/models/client.rb b/app/models/client.rb index 422dbf760..949237518 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -718,17 +718,8 @@ def self.import_dois_not_indexed(query: nil) # loop through repositories that have DOIs not indexed in Elasticsearch table.each do |row| - client = ::Client.where(deleted_at: nil).where(symbol: row["Repository ID"]).first - if client.nil? - puts "Client not found for client ID #{row["Repository ID"]}." - exit - end - - # import DOIs for client. Ignore repositories with more than 10K DOIs - if client.dois.length <= 10000 - puts "#{client.dois.length} DOIs for repository #{client.symbol} will be imported." - Doi.import_by_client(client_id: client.symbol, total_count: client.dois.length) - end + Rails.logger.info "Indexing missing DOIs for repository #{row["Repository ID"]}." + DoiNotIndexedJob.perform_later(row["Repository ID"]) end end diff --git a/app/models/doi.rb b/app/models/doi.rb index 21b01bcba..655988a54 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -1243,7 +1243,7 @@ def self.import_one(doi_id: nil) message end - def self.import_by_client(client_id: nil, total_count: nil) + def self.import_by_client(client_id: nil) client = ::Client.where(symbol: client_id).first return nil if client.blank? @@ -1271,7 +1271,7 @@ def self.import_by_client(client_id: nil, total_count: nil) end count += dois.length - Rails.logger.info "[Elasticsearch] Imported #{count} DOIs for repository #{client_id}." if total_count > 500 + Rails.logger.info "[Elasticsearch] Imported #{count} DOIs for repository #{client_id}." end if errors > 1