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

发布于 2024-07-14 05:43:00 字数 159 浏览 6 评论 0原文

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

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

愿与i 2024-07-21 05:43:00

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

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 来执行它,只要您愿意:

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

请注意,如果您的应用程序默认处于开发模式。

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

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

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.

指尖上得阳光 2024-07-21 05:43:00

我对讨论的 rufus-scheduler gem 印象深刻(并计划尝试)< a href="http://intridea.com/2009/2/13/dead-simple-task-scheduling-in-rails?blog=company" rel="noreferrer">在此博客文章中

他描述了像这样的东西:

scheduler = Rufus::Scheduler.start_new  

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

..这看起来很简单。 我想知道添加基于 HTML 的配置有多容易?

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

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?

时光无声 2024-07-21 05:43:00

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

我使用它并且对它非常满意。

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).

I use it and I am very happy with it.

紫南 2024-07-21 05:43:00

向您的模型之一添加一个类方法来为您处理此问题。 现在尝试使用运行程序脚本执行该方法

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

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

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

现在更改为任意随机目录并运行该命令。 如果运行正常,请运行 crontab -e 并将命令插入到设置中以每天早上 7 点运行。 如果你用谷歌搜索的话,那里有很多关于 cron 格式的解释,应该很容易理解。

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

./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.

述情 2024-07-21 05:43:00

正如已接受的答案已经说过的那样,进行 rake 任务和 cron 作业。 但请注意,更新 cron 文件本身是一项手动任务。 如果您在开发过程中不更改它,那可能没问题。 否则,您可以通过以下方式让 Capistrano 为您完成此操作: http:// /push.cx/2008/deploying-crontab-with-your-rails-app

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文