Skip to content

Commit

Permalink
Fix deadlock, remove Honeybadger
Browse files Browse the repository at this point in the history
  • Loading branch information
jcraigk committed Nov 15, 2023
1 parent 183ca63 commit 34c5a1e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/controllers/oauth/sorcery_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def callback
return redirect_back_or_to(dashboard_path) if login_from(params[:provider])
create_user_and_login
rescue StandardError => e
Honeybadger.notify(e) if defined?(Honeybadger)
Rails.logger.error(e)
redirect_to login_path, alert: t('auth.external_fail', provider: provider_title)
end

Expand Down
3 changes: 1 addition & 2 deletions app/services/base/subteam_sync_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def find_or_create_subteam(attrs) # rubocop:disable Metrics/MethodLength
Subteam.create!(combined_attrs)
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique => e
parameters = { attrs: attrs.to_h, combined_attrs: }
Honeybadger.notify(e, parameters:) if defined?(Honeybadger)
Rails.logger.error(e)
nil
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/event_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def call
return post_success_message if respond_in_chat?
delete_slack_ack_message if slack_fast_acked?
rescue StandardError => e
Honeybadger.notify(e) if defined?(Honeybadger) && reportable?(e)
Rails.logger.error(e) if reportable?(e)
post_error_message(e)
end

Expand Down
2 changes: 0 additions & 2 deletions app/services/slack/post_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ def alt_text
def respond_dm(to_profile_rid)
post_params = message_params(to_profile_rid).merge(as_user: true)
slack_client.chat_postMessage(post_params)
rescue Slack::Web::Api::Errors::InvalidBlocks => e
Honeybadger.notify(e, parameters: post_params)
end

def respond_ephemeral(to_profile_rid)
Expand Down
43 changes: 24 additions & 19 deletions app/services/tip_outcome_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,48 @@ def call

def update_profile_and_team_stats
Tip.transaction do
lock_records_for_transaction

update_to_profiles
update_from_profile
update_team
tips.map(&:destroy) if destroy
end
end

# Lock profiles in a consistent order to reduce deadlocks
def lock_records_for_transaction
[team, from_profile, *to_profiles].sort_by(&:id).each(&:lock!)
end

def to_profiles
@to_profiles ||= tips.filter_map(&:to_profile).uniq
end

# rubocop:disable Metrics/AbcSize
def update_to_profiles
tips.each do |tip|
profile = tip.to_profile
profile.with_lock do
last_tip_received_at = destroy ? previous_received_at(profile) : tip.created_at
value_col = tip.jab? ? :jabs_received : :points_received
value = profile.send(value_col).send(operator, tip.quantity.abs)
balance = profile.balance.send(operator, tip.quantity)
profile.update!(value_col => value, balance:, last_tip_received_at:)
end
last_tip_received_at = destroy ? previous_received_at(profile) : tip.created_at
value_col = tip.jab? ? :jabs_received : :points_received
value = profile.send(value_col).send(operator, tip.quantity.abs)
balance = profile.balance.send(operator, tip.quantity)
profile.update!(value_col => value, balance:, last_tip_received_at:)
end
end

def update_from_profile
from_profile.with_lock do
points_sent = from_profile.points_sent.send(operator, total_points)
jabs_sent = from_profile.jabs_sent.send(operator, total_jabs)
last_tip_sent_at = destroy ? previous_sent_at : tips.first.created_at
from_profile.update!(points_sent:, jabs_sent:, last_tip_sent_at:)
end
points_sent = from_profile.points_sent.send(operator, total_points)
jabs_sent = from_profile.jabs_sent.send(operator, total_jabs)
last_tip_sent_at = destroy ? previous_sent_at : tips.first.created_at
from_profile.update!(points_sent:, jabs_sent:, last_tip_sent_at:)
end

def update_team
team.with_lock do
points_sent = team.points_sent.send(operator, total_points)
jabs_sent = team.jabs_sent.send(operator, total_jabs)
balance = team.balance.send(operator, total_points - total_jabs)
team.update!(points_sent:, jabs_sent:, balance:)
end
points_sent = team.points_sent.send(operator, total_points)
jabs_sent = team.jabs_sent.send(operator, total_jabs)
balance = team.balance.send(operator, total_points - total_jabs)
team.update!(points_sent:, jabs_sent:, balance:)
end
# rubocop:enable Metrics/AbcSize

Expand Down

0 comments on commit 34c5a1e

Please sign in to comment.