简体   繁体   English

Rails + Devise + delayed_job?

[英]Rails + Devise + delayed_job?

I have a Rails 3 App using Devise on Heroku. 我在Heroku上使用Devise有一个Rails 3应用程序。 Problem is I'm sending emails with Sendgrid and email delivery is slow, it makes the app hang. 问题是我正在发送带有Sendgrid的电子邮件,而且电子邮件递送速度很慢,这会让应用程序挂起。 So I'm interested in using delayed_job to queue the email delivery in the background so my app is responsive to the user. 所以我有兴趣使用delayed_job在后台排队电子邮件,因此我的应用程序响应用户。

How can Devise be used with delayed_job? Devise如何与delayed_job一起使用? Any way to setup Devise to use delayed_job? 设置Devise以使用delayed_job的任何方法?

From Robert May: https://gist.github.com/659514 来自Robert May: https//gist.github.com/659514

Add this to a file in config/initializers directory: 将其添加到config/initializers目录中的文件:

module Devise
  module Models
    module Confirmable
      handle_asynchronously :send_confirmation_instructions
    end

    module Recoverable
      handle_asynchronously :send_reset_password_instructions
    end

    module Lockable
      handle_asynchronously :send_unlock_instructions
    end
  end
end

According to the Devise wiki page , the devise-async gem will handle this for you. 根据Devise wiki页面devise-async gem将为您处理此问题。 It supports delayed_job, resque, and sidekiq. 它支持delayed_job,resque和sidekiq。

I found that none of the above worked for me. 我发现以上都不适合我。 I'm using Devise 2.0.4 and Rails 3.2.2 with delayed_job_active_record 0.3.2 我正在使用Devise 2.0.4和Rails 3.2.2以及delayed_job_active_record 0.3.2

The way devise actually talks about doing something like this in the comments in the code is to override the methods in the User class. 设计实际上谈论在代码中的注释中做这样的事情的方式是覆盖User类中的方法。 Thus, I solved it like so, and it works perfectly: 因此,我这样解决了它,它完美地工作:

app/models/User.rb 应用程序/模型/ User.rb

def send_on_create_confirmation_instructions
  Devise::Mailer.delay.confirmation_instructions(self)
end
def send_reset_password_instructions
  Devise::Mailer.delay.reset_password_instructions(self)
end
def send_unlock_instructions
  Devise::Mailer.delay.unlock_instructions(self)
end

this is the thing that worked for me: 这对我有用:

in app/models/user.rb 在app / models / user.rb中

  # after your devise declarations
  handle_asynchronously :send_reset_password_instructions
  handle_asynchronously :send_confirmation_instructions
  handle_asynchronously :send_on_create_confirmation_instructions

you may not need all of them depending on which devise modules you are including 您可能不需要所有这些,具体取决于您所包含的设计模块

I'm not sure Devise provides an easy way to do backgrounding of email delivery. 我不确定Devise提供了一种简单的方法来进行电子邮件传递的后台处理。 However there are ways to be able to do that with DelayedJoy. 但是有一些方法可以使用DelayedJoy来做到这一点。 DelyedJob provides a function "handle_asynchronously" which if injected into the DeviseMailer can ensure that deliveries happen in the background. DelyedJob提供了一个“handle_asynchronously”函数,如果注入DeviseMailer,可以确保在后台发生交付。

Try this in your config/application.rb 在config / application.rb中尝试这个

config.after_initialize do 
  ::DeviseMailer.handle_asynchronously :deliver_confirmation_instructions 
  # or
  ::DeviseMailer.handle_asynchronously :deliver!
end

You will need to experiment with that. 你需要尝试一下。 You could also try to inherit the DeviseMailer and set it to deliver asynchronously in an initializer in config/initializer/devise_mailer_setup.rb or something to that effect. 您还可以尝试继承DeviseMailer并将其设置为在config / initializer / devise_mailer_setup.rb中的初始化程序中异步传递,或者为此。

For delayed_job_active_record with Devise , just use the following . 对于使用Devise的 delayed_job_active_record ,只需使用以下内容即可。 (add it to user.rb model ) (将其添加到user.rb model

handle_asynchronously :send_devise_notification, :queue => 'devise'

Not sure why you need to authenticate any of the delayed_job tasks. 不确定为什么需要验证任何delayed_job任务。 Just have the Devise authenticated controller actions queue the email delivery to a library method. 只需让Devise认证的控制器操作将电子邮件传递排队到库方法。

I use the delayed_job_mailer plugin to accomplish this in Rails 2 apps. 我使用delayed_job_mailer插件在Rails 2应用程序中完成此操作。 Not sure if it works with Rails 3. 不确定它是否适用于Rails 3。

I have generally found it best to avoid any and all background-job task managers available for Ruby and Rails, period. 我一般认为最好避免任何和所有后台作业任务管理器可用于Ruby和Rails期间。 They suck. 他们很糟糕。

Instead, I use crontab which is an ancient program and very good at its job. 相反,我使用crontab,这是一个古老的程序,非常擅长它的工作。 Just make a script to do your dirty work every so often and tell crontab to run it at the right times using rails runner. 只需制作一个脚本来经常进行脏工作,并告诉crontab使用rails runner在正确的时间运行它。 This keeps those pesky long-running tasks outside the run-time of the rest of your Rails application, but you still have access to the database and the whole Rails stack if you want/need it. 这使得那些讨厌的长时间运行任务超出了Rails应用程序其余部分的运行时间,但是如果你想要/需要它,你仍然可以访问数据库和整个Rails堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM