Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Handle conversation participation creation race condition error #9449

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/listeners/participation_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ class ParticipationListener < BaseListener

def assignee_changed(event)
conversation, _account = extract_conversation_and_account(event)
conversation.conversation_participants.find_or_create_by!(user_id: conversation.assignee_id) if conversation.assignee_id.present?
return if conversation.assignee_id.blank?

conversation.conversation_participants.find_or_create_by!(user_id: conversation.assignee_id)
# We have observed race conditions triggering these errors
# example: Assignment happening via automation, while auto assignment is also configured.
rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid
Rails.logger.warn "Failed to create conversation participant for account #{conversation.account.id} " \
": user #{conversation.assignee_id} : conversation #{conversation.id}"
end
end
2 changes: 1 addition & 1 deletion app/models/concerns/assignment_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def notify_assignment_change
ASSIGNEE_CHANGED => -> { saved_change_to_assignee_id? },
TEAM_CHANGED => -> { saved_change_to_team_id? }
}.each do |event, condition|
condition.call && dispatcher_dispatch(event)
condition.call && dispatcher_dispatch(event, previous_changes)
end
end

Expand Down
16 changes: 14 additions & 2 deletions spec/listeners/participation_listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

before do
create(:inbox_member, inbox: inbox, user: agent)
Current.user = nil
Current.account = nil
end

describe '#assignee_changed' do
Expand All @@ -22,5 +20,19 @@
listener.assignee_changed(event)
expect(conversation.conversation_participants.map(&:user_id)).to include(agent.id)
end

it 'does not fail if the conversation participant already exists' do
conversation.conversation_participants.create!(user: agent)
expect { listener.assignee_changed(event) }.not_to raise_error
end

it 'logs a debug message if participant save fails due to a race condition' do
allow(Rails.logger).to receive(:warn)
allow(conversation).to receive(:conversation_participants).and_return(double)
allow(conversation.conversation_participants).to receive(:find_or_create_by!).and_raise(ActiveRecord::RecordNotUnique)
expect { listener.assignee_changed(event) }.not_to raise_error
expect(Rails.logger).to have_received(:warn).with('Failed to create conversation participant for account ' \
"#{account.id} : user #{agent.id} : conversation #{conversation.id}")
end
end
end
2 changes: 1 addition & 1 deletion spec/models/conversation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
changed_attributes: nil, performed_by: nil)
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
.with(described_class::ASSIGNEE_CHANGED, kind_of(Time), conversation: conversation, notifiable_assignee_change: true,
changed_attributes: nil, performed_by: nil)
changed_attributes: changed_attributes, performed_by: nil)
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
.with(described_class::CONVERSATION_UPDATED, kind_of(Time), conversation: conversation, notifiable_assignee_change: true,
changed_attributes: changed_attributes, performed_by: nil)
Expand Down