From 9562477add822ff3444efcf4a0c53786d2944e12 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Mon, 15 Oct 2018 21:22:38 +0200 Subject: [PATCH] automatically update doi indexed column. #116 --- app/models/concerns/indexable.rb | 9 +++------ app/models/doi.rb | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 5baec2d24..25fa5d2eb 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -7,12 +7,9 @@ module Indexable after_commit on: [:create, :update] do # use index_document instead of update_document to also update virtual attributes IndexJob.perform_later(self) - if self.class.name == "Doi" && !Rails.env.test? - index_interval = (Time.zone.now - updated_at) - logger = Logger.new(STDOUT) - logger.info "[Elasticsearch] Indexing of DOI #{doi} finished #{index_interval.to_s} seconds after DOI update." - - send_import_message(self.to_jsonapi) if aasm_state == "findable" + if self.class.name == "Doi" + update_column(:indexed, Time.zone.now) + send_import_message(self.to_jsonapi) if aasm_state == "findable" unless Rails.env.test? end end diff --git a/app/models/doi.rb b/app/models/doi.rb index b7bf88ee3..f6b448dfc 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -250,7 +250,7 @@ def self.index_by_day(options={}) logger = Logger.new(STDOUT) - Doi.where("created >= ?", from_date.strftime("%F") + " 00:00:00").where("created < ?", until_date.strftime("%F") + " 00:00:00").find_in_batches(batch_size: 100) do |dois| + Doi.where("created >= ?", from_date.strftime("%F") + " 00:00:00").where("created < ?", until_date.strftime("%F") + " 00:00:00").where("updated > indexed").find_in_batches(batch_size: 100) do |dois| response = Doi.__elasticsearch__.client.bulk \ index: Doi.index_name, type: Doi.document_type, @@ -258,6 +258,7 @@ def self.index_by_day(options={}) errors += response['items'].map { |k, v| k.values.first['error'] }.compact.length count += dois.length + dois.each { |doi| doi.update_column(:indexed, Time.zone.now) } end logger.info "[Elasticsearch] #{errors} errors indexing #{count} DOIs created on #{from_date.strftime("%F")}."