使用每日 cron 作业在 Rails 中使用 Ice Cube 创建的事件的正确方法
我想使用 Rails 中的 Ice Cube gem 创建重复事件 - 我的问题是,如何正确地或更有效地使用这些重复规则来触发实际事件?
经常性发票就是一个例子。
假设我将 Ice Cube 重复设置为每周一次,并使用 to_yaml 将其保存到重复发票行。我现在数据库中有一行具有序列化的重复规则。我可以想象使用它的唯一方法是运行数据库中的每一行,反序列化保存的重复规则并检查它今天是否需要使用schedule.occurrs_on?(Date.new)运行 - 然后将其放入每天运行的 cronjob:
items = RecurringItem.find(:all)
items.each do |item|
schedule = Schedule.from_yaml(item.schedule_yaml)
if schedule.occurs_on?(Date.new)
#if today is a recurrence, do stuff!
end
end
这对我来说看起来效率非常低 - 但我可能做得完全错误。有没有更好的方法来使用Ice Cube?
I want to create recurring events using the Ice Cube gem in Rails - my question is, how do I then correctly, or rather efficiently, use these recurring rules for triggering actual events?
An example of this would be a recurring invoice.
Say I have an Ice Cube recurrence set for once a week and I saved it to a recurring invoice row using to_yaml. I now have a row in the database with a serialized recurrence rule. The only way I can imagine using this is to run through each and every row in the database, unserializing the saved recurrence rules and checking whether it needs to run today with schedule.occurs_on?(Date.new) - this would then be put into a cronjob that runs daily:
items = RecurringItem.find(:all)
items.each do |item|
schedule = Schedule.from_yaml(item.schedule_yaml)
if schedule.occurs_on?(Date.new)
#if today is a recurrence, do stuff!
end
end
This looks terribly inefficient to me - but I might be doing it completely wrong. Is there no better way to use Ice Cube?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ice Cube 似乎专注于设置非常复杂的时间表(发生在第一个和第四个星期三,但前提是它们是偶数天,而不是周末等)
如果这就是您需要的,那么您描述的任务可能是每天按照这种复杂的时间表运行许多任务的最有效方法。如果您的计划不需要那么复杂,那么您可以查看类似“每当”(如 MatthewFord 提到的)之类的内容,它仅使用 cron 计划来设置要执行的任务,但这适用于管理类型的任务,因此需要要编辑的配置文件,如果您需要通过应用程序界面添加和删除内容,则该文件不起作用。
使用 Ice Cube 的另一个选择是让每月的 cron 遍历每个计划,并设置另一个表来定义下个月的哪些事件必须在哪些天运行。 (每一行都有一个日期和一个任务定义),您的每日 cron 可以从该表中进行选择...
每次应用程序中的某个计划发生更改时,您还必须提前一个月更新该表。有点麻烦,所以除非您每天有数十万个时间表需要查看一次,否则可能不值得这么麻烦。
Ice Cube seems to specialize in setting up very complicated schedules (occurs on the 1st and 4th wednesdays, but only if they're even numbered days, and not if a weekend, etc.)
If that's what you need, then the task you described probably IS the most efficient way to run a number of tasks every day on that kind of complex schedule. If you don't need that complexity in your schedules, then you could look at something like whenever (as MatthewFord mentioned), which just uses cron schedules to setup tasks to be executed, but that's intended for admin-type tasks, and so requires a config file to be edited, and doesn't work if you need to be adding and removing things via your application interface.
Another option for using Ice Cube would be to have a monthly cron go through every schedule, and set up another table defining which events have to be run on which days for the next month. (each row has a date and a task definition), and your daily cron could select from that table...
You would also have to update that table for one month ahead of time every time one of the schedules changed in the application... kind of a hassle, so unless you have hundreds of thousands of schedules to look through once a day, it's probably not worth the trouble.
由于您使用ice_cube,您可以查看Sidetiq。它使用ice_cube来指定作业的调度,因此您很容易掌握。
我的尝试不是检查所有记录以找到符合条件的记录,而是根据这些规则生成计划事件,但我认为有很多方法可以做到这一点可能会更有效。
Since you use ice_cube, you can look into Sidetiq. It uses ice_cube to specify the scheduling of jobs, so would be easy for you to get hang of.
Instead of checking all records to find the one eligible, my attempt would be to generate scheduled events based on these rules instead, but I think there are many ways to do it can may be more efficient.