Commit 076cb80f authored by Nick Thomas's avatar Nick Thomas

Robustify reading attributes for elasticsearch

parent 6e519f8c
...@@ -74,6 +74,16 @@ module Elastic ...@@ -74,6 +74,16 @@ module Elastic
def es_parent def es_parent
project_id if respond_to?(:project_id) project_id if respond_to?(:project_id)
end end
# Some attributes are actually complicated methods. Bad data can cause
# them to raise exceptions. When this happens, we still want the remainder
# of the object to be saved, so silently swallow the errors
def safely_read_attribute_for_elasticsearch(attr_name)
send(attr_name)
rescue => err
logger.warn("Elasticsearch failed to read #{attr_name} for #{self.class} #{self.id}: #{err}")
nil
end
end end
module ClassMethods module ClassMethods
......
...@@ -27,7 +27,7 @@ module Elastic ...@@ -27,7 +27,7 @@ module Elastic
# We don't use as_json(only: ...) because it calls all virtual and serialized attributtes # We don't use as_json(only: ...) because it calls all virtual and serialized attributtes
# https://gitlab.com/gitlab-org/gitlab-ee/issues/349 # https://gitlab.com/gitlab-org/gitlab-ee/issues/349
[:id, :iid, :title, :description, :created_at, :updated_at, :state, :project_id, :author_id, :assignee_id, :confidential].each do |attr| [:id, :iid, :title, :description, :created_at, :updated_at, :state, :project_id, :author_id, :assignee_id, :confidential].each do |attr|
data[attr.to_s] = self.send(attr) data[attr.to_s] = safely_read_attribute_for_elasticsearch(attr)
end end
data data
......
...@@ -45,7 +45,7 @@ module Elastic ...@@ -45,7 +45,7 @@ module Elastic
:target_project_id, :target_project_id,
:author_id :author_id
].each do |attr| ].each do |attr|
data[attr.to_s] = self.send(attr) data[attr.to_s] = safely_read_attribute_for_elasticsearch(attr)
end end
data data
......
...@@ -17,9 +17,15 @@ module Elastic ...@@ -17,9 +17,15 @@ module Elastic
end end
def as_indexed_json(options = {}) def as_indexed_json(options = {})
as_json( # We don't use as_json(only: ...) because it calls all virtual and serialized attributtes
only: [:id, :title, :description, :project_id, :created_at, :updated_at] # https://gitlab.com/gitlab-org/gitlab-ee/issues/349
) data = {}
[:id, :title, :description, :project_id, :created_at, :updated_at].each do |attr|
data[attr.to_s] = safely_read_attribute_for_elasticsearch(attr)
end
data
end end
def self.nested? def self.nested?
......
...@@ -26,7 +26,7 @@ module Elastic ...@@ -26,7 +26,7 @@ module Elastic
# We don't use as_json(only: ...) because it calls all virtual and serialized attributtes # We don't use as_json(only: ...) because it calls all virtual and serialized attributtes
# https://gitlab.com/gitlab-org/gitlab-ee/issues/349 # https://gitlab.com/gitlab-org/gitlab-ee/issues/349
[:id, :note, :project_id, :created_at, :updated_at].each do |attr| [:id, :note, :project_id, :created_at, :updated_at].each do |attr|
data[attr.to_s] = self.send(attr) data[attr.to_s] = safely_read_attribute_for_elasticsearch(attr)
end end
if noteable.is_a?(Issue) if noteable.is_a?(Issue)
......
...@@ -46,7 +46,7 @@ module Elastic ...@@ -46,7 +46,7 @@ module Elastic
:name_with_namespace, :name_with_namespace,
:path_with_namespace :path_with_namespace
].each do |attr| ].each do |attr|
data[attr.to_s] = self.send(attr) data[attr.to_s] = safely_read_attribute_for_elasticsearch(attr)
end end
data data
......
...@@ -22,20 +22,25 @@ module Elastic ...@@ -22,20 +22,25 @@ module Elastic
end end
def as_indexed_json(options = {}) def as_indexed_json(options = {})
as_json({ # We don't use as_json(only: ...) because it calls all virtual and serialized attributtes
only: [ # https://gitlab.com/gitlab-org/gitlab-ee/issues/349
data = {}
[
:id, :id,
:title, :title,
:file_name, :file_name,
:content, :content,
:created_at, :created_at,
:updated_at, :updated_at,
:state,
:project_id, :project_id,
:author_id, :author_id,
:visibility_level :visibility_level
] ].each do |attr|
}) data[attr.to_s] = safely_read_attribute_for_elasticsearch(attr)
end
data
end end
def self.elastic_search(query, options: {}) def self.elastic_search(query, options: {})
......
---
title: Robustify reading attributes for elasticsearch
merge_request: 1365
author:
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment