My previous deep dive into the Rails 4.0 Queueing system was motivated by a patch to Rails I was working on while at RailsCamp New England this past weekend. I'm happy to say that Rails 4.0 now has an optional asynchronous ActionMailer.
The API for pushing your emails to the background is very simple. If you want to make this change application wide simply set it in your application.rb (or in any of the environment files)
self.async = true
endThat's it! Any messages that are being delivered will be sent as a background job. In fact, the rendering is happening on the background as well.
You will need to take care that the arguments you are passing your mailers can be properly marshalled. Instead of:
def welcome(id)
@user = User.find(id)
...
end
end
The API for pushing your emails to the background is very simple. If you want to make this change application wide simply set it in your application.rb (or in any of the environment files)
CODE:
config.action_mailer.async = trueOr if you want to only make specific mailers asynchrounousCODE:
class WelcomeMailer < ActionMailer::Baseself.async = true
endThat's it! Any messages that are being delivered will be sent as a background job. In fact, the rendering is happening on the background as well.
You will need to take care that the arguments you are passing your mailers can be properly marshalled. Instead of:
CODE:
WelcomeMailer.welcome(@user).deliverYou should do:CODE:
WelcomeMailer.welcome(@user.id).deliverThen in your mailer:CODE:
class WelcomeMailer < ActionMailer::Basedef welcome(id)
@user = User.find(id)
...
end
end
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/301743/viewspace-734050/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/301743/viewspace-734050/