Skip to content

Commit

Permalink
fix: 🐛 make everything work with rrulesets
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickBrun committed Dec 16, 2024
1 parent 46b6fb7 commit a982907
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions rq_scheduler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def get_next_rrule_scheduled_time(rrule_string):
"""Calculate the next scheduled time by creating a rrule object
with a rrule string"""
timezone = dateutil.tz.UTC
rule = dateutil.rrule.rrulestr(rrule_string)
if rule._dtstart.tzinfo is None:
ruleset = dateutil.rrule.rrulestr(rrule_string, forceset=True)
if ruleset[0].tzinfo is None:
now = datetime.now() # naive datetime
else:
now = datetime.now(tz=timezone) # aware datetime
next_time = rule.after(now)
next_time = ruleset.after(now)
return next_time.astimezone(timezone)


Expand Down
18 changes: 18 additions & 0 deletions tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,3 +1125,21 @@ def test_rrule_persisted_correctly_with_until(self):
assert datetime_time.minute == 1
assert datetime_time.second == 0
assert datetime_time - now <= timedelta(hours=1), f"{datetime_time - now} is greater than 1 hour"

def test_rruleset_persisted_correctly_with_dtstart(self):
"""
Ensure that rrule attribute gets correctly saved in Redis.
"""
# create a job that runs one minute past each whole hour
job = self.scheduler.rrule("DTSTART:20241126T154900Z\nRRULE:FREQ=HOURLY;WKST=MO;BYMINUTE=1;BYSECOND=0\nRRULE:FREQ=MINUTELY;INTERVAL=5;COUNT=2", say_hello)
job_from_queue = Job.fetch(job.id, connection=self.testconn)
self.assertEqual(job_from_queue.meta['rrule_string'], "DTSTART:20241126T154900Z\nRRULE:FREQ=HOURLY;WKST=MO;BYMINUTE=1;BYSECOND=0\nRRULE:FREQ=MINUTELY;INTERVAL=5;COUNT=2")

# get the scheduled_time and convert it to a datetime object
unix_time = self.testconn.zscore(self.scheduler.scheduled_jobs_key, job.id)
datetime_time = from_unix(unix_time)

# check that minute=1, seconds=0, and is within an hour
assert datetime_time.minute == 1
assert datetime_time.second == 0
assert datetime_time - datetime.utcnow() <= timedelta(hours=1), f"{datetime_time - datetime.utcnow()} is greater than 1 hour"

0 comments on commit a982907

Please sign in to comment.