Skip to content

Commit

Permalink
reindex doi when event is updated. #388
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 5, 2020
1 parent f54b3a0 commit a3c0727
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
11 changes: 11 additions & 0 deletions app/jobs/index_background_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class IndexBackgroundJob < ActiveJob::Base
queue_as :lupo_background

rescue_from ActiveJob::DeserializationError, Elasticsearch::Transport::Transport::Errors::BadRequest do |error|
Rails.logger.error error.message
end

def perform(obj)
obj.__elasticsearch__.index_document
end
end
2 changes: 1 addition & 1 deletion app/jobs/index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ class IndexJob < ActiveJob::Base
def perform(obj)
obj.__elasticsearch__.index_document
end
end
end
9 changes: 1 addition & 8 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ module Indexable
if self.class.name == "Doi"
update_column(:indexed, Time.zone.now)
send_import_message(self.to_jsonapi) if aasm_state == "findable" && !Rails.env.test? && !%w(crossref medra kisti jalc op).include?(client.symbol.downcase.split(".").first)
# elsif self.class.name == "Event"
# # reindex dois associated with Event
# @source_doi = Doi.where(doi: source_doi).first if source_doi
# IndexJob.perform_later(@source_doi) if @source_doi

# @target_doi = Doi.where(doi: target_doi).first if target_doi
# IndexJob.perform_later(@target_doi) if @target_doi
end
end

after_touch do
# use index_document instead of update_document to also update virtual attributes
IndexJob.perform_later(self)
IndexBackgroundJob.perform_later(self)
end

before_destroy do
Expand Down
3 changes: 3 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Event < ActiveRecord::Base

include Elasticsearch::Model

belongs_to :doi_for_source, class_name: "Doi", primary_key: :doi, foreign_key: :source_doi, touch: true, optional: true
belongs_to :doi_for_target, class_name: "Doi", primary_key: :doi, foreign_key: :target_doi, touch: true, optional: true

before_validation :set_defaults
before_create :set_source_and_target_doi

Expand Down

0 comments on commit a3c0727

Please sign in to comment.