Commit f2d21bea authored by Nick Thomas's avatar Nick Thomas

Simplify elasticsearch maintenance callbacks

parent fcc2bdb4
...@@ -34,14 +34,21 @@ module Elastic ...@@ -34,14 +34,21 @@ module Elastic
Elasticsearch::Model::Registry.add(self) if self.is_a?(Class) Elasticsearch::Model::Registry.add(self) if self.is_a?(Class)
if self < ActiveRecord::Base if self < ActiveRecord::Base
after_commit on: :create do after_commit :maintain_elasticsearch_create, on: :create
if Gitlab::CurrentSettings.elasticsearch_indexing? && self.searchable? after_commit :maintain_elasticsearch_update, on: :update
ElasticIndexerWorker.perform_async(:index, self.class.to_s, self.id, self.es_id) after_commit :maintain_elasticsearch_destroy, on: :destroy
end end
end end
after_commit on: :update do def maintain_elasticsearch_create
if Gitlab::CurrentSettings.elasticsearch_indexing? && self.searchable? return unless Gitlab::CurrentSettings.elasticsearch_indexing? && self.searchable?
ElasticIndexerWorker.perform_async(:index, self.class.to_s, self.id, self.es_id)
end
def maintain_elasticsearch_update
return unless Gitlab::CurrentSettings.elasticsearch_indexing? && self.searchable?
ElasticIndexerWorker.perform_async( ElasticIndexerWorker.perform_async(
:update, :update,
self.class.to_s, self.class.to_s,
...@@ -50,21 +57,14 @@ module Elastic ...@@ -50,21 +57,14 @@ module Elastic
changed_fields: self.previous_changes.keys changed_fields: self.previous_changes.keys
) )
end end
end
after_commit on: :destroy do def maintain_elasticsearch_destroy
if Gitlab::CurrentSettings.elasticsearch_indexing? && self.searchable? return unless Gitlab::CurrentSettings.elasticsearch_indexing? && self.searchable?
ElasticIndexerWorker.perform_async( ElasticIndexerWorker.perform_async(
:delete, :delete, self.class.to_s, self.id, self.es_id, es_parent: self.es_parent
self.class.to_s,
self.id,
self.es_id,
es_parent: self.es_parent
) )
end end
end
end
end
class_methods do class_methods do
def __elasticsearch__ def __elasticsearch__
......
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