Try to address the concurrency problem

This commit is contained in:
Hugo Peixoto 2023-04-05 23:04:48 +01:00
parent eb8f60192f
commit 3fa1ac101c
2 changed files with 12 additions and 4 deletions

View File

@ -104,8 +104,12 @@ class Member < ApplicationRecord
end end
def self.regenerate_all_notifications def self.regenerate_all_notifications
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute('LOCK members IN ACCESS EXCLUSIVE MODE')
Member.all.each do |member| Member.all.each do |member|
member.regenerate_notifications member.regenerate_notifications
end end
end end
end end
end

View File

@ -4,10 +4,14 @@ class Notification < ApplicationRecord
scope :scheduled_for_today, ->() { where(status: 'scheduled', to_be_sent_on: Date.today) } scope :scheduled_for_today, ->() { where(status: 'scheduled', to_be_sent_on: Date.today) }
def self.send_scheduled_for_today def self.send_scheduled_for_today
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute('LOCK members IN ACCESS EXCLUSIVE MODE')
scheduled_for_today.each do |n| scheduled_for_today.each do |n|
n.deliver! n.deliver!
end end
end end
end
def deliver! def deliver!
# actually send the email. # actually send the email.