使用 Ruby on Rails 安排发送电子邮件任务的最佳方法是什么?

问题描述:

我想安排一项每日任务:每天早上 7 点,我想发送一封电子邮件(无需人工干预).

I would like to schedule a daily task : every day at 7 AM, I want an email to be sent (without human intervention).

我正在研究 RoR 框架,我想知道什么是最好的方法?

I'm working on the RoR framework and I'm wondering what is the best way to do that?

我听说过 BackgrounDRB、OpenWFEru 调度程序或基于 Cron 的东西,但我是新手,不知道哪一个适合我的需要.

I've heard about BackgrounDRB, OpenWFEru scheduler or things based on Cron, but I'm a newbie and don't understand which one is made for my need.

另一种选择是创建由 cron 作业运行的 rake 任务.为此,请创建一个文件 some_file.rake 并将其放在您的 lib/tasks 文件夹中.您的文件可能如下所示:

Another option is to create a rake task that is run by a cron job. To do this, create a file some_file.rake and put it in your lib/tasks folder. Your file might look like this:

Rails 2.x:

task :send_daily_mail, :needs => :environment do
    Model.send_daily_mail
end

Rails 3.x:

task :send_daily_mail => :environment do
    Model.send_daily_mail
end

然后根据需要使用 cron 执行它:

Then use cron to execute it as often as you like:

cd /path/to/app && /usr/bin/rake send_daily_mail

请注意,如果您的应用默认处于开发模式,您可能需要将 RAILS_ENV=production 放在您的 crontab 中.

Note you might need to put RAILS_ENV=production in your crontab if your app is in development mode by default.