0

I've got an app that allows users to schedule tasks to run whenever they desire. (A todo list with recurring items.)

I need to somehow re-trigger these events to show up again each time their schedule comes up by updating an attribute on the object - it may also send a notification to the user.

My plan for this was to have a cron job that runs every minute/hour/short interval, and in that job, it would find all of the items with schedules that match the current time or should be updated since the last job, however, short of iterating through every item, I don't see a quick way of querying for those objects.

Using Ice Cube I can very easily and cleanly save schedules in my database, but I don't see a method of finding all events that match a particular point in time.

I know once I find the item I can run occurring_between? or occurring_at? to find if I should run it, but that requires pulling every single item into memory and manually checking it, which is not very scaleable.

Is there a way I'm missing, or are there other suggestions for accomplishing what I'm trying to do here? It's still pretty early, so I'm not attached to Ice Cube or any of the current implementations.

Rockster160
  • 1,579
  • 1
  • 15
  • 30
  • You could probably come up with a adapter to save the objects from icecube as a normal ActiveRecord model instead of storing it in a serialized column as YAML or JSON or whatever. To get a time between two points you just use a range `Task.where(timestamp_column: Time.now..3.days.ago)`. Dealing with recurring times is a bit tricker - you extract the right parts with the DB's time function and compare with between. – max Mar 08 '18 at 19:21
  • Yeah, the recurring times are the issue, as I can't query them as timestamps. :( I came up with a solution similar to this one that I'll post as a temporary answer. – Rockster160 Mar 08 '18 at 20:09

1 Answers1

0

After some more thought- I'm not seeing any way to do this, so I've come up with a little hack that I'll do instead:

On the item object, I'll have 2 additional attributes. One will be the schedule which is fed to Ice Cube to generate all of the dates/times to recur at. The next will be next_occurrence, which I'll set on create and each time the item is renewed. Then in the worker, I'll query for all items that have a next_occurrence in the past and process them, resetting the next_occurrence to be the next time the schedule is to occur.

I'll leave this answer unmarked for a bit in case anybody has a better solution.

Rockster160
  • 1,579
  • 1
  • 15
  • 30