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
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
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.
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.
I use the delayed_job_mailer plugin to accomplish this in Rails 2 apps. Not sure if it works with Rails 3.
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.
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