gpt4 book ai didi

ruby-on-rails - ActionMailer 执行超时

转载 作者:行者123 更新时间:2023-12-01 05:44:40 27 4
gpt4 key购买 nike

尝试向用户发送电子邮件以重置其密码时,我不断收到执行超时错误。其他邮件程序功能有效,所以我知道配置设置是正确的。标题显示:“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

为什么错误消息中的“密码”会导致超时::错误

最佳答案

从主 Controller 请求线程发送电子邮件(或其他长时间运行的进程)不是一个好主意。电子邮件的发送可能因各种不受您控制的原因而超时(例如,出站电子邮件传送服务器已关闭),您不希望您的应用程序服务器和用户因此而受到影响。

更好的方法是使用诸如延迟作业 (DJ) 之类的排队机制来对这些电子邮件任务进行排队,并让它们在您的 Controller 线程之外进行处理。

https://github.com/collectiveidea/delayed_job

将此(或其他排队系统)集成到您的 rails 应用程序中是相当简单的。据说 rails 4 内置了排队服务(我还没有使用)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 的更多信息。

关于ruby-on-rails - ActionMailer 执行超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3009605/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com