Reputation: 19903
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.
Upvotes: 7
Views: 3661
Reputation: 31
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.
Upvotes: 0
Reputation: 19903
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.
Upvotes: 7
Reputation: 1660
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.
Upvotes: 1
Reputation: 19903
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.
Upvotes: 1