Rails 异步发送邮件和测试
发布日期:2021-09-29 20:08:31 浏览次数:2 分类:技术文章

本文共 2211 字,大约阅读时间需要 7 分钟。

异步执行在当前(这个东西在rails的领域,变化的可能很大,总是有更好用的插件,更酷的想法冒出来)
[size=large]最流行的应该就是delayed_job[/size],这个的资料比较多,例如,railscast就有介绍。所以,[url=hlee.iteye.com]老猪[/url]这里就简单说说用法。delayed_job异步思路还是,在需要调用的地方加delay,例外启动一个deamon,处理任务。
其中,railscast的介绍已经被JE某XD翻译了,用到的参考[url]http://enn.iteye.com/blog/632960[/url]
原本地址:
[url]http://asciicasts.com/episodes/171-delayed-job[/url]
并该方案当前的Rails 3解决方案JE也有
参考如下:
[url]http://t0uch.iteye.com/blog/673336[/url]
安装:
script/plugin install git://github.com/collectiveidea/delayed_job.git
生成:
script/generate delayed_job
rake db:migrate #后台处理 rake jobs:work
生成表如下:
create_table :delayed_jobs, :force => true do |table|     table.integer  :priority, :default => 0      # Allows some jobs to jump to the front of the queue     table.integer  :attempts, :default => 0      # Provides for retries, but still fail eventually.     table.text     :handler                      # YAML-encoded string of the object that will do work     table.string   :last_error                   # reason for last failure (See Note below)     table.datetime :run_at                       # When to run. Could be Time.now for immediately, or sometime in the future.     table.datetime :locked_at                    # Set when a client is working on this object     table.datetime :failed_at                    # Set when all retries have failed (actually, by default, the record is deleted instead)     table.string   :locked_by                    # Who is working on this object (if locked)     table.timestamps   end
[size=large]另一个知名的解决方案是ar_mailer
[/size]
ar_mailer的时间要长,用起来和delayed job的解决方案差不多,异步的思路把任务存到数据库,然后,后台任务轮询触发。
和Delayed Job相同,也有一篇介绍Rails 3使用的在letrails上
[url]http://www.letrails.cn/archives/ar_mailer-rails-3-plugi[/url]
[url=hlee.iteye.com]老猪[/url]自己当前的项目用的ar_mailer
大致使用如下:
$ sudo gem install adzap-ar_mailer
config.gem "adzap-ar_mailer"   ./script/generate ar_mailer
#config/environments/production.rb   config.action_mailer.delivery_method = :activerecord
启动后台
$ ar_sendmail
[size=large]最后,测试异步的发送邮件[/size]
也是使用一个插件,保证在测试环境邮件并不真正发送,就可以验证了
#$ ./script/plugin install git://github.com/ngty/action_mailer_cache_delivery.git
#RAILS_ROOT/config/environments/test.rb,   config.action_mailer.delivery_method = :cache

转载地址:https://blog.csdn.net/horace_lee/article/details/83706124 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:在单双行显示不同的颜色Rails的一些view helper
下一篇:电子书推荐 Textmate使用的书

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年03月19日 17时59分20秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章