[deploy] Optimization:Index Chat Channel Memberships Async on Message Creation (#9491)

This commit is contained in:
Molly Struve 2020-07-24 09:52:42 -05:00 committed by GitHub
parent cd11ebefb9
commit 330009995d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 2 deletions

View file

@ -27,7 +27,7 @@ class Message < ApplicationRecord
def update_chat_channel_last_message_at
chat_channel.touch(:last_message_at)
chat_channel.chat_channel_memberships.each(&:index_to_elasticsearch)
ChatChannels::IndexesMembershipsWorker.perform_async(chat_channel.id)
end
def update_all_has_unopened_messages_statuses
@ -195,7 +195,7 @@ class Message < ApplicationRecord
def channel_permission
errors.add(:base, "Must be part of channel.") if chat_channel_id.blank?
channel = ChatChannel.find(chat_channel_id)
channel = chat_channel || ChatChannel.find(chat_channel_id)
return if channel.open?
errors.add(:base, "You are not a participant of this chat channel.") unless channel.has_member?(user)

View file

@ -0,0 +1,12 @@
module ChatChannels
class IndexesMembershipsWorker
include Sidekiq::Worker
sidekiq_options queue: :high_priority, lock: :until_executing
def perform(chat_channel_id)
chat_channel = ChatChannel.find(chat_channel_id)
chat_channel.chat_channel_memberships.each(&:index_to_elasticsearch)
end
end
end

View file

@ -143,4 +143,15 @@ RSpec.describe Message, type: :model do
end.to change(EmailMessage, :count).by(0)
end
end
describe "#after_create" do
it "enqueues ChatChannels::IndexesMembershipsWorker" do
chat_channel.add_users([user])
allow(ChatChannels::IndexesMembershipsWorker).to receive(:perform_async)
create(:message, chat_channel: chat_channel, user: user)
expect(ChatChannels::IndexesMembershipsWorker).to have_received(:perform_async)
end
end
end

View file

@ -0,0 +1,18 @@
require "rails_helper"
RSpec.describe ChatChannels::IndexesMembershipsWorker, type: :worker do
describe "#perform" do
let(:chat_channel) { create(:chat_channel) }
let(:chat_channel_membership) { create(:chat_channel_membership) }
it "indexes chat channel memberships" do
allow(ChatChannel).to receive(:find).and_return(chat_channel)
allow(chat_channel).to receive(:chat_channel_memberships).and_return([chat_channel_membership])
allow(chat_channel_membership).to receive(:index_to_elasticsearch)
described_class.new.perform(chat_channel.id)
expect(chat_channel_membership).to have_received(:index_to_elasticsearch)
end
end
end