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
def self.regenerate_all_notifications
Member.all.each do |member|
member.regenerate_notifications
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute('LOCK members IN ACCESS EXCLUSIVE MODE')
Member.all.each do |member|
member.regenerate_notifications
end
end
end
end

View File

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