def test_shutdown_hook_not_flushed(self, _mock_flush): time_now = 10000 id = shared.get_instance_entity().key.id() with shared.instance_namespace_context(): self.assertIsNotNone(shared.Instance.get_by_id(id)) config._shutdown_hook(time_fn=lambda: time_now) with shared.instance_namespace_context(): self.assertIsNone(shared.Instance.get_by_id(id))
def test_shutdown_hook_not_flushed(self, _mock_flush): time_now = 10000 id = shared.get_instance_entity().key.id() with shared.instance_namespace_context(): self.assertIsNotNone(shared.Instance.get_by_id(id)) config._shutdown_hook(time_fn=lambda: time_now) with shared.instance_namespace_context(): self.assertIsNone(shared.Instance.get_by_id(id))
def test_assign_task_num(self): time_now = datetime.datetime(2016, 2, 8, 1, 0, 0) time_current = time_now - datetime.timedelta( seconds=shared.INSTANCE_EXPIRE_SEC - 1) time_expired = time_now - datetime.timedelta( seconds=shared.INSTANCE_EXPIRE_SEC + 1) with shared.instance_namespace_context(): shared.Instance(id='expired', task_num=0, last_updated=time_expired).put() shared.Instance(id='inactive', task_num=-1, last_updated=time_expired).put() shared.Instance(id='new', task_num=-1, last_updated=time_current).put() shared.Instance(id='current', task_num=2, last_updated=time_current).put() handlers._assign_task_num(time_fn=lambda: time_now) expired = shared.Instance.get_by_id('expired') inactive = shared.Instance.get_by_id('inactive') new = shared.Instance.get_by_id('new') current = shared.Instance.get_by_id('current') self.assertIsNone(expired) self.assertIsNone(inactive) self.assertIsNotNone(new) self.assertIsNotNone(current) self.assertEqual(2, current.task_num) self.assertEqual(1, new.task_num)
def get(self): if self.request.headers.get('X-Appengine-Cron') != 'true': self.abort(403) with shared.instance_namespace_context(): _assign_task_num() interface.invoke_global_callbacks()
def get(self): if self.request.headers.get('X-Appengine-Cron') != 'true': self.abort(403) with shared.instance_namespace_context(): _assign_task_num() for name, callback in shared.global_metrics_callbacks.iteritems(): logging.debug('Invoking callback %s', name) callback()
def _shutdown_hook(time_fn=time.time): shared.shutdown_counter.increment() if flush_metrics_if_needed(time_fn()): logging.info('Shutdown hook: deleting %s, metrics were flushed.', shared.instance_key_id()) else: logging.warning('Shutdown hook: deleting %s, metrics were NOT flushed.', shared.instance_key_id()) with shared.instance_namespace_context(): ndb.Key('Instance', shared.instance_key_id()).delete()
def get(self): if self.request.headers.get('X-Appengine-Cron') != 'true': self.abort(403) with shared.instance_namespace_context(): _assign_task_num() for name, callback in shared.global_metrics_callbacks.iteritems(): logging.debug('Invoking callback %s', name) callback()
def _shutdown_hook(time_fn=time.time): shared.shutdown_counter.increment() if flush_metrics_if_needed(time_fn()): logging.info('Shutdown hook: deleting %s, metrics were flushed.', shared.instance_key_id()) else: logging.warning( 'Shutdown hook: deleting %s, metrics were NOT flushed.', shared.instance_key_id()) with shared.instance_namespace_context(): ndb.Key(shared.Instance._get_kind(), shared.instance_key_id()).delete()