Update Exchange touch on Enterprise to use touch_later

This can improve issues with deadlocks where multiple touch calls are triggered in a single update, for example where an exchange touches it's parent enterprise on save, and we do this:

oc_with_lots_of_exchanges.exchanges.each{|ex| ex.clone! }
This commit is contained in:
Matt-Yorkley
2021-03-26 12:44:50 +00:00
parent 5b3fd25a78
commit 45a06a300e

View File

@@ -12,7 +12,7 @@ class Exchange < ActiveRecord::Base
belongs_to :order_cycle
belongs_to :sender, class_name: 'Enterprise'
belongs_to :receiver, class_name: 'Enterprise', touch: true
belongs_to :receiver, class_name: 'Enterprise'
has_many :exchange_variants, dependent: :destroy
has_many :variants, through: :exchange_variants
@@ -23,6 +23,8 @@ class Exchange < ActiveRecord::Base
validates :order_cycle, :sender, :receiver, presence: true
validates :sender_id, uniqueness: { scope: [:order_cycle_id, :receiver_id, :incoming] }
after_save :touch_receiver
accepts_nested_attributes_for :variants
scope :in_order_cycle, lambda { |order_cycle| where(order_cycle_id: order_cycle) }
@@ -95,4 +97,10 @@ class Exchange < ActiveRecord::Base
def participant
incoming? ? sender : receiver
end
def touch_receiver
return unless receiver&.persisted?
receiver.touch_later
end
end