def __init__(self, model): logger = get_logger(__name__) self.app = current_app._get_current_object() self.name = model.name self.task = model.task try: self.schedule = model.schedule except model.DoesNotExist: logger.error('Schedule was removed from database') logger.warning('Disabling %s', self.name) self._disable(model) try: self.args = loads(model.args or '[]') self.kwargs = loads(model.kwargs or '{}') except ValueError: logging.error('Failed to serialize arguments for %s.', self.name, exc_info=1) logging.warning('Disabling %s', self.name) self._disable(model) self.options = { 'queue': model.queue, 'exchange': model.exchange, 'routing_key': model.routing_key, 'expires': model.expires } self.total_run_count = model.total_run_count self.model = model if not model.last_run_at: model.last_run_at = self._default_now() self.last_run_at = make_aware(model.last_run_at)
def test_sync_saves_last_run_at(self): e1 = self.s.schedule[self.m2.name] last_run = e1.last_run_at last_run2 = last_run - timedelta(days=1) e1.model.last_run_at = last_run2 self.s._dirty.add(self.m2.name) self.s.sync() e2 = self.s.schedule[self.m2.name] self.assertEqual(make_aware(e2.last_run_at), last_run2)
def test_get_heartbeat(self): worker = Worker(hostname='fuzzie') self.assertIsNone(self.cam.get_heartbeat(worker)) t1 = time() t2 = time() t3 = time() for t in t1, t2, t3: worker.on_heartbeat(timestamp=t) self.assertEqual(self.cam.get_heartbeat(worker), make_aware(datetime.fromtimestamp(t3)))
def test_get_heartbeat(self): worker = Worker(hostname='fuzzie') self.assertIsNone(self.cam.get_heartbeat(worker)) t1 = time() t2 = time() t3 = time() for t in t1, t2, t3: worker.event('heartbeat', t, t, {}) self.state.workers[worker.hostname] = worker self.assertEqual(self.cam.get_heartbeat(worker), make_aware(datetime.fromtimestamp(t3)))
def test_sync_syncs_before_save(self): # Get the entry for m2 e1 = self.s.schedule[self.m2.name] # Increment the entry (but make sure it doesn't sync) self.s._last_sync = time() e2 = self.s.schedule[e1.name] = self.s.reserve(e1) self.assertEqual(self.s.flushed, 2) # Fetch the raw object from db, change the args # and save the changes. m2 = PeriodicTask.objects.get(pk=self.m2.pk) m2.args = "[16, 16]" m2.save() # get_schedule should now see the schedule has changed. # and also sync the dirty objects. e3 = self.s.schedule[self.m2.name] self.assertEqual(self.s.flushed, 3) self.assertEqual(make_aware(e3.last_run_at), e2.last_run_at) self.assertListEqual(e3.args, [16, 16])
def tstamp(task): # convert to local timezone value = make_aware(task.tstamp) return "{0}".format(escape(naturaldate(value)), )
def eta(task): if not task.eta: return 'none' return escape(make_aware(task.eta))