def test_crontab_rescheduled_correctly_with_local_timezone(self): # Create a job with a cronjob_string job = self.scheduler.cron("1 15 * * *", say_hello, use_local_timezone=True) # change crontab job.meta['cron_string'] = "2 15 * * *" # reenqueue the job self.scheduler.enqueue_job(job) # 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) expected_datetime_in_local_tz = datetime.now( get_utc_timezone()).replace(hour=15, minute=2, second=0, microsecond=0) assert datetime_time.time( ) == expected_datetime_in_local_tz.astimezone( get_utc_timezone()).time()
def test_crontab_persisted_correctly_with_local_timezone(self): """ Ensure that crontab attribute gets correctly saved in Redis when using local TZ. """ # create a job that runs one minute past each whole hour job = self.scheduler.cron("0 15 * * *", say_hello, use_local_timezone=True) job_from_queue = Job.fetch(job.id, connection=self.testconn) self.assertEqual(job_from_queue.meta['cron_string'], "0 15 * * *") # 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) expected_datetime_in_local_tz = datetime.now(tzlocal()).replace(hour=15,minute=0,second=0,microsecond=0) assert datetime_time.time() == expected_datetime_in_local_tz.astimezone(UTC).time()
def test_crontab_persisted_correctly(self): """ Ensure that crontab attribute gets correctly saved in Redis. """ # create a job that runs one minute past each whole hour job = self.scheduler.cron("1 * * * *", say_hello) job_from_queue = Job.fetch(job.id, connection=self.testconn) self.assertEqual(job_from_queue.meta['cron_string'], "1 * * * *") # 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)
def scheduled_execution_time(job_id, scheduler=None, naive=True): """Get RQ-Scheduler scheduled execution time for specific job.""" _scheduler = scheduler if not scheduler: _scheduler = django_rq.get_scheduler("default") # Scheduler keeps jobs in a single key, they are sorted by score, which is # scheduled execution time (linux epoch). We can retrieve single # entry's score. time = _scheduler.connection.zscore(_scheduler.scheduled_jobs_key, job_id) # Convert linux time epoch to UTC. if time: time = from_unix(time) if not naive: # By default, RQ-Scheduler uses UTC naive (TZ-unaware) objects, # which we can "convert" to TZ-aware UTC. time = time.replace(tzinfo=pytz.UTC) return time