0

Am using Ice Cube gem https://github.com/seejohnrun/ice_cube to schedule events, I want to make every event to appear in the app every week on monday, and after 6 hours the event title should be marked with a red color.

In the doc ice cube has this example for setting a duration

# or give the schedule a duration and ask if occurring_at?
schedule = IceCube::Schedule.new(now, :duration => 3600)
schedule.add_recurrence_rule IceCube::Rule.daily
schedule.occurring_at?(now + 1800) # true

which I translated to my need as the following :

start_date = Time.now.utc - 10.days # any datetime basically
schedule = IceCube::Schedule.new(start_date, :duration => (6.hours).seconds)
schedule.add_recurrence_rule IceCube::Rule.weekly(1).day(:monday)

But from what I get it seems that the 6 hours duration works only for the FIRST occurrence (6 hours from the start_date), what I want is 6 hours from every monday, every week.

medBouzid
  • 7,484
  • 10
  • 56
  • 86

1 Answers1

0

I was looking for something similar so I stumbled upon your question. If you have not yet figured this out, here is what works.

So, first we set the end_time for the event:

start_date = Time.now.utc - 10.days
schedule = IceCube::Schedule.new(start_date, end_time: start_date + 6.hours.seconds)
schedule.add_recurrence_rule IceCube::Rule.weekly(1).day(:monday)

We also set a weekly rule (on every Monday). Now we see that this works fine for the occurrences e.g.

schedule.next_occurrences(6)
# [2019-12-09 10:01:13 UTC, 2019-12-16 10:01:13 UTC, 2019-12-23 10:01:13 UTC, 2019-12-30 10:01:13 UTC, 2020-01-06 10:01:13 UTC, 2020-01-13 10:01:13 UTC]

Let's see the "duration" of the first occurence listed above:

# The event has started
schedule.occurring_at? Time.utc(2019, 12, 9,  10, 1, 14)
# true
# Almost 6 hours later
schedule.occurring_at? Time.utc(2019, 12, 9,  16, 1, 12)
# true
schedule.occurring_at? Time.utc(2019, 12, 9,  16, 1, 13)
# false
# The event just ended

So if you check the event occurring_at? you will be able to change the colour you need. If we set a duration for an event, after that duration the occurrences will stop. But if we set an end_time all occurrences will work fine for the period between start_time and end_time

alexts
  • 180
  • 2
  • 13
  • thank @alexts if I remember well I found a workaround that issue... it's been a while since I haven't updated that project, I will get back to it soon and make sure to try your answer. thanks a lot – medBouzid Dec 05 '19 at 17:20
  • oh wow, took a while to understand this... `end_time` has rather the purpose to set the `duration` that each individual occurrence will be considered an **active** occurrence (i.e. used in `IceCube:::Schedule#conflicts_with?(other_schedule)` to manage resources). A better name would be `start_end_time`. Unless a very complex scenario to move data between different timezones, you better use `IceCube::Schedule.new(start_date, duration: 6.hours)` (the max or the average time that each job will take to finish). `end_time` does NOT relate at all to terminating occurrences (rather use `Rule#until`). – rellampec Aug 19 '23 at 07:44