mails are now sent in the background

This commit is contained in:
jomo
2014-05-01 23:00:46 +02:00
parent c85364fdd1
commit fea0e3ab25
4 changed files with 58 additions and 30 deletions

View File

@@ -98,13 +98,14 @@ class UsersController < ApplicationController
is_idiot = false is_idiot = false
end end
begin begin
# these shouldn't be send in the background
RedstonerMailer.register_mail(@user, is_idiot).deliver RedstonerMailer.register_mail(@user, is_idiot).deliver
RedstonerMailer.register_info_mail(@user, is_idiot).deliver RedstonerMailer.register_info_mail(@user, is_idiot).deliver
rescue => e rescue => e
puts "---" Rails.logger.error "---"
puts "WARNING: registration mail failed for user #{@user.name}, #{@user.email}" Rails.logger.error "WARNING: registration mail failed for user #{@user.name}, #{@user.email}"
puts e.message Rails.logger.error e.message
puts "---" Rails.logger.error "---"
flash[:alert] = "Registration mail failed. Please contact us in-game." flash[:alert] = "Registration mail failed. Please contact us in-game."
end end
flash[:notice] = "Successfully signed up! Check your email!" flash[:notice] = "Successfully signed up! Check your email!"

View File

@@ -0,0 +1,22 @@
module MailerHelper
def background_mailer(mails)
Thread.new do
begin
mails.each do |mail|
begin
mail.deliver
rescue => e
Rails.logger.error "---"
Rails.logger.error "WARNING: '#{mail.try(:subject)}' failed for user #{@user.name}, #{@user.email}"
Rails.logger.error e.message
Rails.logger.error "---"
end
end
ensure
# threads open their own DB connection
ActiveRecord::Base.connection.close
Rails.logger.flush
end
end
end
end

View File

@@ -1,4 +1,7 @@
class Threadreply < ActiveRecord::Base class Threadreply < ActiveRecord::Base
include MailerHelper
belongs_to :forumthread belongs_to :forumthread
belongs_to :user_author, class_name: "User", foreign_key: "user_author_id" belongs_to :user_author, class_name: "User", foreign_key: "user_author_id"
belongs_to :user_editor, class_name: "User", foreign_key: "user_editor_id" belongs_to :user_editor, class_name: "User", foreign_key: "user_editor_id"
@@ -34,23 +37,25 @@ class Threadreply < ActiveRecord::Base
# thread + replies # thread + replies
(thread.replies.to_a << thread).each do |post| (thread.replies.to_a << thread).each do |post|
# don't send mail to the user who wrote this # don't send mail to the user who wrote this, don't send to banned/disabled users
if post.author != author # && user.send_threadreply_mail (TODO) if post.author != author && post.author.normal? # && user.send_threadreply_mail (TODO)
userids << post.author.id userids << post.author.id
end end
end end
# making sure we don't send multiple mails to the same user # making sure we don't send multiple mails to the same user
userids.uniq! userids.uniq!
mails = []
userids.each do |uid| userids.each do |uid|
begin begin
RedstonerMailer.thread_reply_mail(User.find(uid), self).deliver mails << RedstonerMailer.thread_reply_mail(User.find(uid), self)
rescue => e rescue => e
puts "---" Rails.logger.error "---"
puts "WARNING: registration mail failed for user #{@user.name}, #{@user.email}" Rails.logger.error "WARNING: Failed to create thread_reply mail (view) for reply#: #{@self.id}, user: #{@user.name}, #{@user.email}"
puts e.message Rails.logger.error e.message
puts "---" Rails.logger.error "---"
end end
end end
background_mailer(mails)
end end
end end

View File

@@ -92,10 +92,10 @@ class User < ActiveRecord::Base
return http.post(uri.request_uri, payload.to_json, "Content-Type" => "application/json").code == "200" return http.post(uri.request_uri, payload.to_json, "Content-Type" => "application/json").code == "200"
end end
rescue => e rescue => e
puts "---" Rails.logger.error "---"
puts "ERROR: failed to check mc password for '#{self.uuid}'. Login servers down?" Rails.logger.error "ERROR: failed to check mc password for '#{self.uuid}'. Login servers down?"
puts e.message Rails.logger.error e.message
puts "---" Rails.logger.error "---"
return false return false
end end
end end
@@ -111,16 +111,16 @@ class User < ActiveRecord::Base
# # user doesn't exist # # user doesn't exist
# return false # return false
# else # else
# puts "---" # Rails.logger.error "---"
# puts "ERROR: unexpected response code while checking '#{self.uuid}' for premium account" # Rails.logger.error "ERROR: unexpected response code while checking '#{self.uuid}' for premium account"
# puts "code: #{reponse.status}, body: '#{reponse.read}'" # Rails.logger.error "code: #{reponse.status}, body: '#{reponse.read}'"
# puts "---" # Rails.logger.error "---"
# end # end
# rescue => e # rescue => e
# puts "---" # Rails.logger.error "---"
# puts "ERROR: failed to check for premium account for '#{self.uuid}'. Minecraft servers down?" # Rails.logger.error "ERROR: failed to check for premium account for '#{self.uuid}'. Minecraft servers down?"
# puts e.message # Rails.logger.error e.message
# puts "---" # Rails.logger.error "---"
# end # end
# # mojang servers have trouble # # mojang servers have trouble
# return true # return true
@@ -132,9 +132,9 @@ class User < ActiveRecord::Base
# skin = http.get("/MinecraftSkins/#{CGI.escape(ign)}.png") # skin = http.get("/MinecraftSkins/#{CGI.escape(ign)}.png")
# http.finish # http.finish
# rescue # rescue
# puts "---" # Rails.logger.error "---"
# puts "ERROR: failed to get skin status code for '#{ign}'. Skin servers down?" # Rails.logger.error "ERROR: failed to get skin status code for '#{ign}'. Skin servers down?"
# puts "---" # Rails.logger.error "---"
# end # end
# skin.code != "404" # skin.code != "404"
# end # end
@@ -159,10 +159,10 @@ class User < ActiveRecord::Base
end end
end end
rescue => e rescue => e
puts "----" Rails.logger.error "----"
puts "Failed to get mojang profile for #{self.ign}" Rails.logger.error "Failed to get mojang profile for #{self.ign}"
puts e.message Rails.logger.error e.message
puts "----" Rails.logger.error "----"
return nil return nil
end end
end end