Skip to content

Commit

Permalink
update prefix indexes without using callback. #642
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Sep 27, 2020
1 parent 7c26172 commit de562b8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
22 changes: 21 additions & 1 deletion app/controllers/client_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ def create
authorize! :create, @client_prefix

if @client_prefix.save
if @client_prefix.__elasticsearch__.index_document.dig("result") != "created"
logger.error "Error adding Client Prefix #{@client_prefix.uid} to Elasticsearch index."
end
if @client_prefix.prefix.__elasticsearch__.index_document.dig("result") != "updated"
logger.error "Error updating Elasticsearch index for Prefix #{@client_prefix.prefix.uid}."
end
if @client_prefix.provider_prefix.__elasticsearch__.index_document.dig("result") != "updated"
logger.error "Error updating Elasticsearch index for Provider Prefix #{@client_prefix.provider_prefix.uid}."
end

options = {}
options[:include] = @include
options[:is_collection] = false
Expand All @@ -105,7 +115,17 @@ def destroy
message = "Client prefix #{@client_prefix.uid} deleted."

if @client_prefix.destroy
Rails.logger.warn message
if @client_prefix.__elasticsearch__.delete_document.dig("result") != "deleted"
logger.error "Error deleting Client Prefix #{@client_prefix.uid} from Elasticsearch index."
end
if @client_prefix.prefix.__elasticsearch__.index_document.dig("result") != "updated"
logger.error "Error updating Elasticsearch index for Prefix #{@client_prefix.prefix.uid}."
end
if @client_prefix.provider_prefix.__elasticsearch__.index_document
logger.error "Error updating Elasticsearch index for Provider Prefix #{@client_prefix.provider_prefix.uid}."
end

logger.warn message
head :no_content
else
Rails.logger.error @client_prefix.errors.inspect
Expand Down
22 changes: 21 additions & 1 deletion app/controllers/provider_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ def create
authorize! :create, @provider_prefix

if @provider_prefix.save
if @provider_prefix.__elasticsearch__.index_document.dig("result") != "created"
logger.error "Error adding Provider Prefix #{@provider_prefix.uid} to Elasticsearch index."
end
if @provider_prefix.prefix.__elasticsearch__.index_document.dig("result") != "updated"
logger.error "Error updating Elasticsearch index for Prefix #{@provider_prefix.prefix.uid}."
end
if @provider_prefix.provider.__elasticsearch__.index_document.dig("result") != "updated"
logger.error "Error updating Elasticsearch index for Provider #{@provider_prefix.provider.uid}."
end

options = {}
options[:include] = @include
options[:is_collection] = false
Expand All @@ -104,7 +114,17 @@ def update
def destroy
message = "Provider prefix #{@provider_prefix.uid} deleted."
if @provider_prefix.destroy
Rails.logger.warn message
if @provider_prefix.__elasticsearch__.delete_document.dig("result") != "deleted"
logger.error "Error deleting Provider Prefix #{@provider_prefix.uid} from Elasticsearch index."
end
if @provider_prefix.prefix.__elasticsearch__.index_document.dig("result") != "updated"
logger.error "Error updating Elasticsearch index for Prefix #{@provider_prefix.prefix.uid}."
end
if @provider_prefix.provider.__elasticsearch__.index_document.dig("result") != "updated"
logger.error "Error updating Elasticsearch index for Provider #{@provider_prefix.provider.uid}."
end

logger.info message
head :no_content
else
Rails.logger.error @provider_prefix.errors.inspect
Expand Down
28 changes: 1 addition & 27 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ module Indexable
send_import_message(self.to_jsonapi) if aasm_state == "findable" && !Rails.env.test?
elsif self.class.name == "Event"
OtherDoiJob.perform_later(self.dois_to_import)
elsif self.class.name == "ProviderPrefix"
self.__elasticsearch__.index_document
Rails.logger.info "#{self.class.name} #{uid} added to Elasticsearch index."
prefix.__elasticsearch__.index_document
Rails.logger.info "Prefix #{prefix.uid} Elasticsearch index updated."
provider.__elasticsearch__.index_document
Rails.logger.info "Provider #{provider.uid} Elasticsearch index updated."
elsif self.class.name == "ClientPrefix"
self.__elasticsearch__.index_document
Rails.logger.info "#{self.class.name} #{uid} added to Elasticsearch index."
prefix.__elasticsearch__.index_document
Rails.logger.info "Prefix #{prefix.uid} Elasticsearch index updated."
provider_prefix.__elasticsearch__.index_document
Rails.logger.info "Provider Prefix #{provider_prefix.uid} Elasticsearch index updated."
end
end

Expand All @@ -36,21 +22,9 @@ module Indexable

after_commit on: [:destroy] do
begin
__elasticsearch__.delete_document
__elasticsearch__.delete_document unless ["ProviderPrefix", "ClientPrefix"].include?(self.class.name)
if self.class.name == "Event"
Rails.logger.info "#{self.class.name} #{uuid} deleted from Elasticsearch index."
elsif self.class.name == "ProviderPrefix"
Rails.logger.info "#{self.class.name} #{uid} deleted from Elasticsearch index."
prefix.__elasticsearch__.index_document
Rails.logger.info "Prefix #{prefix.uid} Elasticsearch index updated."
provider.__elasticsearch__.index_document
Rails.logger.info "Provider #{provider.uid} Elasticsearch index updated."
elsif self.class.name == "ClientPrefix"
Rails.logger.info "#{self.class.name} #{uid} deleted from Elasticsearch index."
prefix.__elasticsearch__.index_document
Rails.logger.info "Prefix #{prefix.uid} Elasticsearch index updated."
provider_prefix.__elasticsearch__.index_document
Rails.logger.info "Provider Prefix #{provider_prefix.uid} Elasticsearch index updated."
else
Rails.logger.info "#{self.class.name} #{uid} deleted from Elasticsearch index."
end
Expand Down

0 comments on commit de562b8

Please sign in to comment.