当尝试向用户发送电子邮件以重置其密码时,我不断收到执行超时错误。其他邮件功能正常工作,所以我知道配置设置是正确的。标题为:"Timeout::Error in Password resetsController#create“
下面是password_resets_controller:
def create
@user = User.find_by_email(params[:email])
if @user
User.deliver_password_reset_instructions(@user.id)
flash[:notice] = "Instructions to reset your password have been emailed to you. " +
"Please check your email."
redirect_to '/'
else
flash[:notice] = "No user was found with that email address"
render :action => :new
end
end下面是User.rb内部的方法
def self.deliver_password_reset_instructions(user_id)
user = User.find(user_id)
user.reset_perishable_token!
Emailer.deliver_password_reset_instructions(user)
end最后,下面是emailer.rb内部的实际方法:
default_url_options[:host] = "http://0.0.0.0:3000" #development
def password_reset_instructions(user)
@subject = "Application Password Reset"
@from = 'Notice@myApp.com'
@recipients = user.email
@sent_on = Time.now
@body["edit_password_reset_url"] = edit_password_reset_url(user.perishable_token)
@headers["X-SMTPAPI"] = "{\"category\" : \"Password Recovery\"}"#send grid category header
end为什么错误消息中的"Password“是指导致超时::error
发布于 2013-10-05 15:34:04
从主控制器请求线程发送电子邮件(或其他长时间运行的进程)不是一个好主意。发送电子邮件可能会因为各种不受您控制的原因而超时(例如,出站电子邮件传送服务器关闭),您不希望应用程序服务器和用户因此而受到影响。
更好的方法是使用像延迟作业(DJ)这样的排队机制来对这些电子邮件任务进行排队,并让它们在控制器线程之外进行处理。
请参阅https://github.com/collectiveidea/delayed_job
将这个(或另一个排队系统)集成到您的rails应用程序中相当简单。据说Rails4内置了队列服务(我还没用过) http://blog.remarkablelabs.com/2012/12/asynchronous-action-mailer-rails-4-countdown-to-2013。
例如,如果您在应用程序中使用DJ,则新代码将如下所示
def self.deliver_password_reset_instructions(user_id)
user = User.find(user_id)
user.reset_perishable_token!
# this is the only line that changes
Emailer.delay.deliver_password_reset_instructions(user)
end这些作业存储在数据库中,并在发生诸如超时之类的错误时重试。
你可以在github页面上阅读更多关于DJ的信息。
https://stackoverflow.com/questions/3009605
复制相似问题