如何在 Rails 中建模和存储重复任务?

发布于 2024-07-27 01:51:09 字数 326 浏览 6 评论 0原文

Rails 中的 Cron 解决方案有很多而且相当不错。 这不是我在这里苦苦挣扎的问题。

相反,我遇到的麻烦是让用户创建自己的重复任务(例如提醒) - 特别是如何在数据库中建模和存储这些任务(一个好的用户界面也很重要 - 如果有的话那就太棒了代码在那里)。 Google 日历就是一个很好的例子(添加事件的 UI,而不是整个日历)...他们应该能够在 CST 每天下午 1 点、周一/周三/周五或每周等进行操作。无论 cron 解决方案是什么然后使用时需要轮询数据库以查看在那个时间需要发送哪些提醒等。

有人在rails中见过一个很好的插件/gem吗? 似乎会有一些东西可以用,但我还没有找到。

Cron solutions in rails are numerous and pretty good. That's not what I'm struggling with here.

Instead, what I'm having trouble with is letting users create their own recurring tasks (like reminders) - specifically how to model and store these in the DB (a good UI for it is non-trivial too - would be awesome if there was code out there for that). Google calendar is a great example here (the UI to add an event, not the entire calendar)...they should be able to do daily at 1pm CST, or mon/wed/fri, or weekly, etc. Whatever cron solution is being used would then need to poll the database to see which reminders needed to be sent at that hour, etc.

Anyone seen a good plugin/gem for this in rails? Seems like there would be something out there for it, but I haven't found it yet.

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

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

发布评论

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

评论(4

篱下浅笙歌 2024-08-03 01:51:09

我最终推出了自己的解决方案,因为我不需要任何超级花哨的东西。

基本上,我向数据库添加了 next_run 日期时间和 interval 字符串列,该列是(日、周等)之一。 然后设置一个要运行的 cron 作业来查找任何已过去的 next_run 日期。 它运行它们,然后根据 interval 列将 next_run 设置为未来的某个时间点。

简单但适合我的需要。

I ended up rolling my own solution since I didn't need anything super fancy.

Basically I added a next_run datetime and interval string column to the db which was one of (day, week, etc). Then setup a cron job to run that looks for any next_run dates that have passed. It runs them and then sets next_run to some point in the future based on the interval column.

Simple but worked for my needs.

带上头具痛哭 2024-08-03 01:51:09

目前正在考虑使用这样的插件来将重复存储在表中
http://github.com/fnando/recurrence/tree/master

每个提醒都会有一个重复对象,并且提醒在应该发送下一个对象时也会保留一个日期时间字段。 然后 cron 可以...

get all reminder's whose "next_send" date has passed
for each reminder
  send it
  update the "next_send" field using the recurrence object
end

如果有更好的解决方案或者我走错了路,输入总是值得赞赏的。

Currently thinking about using a plugin like this to store recurrences in a table
http://github.com/fnando/recurrence/tree/master

each reminder would have one recurrence object, and the reminder would also keep a datetime field when it's supposed to send it's next one. Then the cron could...

get all reminder's whose "next_send" date has passed
for each reminder
  send it
  update the "next_send" field using the recurrence object
end

If there are better solutions or I'm going down the wrong path, input always appreciated.

与往事干杯 2024-08-03 01:51:09

我一直发现 ical(RFC,而不是程序)解决方案是处理重复事件的最佳方法。 有一些很好的 Ruby 库可以处理 ical,最新的库是 ri_cal< /a>.

I have always found ical (the RFC, not the program) solutions to be the best approach for working with recurring events. There are a few good Ruby libraries for dealing with ical, and the newest kid on the block is ri_cal.

执着的年纪 2024-08-03 01:51:09

我目前遇到这个问题,我正在考虑的解决方案如下:

class AllowReoccuringTasks < ActiveRecord::Migration  
    def self.up  
        add_column :tasks, :reoccuring, :boolean  
        add_column :tasks, :period, :integer  
    end
end

其中 period 可以是 1(每天)、7(每周)或 14(每隔一周)。

如果您想支持其他类型的计划,例如每月、工作日、周末等,您可以添加一个名为“计划”的列,并使用常量来表示不同的计划类型。 您还可以使用enum 插件

I'm currently having this problem and the solution that I'm considering is as follows:

class AllowReoccuringTasks < ActiveRecord::Migration  
    def self.up  
        add_column :tasks, :reoccuring, :boolean  
        add_column :tasks, :period, :integer  
    end
end

where period can be either 1 (every day), 7 (every week), or 14 (every other week).

If you wanted to support other types of schedules like every month, weekdays, weekends, etc., you could instead add a column called "schedule" and use constants to represent different schedule types. You could also use the enum plugin.

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