def schedule(self, args=None, kwargs=None, eta=None, delay=None, priority=None, id=None): if eta is None and delay is None: if isinstance(args, (int, float)): delay = args elif isinstance(args, datetime.timedelta): delay = args.total_seconds() elif isinstance(args, datetime.datetime): eta = args else: raise ValueError('schedule() missing required eta= or delay=') args = None if kwargs is not None and not isinstance(kwargs, dict): raise ValueError('schedule() kwargs argument must be a dict.') eta = normalize_time(eta, delay, self.huey.utc) task = self.task_class( args or (), kwargs or {}, id=id, eta=eta, retries=self.retries, retry_delay=self.retry_delay, priority=priority) return self.huey.enqueue(task)
def reschedule(self, eta=None, delay=None): # Rescheduling works by revoking the currently-scheduled task (nothing # is done to check if the task has already run, however). Then the # original task's data is used to enqueue a new task with a new task ID # and execution_time. self.revoke() if eta is not None or delay is not None: eta = normalize_time(eta, delay, self.huey.utc) task = type(self.task)(self.task.args, self.task.kwargs, eta=eta, retries=self.task.retries, retry_delay=self.task.retry_delay) return self.huey.enqueue(task)
def reschedule(self, eta=None, delay=None): # Rescheduling works by revoking the currently-scheduled task (nothing # is done to check if the task has already run, however). Then the # original task's data is used to enqueue a new task with a new task ID # and execution_time. self.revoke() if eta is not None or delay is not None: eta = normalize_time(eta, delay, self.huey.utc) task = type(self.task)( self.task.args, self.task.kwargs, eta=eta, retries=self.task.retries, retry_delay=self.task.retry_delay) return self.huey.enqueue(task)
def test_normalize_time(self): ts_local = datetime.datetime(2000, 1, 1, 12, 0, 0) # Noon on Jan 1. ts_utc = ts_local + datetime.timedelta(hours=8) # For fake tz. ts_inv = ts_local - datetime.timedelta(hours=8) # Naive datetime. # No conversion is applied, as we treat everything as local time. self.assertEqual(normalize_time(ts_local, utc=False), ts_local) # So we provided a naive timestamp from the localtime (us/pacific), # which is 8 hours behind UTC in January. self.assertEqual(normalize_time(ts_local, utc=True), ts_utc) # TZ-aware datetime in local timezone (Fake US/Pacific). # Here we provide a tz-aware timestamp from the localtime (us/pacific). ts = datetime.datetime(2000, 1, 1, 12, 0, 0, tzinfo=FakePacific()) # No conversion, treated as local time. self.assertEqual(normalize_time(ts, utc=False), ts_local) # Converted to UTC according to rules from our fake tzinfo, +8 hours. self.assertEqual(normalize_time(ts, utc=True), ts_utc) # TZ-aware datetime in UTC timezone. # Here we provide a tz-aware timestamp using UTC timezone. ts = datetime.datetime(2000, 1, 1, 12, 0, 0, tzinfo=UTC()) # Since we're specifying utc=False, we are dealing with localtimes # internally. The timestamp passed in is a tz-aware timestamp in UTC. # To convert to a naive localtime, we subtract 8 hours (since UTC is # 8 hours ahead of our local time). self.assertEqual(normalize_time(ts, utc=False), ts_inv) # When utc=True there's no change, since the timestamp is already UTC. self.assertEqual(normalize_time(ts, utc=True), ts_local)
def revoke(self, task, revoke_until=None, revoke_once=False): if revoke_until is not None: revoke_until = normalize_time(revoke_until, utc=self.utc) self.put(task.revoke_id, (revoke_until, revoke_once))
def revoke_all(self, task_class, revoke_until=None, revoke_once=False): if revoke_until is not None: revoke_until = normalize_time(revoke_until, utc=self.utc) self.put(self._task_key(task_class, 'rt'), (revoke_until, revoke_once))