From c10602f4e614228a386bd6a045bbc338f7d8d784 Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Tue, 12 May 2020 14:39:16 -0500 Subject: [PATCH] [deploy] Delay RemoveFromIndex Job to Fix Callback Race Conditions (#7800) --- app/models/comment.rb | 29 +++++++++++++++++------------ app/models/concerns/searchable.rb | 5 ++++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index f30834f3c..acadecebc 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -22,32 +22,37 @@ class Comment < ApplicationRecord has_many :notifications, as: :notifiable, inverse_of: :notifiable, dependent: :delete_all has_many :notification_subscriptions, as: :notifiable, inverse_of: :notifiable, dependent: :destroy + before_validation :evaluate_markdown, if: -> { body_markdown } + validate :permissions, if: :commentable validates :body_markdown, presence: true, length: { in: BODY_MARKDOWN_SIZE_RANGE } validates :body_markdown, uniqueness: { scope: BODY_MARKDOWN_UNIQUENESS_SCOPES } validates :commentable_id, presence: true validates :commentable_type, inclusion: { in: COMMENTABLE_TYPES } validates :user_id, presence: true + before_create :adjust_comment_parent_based_on_depth + before_save :set_markdown_character_count, if: :body_markdown + after_create :notify_slack_channel_about_warned_users after_create :after_create_checks after_create_commit :record_field_test_event - after_commit :calculate_score, on: %i[create update] - after_update_commit :update_notifications, if: proc { |comment| comment.saved_changes.include? "body_markdown" } - after_save :bust_cache - after_save :synchronous_bust - after_destroy :after_destroy_actions - before_destroy :before_destroy_actions after_create_commit :send_email_notification, if: :should_send_email_notification? after_create_commit :create_first_reaction after_create_commit :send_to_moderator + + after_commit :calculate_score, on: %i[create update] after_commit :index_to_elasticsearch, on: %i[create update] + + after_save :bust_cache + after_save :synchronous_bust + + after_update :remove_notifications, if: :deleted + after_update :update_descendant_notifications, if: :deleted + after_update_commit :update_notifications, if: proc { |comment| comment.saved_changes.include? "body_markdown" } + + after_destroy :after_destroy_actions + before_destroy :before_destroy_actions after_commit :remove_from_elasticsearch, on: [:destroy] - before_save :set_markdown_character_count, if: :body_markdown - before_create :adjust_comment_parent_based_on_depth - after_update :remove_notifications, if: :deleted - after_update :update_descendant_notifications, if: :deleted - before_validation :evaluate_markdown, if: -> { body_markdown } - validate :permissions, if: :commentable scope :eager_load_serialized_data, -> { includes(:user, :commentable) } diff --git a/app/models/concerns/searchable.rb b/app/models/concerns/searchable.rb index 555ff0fe0..fb0dc774d 100644 --- a/app/models/concerns/searchable.rb +++ b/app/models/concerns/searchable.rb @@ -12,7 +12,10 @@ module Searchable end def remove_from_elasticsearch - Search::RemoveFromIndexWorker.perform_async(self.class::SEARCH_CLASS.to_s, search_id) + # Callbacks can cause index and removal jobs to be enqueued at the same time + # to avoid indexing a document after removing it we delay the removal job by 5 seconds to + # ensure it is run last + Search::RemoveFromIndexWorker.perform_in(5.seconds, self.class::SEARCH_CLASS.to_s, search_id) end def serialized_search_hash