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

时间:2023-01-16 10:28:43

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

我想安排一项日常任务:每天早上7点,我都希望发送一封电子邮件(无人为干预)。

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

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

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.

我听说过BackgrounDRB,OpenWFEru调度程序或基于Cron的东西,但我是新手,不明白哪一个是为我的需要而制作的。

5 个解决方案

#1


12  

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:

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

Rails 2.x:

Rails 2.x:

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

Rails 3.x:

Rails 3.x:

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

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

然后使用cron随意执行它:

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

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

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

#2


7  

I was impressed by (and plan to try) the rufus-scheduler gem discussed in this blog post

我对此博客文章中讨论的rufus-scheduler gem印象深刻(并计划尝试)

He describes something like this:

他描述了这样的事情:

scheduler = Rufus::Scheduler.start_new  

scheduler.every("1m") do  
   DailyDigest.send_digest!  
end 

..which seems pretty simple. I wonder how easy it would be to add HTML-based configuration?

..这似乎很简单。我想知道添加基于HTML的配置有多容易吗?

#3


2  

BackgroundRB is what I use and it works perfect. I have several emails being sent, generated by BackgroundRB. I also have other tasks as well. Because it enables both scheduled tasks and asynchronous tasks (tasks that take longer than the normal client/server response cycle).

BackgroundRB是我使用的,它完美无缺。我发送了几封由BackgroundRB生成的电子邮件。我也有其他任务。因为它支持计划任务和异步任务(比正常客户端/服务器响应周期花费更长时间的任务)。

I use it and I am very happy with it.

我用它,我很高兴。

#4


1  

Add a class method to one of your models that will handle this for you. Now try to execute that method using the runner script

将类方法添加到您将为您处理此问题的模型之一。现在尝试使用runner脚本执行该方法

./script/runner "MyModel.send_daily_mail" RAILS_ENV=production

Ensure everything works ok. If it does, then we need to make the command work universally by setting up the path to the project properly.

确保一切正常。如果是这样,那么我们需要通过正确设置项目的路径来使命令普遍运行。

cd /path/to/my/rails/project && ./script/runner "MyModel.send_daily_mail" RAILS_ENV=production

Now change to any random directiry and run that command. If it runs properly, run crontab -e and insert the command in there setup to run daily at 7AM. There are a ton of explanation about the cron format on there if you google for them and should be pretty simple to figure out.

现在更改为任何随机方向并运行该命令。如果它正常运行,请运行crontab -e并在该设置中插入命令,以便每天早上7点运行。如果你谷歌的话,那里有关于cron格式的大量解释,应该很容易理解。

#5


0  

Go with a rake task and cron job, as the accepted answer already says. However, note that, updating the cron file itself is a manual task. That may be fine if you are not changing it during development. Otherwise, here how you can let Capistrano do it for you: http://push.cx/2008/deploying-crontab-with-your-rails-app

接受rake任务和cron工作,正如已接受的答案已经说明的那样。但请注意,更新cron文件本身是一项手动任务。如果您在开发过程中没有更改它,那可能没问题。否则,在这里你可以让Capistrano为你做这些:http://push.cx/2008/deploying-crontab-with-your-rails-app

#1


12  

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:

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

Rails 2.x:

Rails 2.x:

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

Rails 3.x:

Rails 3.x:

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

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

然后使用cron随意执行它:

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

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

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

#2


7  

I was impressed by (and plan to try) the rufus-scheduler gem discussed in this blog post

我对此博客文章中讨论的rufus-scheduler gem印象深刻(并计划尝试)

He describes something like this:

他描述了这样的事情:

scheduler = Rufus::Scheduler.start_new  

scheduler.every("1m") do  
   DailyDigest.send_digest!  
end 

..which seems pretty simple. I wonder how easy it would be to add HTML-based configuration?

..这似乎很简单。我想知道添加基于HTML的配置有多容易吗?

#3


2  

BackgroundRB is what I use and it works perfect. I have several emails being sent, generated by BackgroundRB. I also have other tasks as well. Because it enables both scheduled tasks and asynchronous tasks (tasks that take longer than the normal client/server response cycle).

BackgroundRB是我使用的,它完美无缺。我发送了几封由BackgroundRB生成的电子邮件。我也有其他任务。因为它支持计划任务和异步任务(比正常客户端/服务器响应周期花费更长时间的任务)。

I use it and I am very happy with it.

我用它,我很高兴。

#4


1  

Add a class method to one of your models that will handle this for you. Now try to execute that method using the runner script

将类方法添加到您将为您处理此问题的模型之一。现在尝试使用runner脚本执行该方法

./script/runner "MyModel.send_daily_mail" RAILS_ENV=production

Ensure everything works ok. If it does, then we need to make the command work universally by setting up the path to the project properly.

确保一切正常。如果是这样,那么我们需要通过正确设置项目的路径来使命令普遍运行。

cd /path/to/my/rails/project && ./script/runner "MyModel.send_daily_mail" RAILS_ENV=production

Now change to any random directiry and run that command. If it runs properly, run crontab -e and insert the command in there setup to run daily at 7AM. There are a ton of explanation about the cron format on there if you google for them and should be pretty simple to figure out.

现在更改为任何随机方向并运行该命令。如果它正常运行,请运行crontab -e并在该设置中插入命令,以便每天早上7点运行。如果你谷歌的话,那里有关于cron格式的大量解释,应该很容易理解。

#5


0  

Go with a rake task and cron job, as the accepted answer already says. However, note that, updating the cron file itself is a manual task. That may be fine if you are not changing it during development. Otherwise, here how you can let Capistrano do it for you: http://push.cx/2008/deploying-crontab-with-your-rails-app

接受rake任务和cron工作,正如已接受的答案已经说明的那样。但请注意,更新cron文件本身是一项手动任务。如果您在开发过程中没有更改它,那可能没问题。否则,在这里你可以让Capistrano为你做这些:http://push.cx/2008/deploying-crontab-with-your-rails-app