From 877a55a6148fe036129fc2a5708afbfb46a607c1 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 24 Feb 2013 19:15:54 +0100 Subject: [PATCH] Avoid sending mails twice via concurrent background processes. --- app/models/message.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/models/message.rb b/app/models/message.rb index ddbf16c9..5311afbc 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -64,6 +64,9 @@ class Message < ActiveRecord::Base # Sends all pending messages that are to be send as emails. def self.send_emails 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 recipient in message.recipients if recipient.settings['messages.sendAsEmail'] == "1" && !recipient.email.blank? @@ -74,7 +77,7 @@ class Message < ActiveRecord::Base end end end - message.update_attribute(:email_state, 1) + message.update_attribute(:email_state, 1) # Set email state to 'sent' end end end