Rails 异步发送邮件和测试

异步执行在当前(这个东西在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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值