Rails + Devise + delayed_job?

后端 未结 9 1918
长情又很酷
长情又很酷 2020-12-24 07:38

I have a Rails 3 App using Devise on Heroku. Problem is I\'m sending emails with Sendgrid and email delivery is slow, it makes the app hang. So I\'m interested in using dela

相关标签:
9条回答
  • 2020-12-24 08:19

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

    Add this to a file in config/initializers directory:

    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
    
    0 讨论(0)
  • 2020-12-24 08:25

    I have generally found it best to avoid any and all background-job task managers available for Ruby and Rails, period. They suck.

    Instead, I use crontab which is an ancient program and very good at its job. 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. 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.

    0 讨论(0)
  • 2020-12-24 08:27

    Not sure why you need to authenticate any of the delayed_job tasks. Just have the Devise authenticated controller actions queue the email delivery to a library method.

    0 讨论(0)
  • 2020-12-24 08:28

    I use the delayed_job_mailer plugin to accomplish this in Rails 2 apps. Not sure if it works with Rails 3.

    0 讨论(0)
  • 2020-12-24 08:29

    I'm not sure Devise provides an easy way to do backgrounding of email delivery. However there are ways to be able to do that with DelayedJoy. DelyedJob provides a function "handle_asynchronously" which if injected into the DeviseMailer can ensure that deliveries happen in the background.

    Try this in your 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.

    0 讨论(0)
  • 2020-12-24 08:31

    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

    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. Thus, I solved it like so, and it works perfectly:

    app/models/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
    
    0 讨论(0)
提交回复
热议问题