Avoid sending mails twice via concurrent background processes.

This commit is contained in:
Benjamin Meichsner 2013-02-24 19:15:54 +01:00
parent fe632abf2c
commit 877a55a614

View file

@ -64,6 +64,9 @@ class Message < ActiveRecord::Base
# Sends all pending messages that are to be send as emails. # Sends all pending messages that are to be send as emails.
def self.send_emails def self.send_emails
messages = Message.pending messages = Message.pending
# Set all messages not pending, to avoid sending mails twice with different background processes
messages.each { |m| m.update_attribute(:email_state, 2) }
for message in messages for message in messages
for recipient in message.recipients for recipient in message.recipients
if recipient.settings['messages.sendAsEmail'] == "1" && !recipient.email.blank? if recipient.settings['messages.sendAsEmail'] == "1" && !recipient.email.blank?
@ -74,7 +77,7 @@ class Message < ActiveRecord::Base
end end
end end
end end
message.update_attribute(:email_state, 1) message.update_attribute(:email_state, 1) # Set email state to 'sent'
end end
end end
end end