如何在 Rails 中建模和存储重复任务?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最终推出了自己的解决方案,因为我不需要任何超级花哨的东西。
基本上,我向数据库添加了
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 andinterval
string column to the db which was one of (day, week, etc). Then setup a cron job to run that looks for anynext_run
dates that have passed. It runs them and then setsnext_run
to some point in the future based on theinterval
column.Simple but worked for my needs.
目前正在考虑使用这样的插件来将重复存储在表中
http://github.com/fnando/recurrence/tree/master
每个提醒都会有一个重复对象,并且提醒在应该发送下一个对象时也会保留一个日期时间字段。 然后 cron 可以...
如果有更好的解决方案或者我走错了路,输入总是值得赞赏的。
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...
If there are better solutions or I'm going down the wrong path, input always appreciated.
我一直发现 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.
我目前遇到这个问题,我正在考虑的解决方案如下:
其中 period 可以是 1(每天)、7(每周)或 14(每隔一周)。
如果您想支持其他类型的计划,例如每月、工作日、周末等,您可以添加一个名为“计划”的列,并使用常量来表示不同的计划类型。 您还可以使用enum 插件。
I'm currently having this problem and the solution that I'm considering is as follows:
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.