def get(self): ndb.get_context().set_cache_policy(lambda _: False) bot_management.cron_update_bot_info() self.response.headers['Content-Type'] = 'text/plain; charset=utf-8' self.response.out.write('Success.')
def run_cron(self): bot_management.cron_update_bot_info()
def test_cron_update_bot_info(self): # Create two bots, one becomes dead, updating the cron job fixes composite. timeout = bot_management.config.settings().bot_death_timeout_secs def check(dead, alive): q = bot_management.filter_availability( bot_management.BotInfo.query(), quarantined=None, in_maintenance=None, is_dead=True, is_busy=None, is_mp=None) self.assertEqual(dead, [t.to_dict() for t in q]) q = bot_management.filter_availability( bot_management.BotInfo.query(), quarantined=None, in_maintenance=None, is_dead=False, is_busy=None, is_mp=None) self.assertEqual(alive, [t.to_dict() for t in q]) bot_management.bot_event(event_type='bot_connected', bot_id='id1', external_ip='8.8.4.4', authenticated_as='bot:id1.domain', dimensions={ 'id': ['id1'], 'foo': ['bar'] }, state={'ram': 65}, version=hashlib.sha256().hexdigest(), quarantined=False, maintenance_msg=None, task_id=None, task_name=None) # One second before the timeout value. then = self.mock_now(self.now, timeout - 1) bot_management.bot_event(event_type='bot_connected', bot_id='id2', external_ip='8.8.4.4', authenticated_as='bot:id2.domain', dimensions={ 'id': ['id2'], 'foo': ['bar'] }, state={'ram': 65}, version=hashlib.sha256().hexdigest(), quarantined=False, maintenance_msg=None, task_id=None, task_name=None) bot1_alive = self._gen_bot_info() bot1_dead = self._gen_bot_info(composite=[ bot_management.BotInfo.NOT_IN_MAINTENANCE, bot_management.BotInfo.DEAD, bot_management.BotInfo.NOT_MACHINE_PROVIDER, bot_management.BotInfo.HEALTHY, bot_management.BotInfo.IDLE, ], is_dead=True) bot2_alive = self._gen_bot_info(authenticated_as=u'bot:id2.domain', dimensions={ u'foo': [u'bar'], u'id': [u'id2'] }, first_seen_ts=then, id='id2', last_seen_ts=then) check([], [bot1_alive, bot2_alive]) self.assertEqual(0, bot_management.cron_update_bot_info()) check([], [bot1_alive, bot2_alive]) # Just stale enough to trigger the dead logic. then = self.mock_now(self.now, timeout) # The cron job didn't run yet, so it still has ALIVE bit. check([], [bot1_alive, bot2_alive]) self.assertEqual(1, bot_management.cron_update_bot_info()) # The cron job ran, so it's now correct. check([bot1_dead], [bot2_alive])
def test_cron_update_bot_info(self): # Create two bots, one becomes dead, updating the cron job fixes composite. timeout = bot_management.config.settings().bot_death_timeout_secs def check(dead, alive): q = bot_management.filter_availability( bot_management.BotInfo.query(), quarantined=None, in_maintenance=None, is_dead=True, is_busy=None) self.assertEqual(dead, [t.to_dict() for t in q]) q = bot_management.filter_availability( bot_management.BotInfo.query(), quarantined=None, in_maintenance=None, is_dead=False, is_busy=None) self.assertEqual(alive, [t.to_dict() for t in q]) _bot_event(event_type='request_sleep') # One second before the timeout value. then = self.mock_now(self.now, timeout - 1) _bot_event(event_type='request_sleep', bot_id='id2', external_ip='8.8.4.4', authenticated_as='bot:id2.domain', dimensions={ 'id': ['id2'], 'foo': ['bar'] }) bot1_alive = _gen_bot_info(first_seen_ts=self.now, last_seen_ts=self.now) bot1_dead = _gen_bot_info( first_seen_ts=self.now, last_seen_ts=self.now, composite=[ bot_management.BotInfo.NOT_IN_MAINTENANCE, bot_management.BotInfo.DEAD, bot_management.BotInfo.HEALTHY, bot_management.BotInfo.IDLE, ], is_dead=True) bot2_alive = _gen_bot_info(authenticated_as=u'bot:id2.domain', dimensions={ u'foo': [u'bar'], u'id': [u'id2'] }, first_seen_ts=then, id='id2', last_seen_ts=then) check([], [bot1_alive, bot2_alive]) self.assertEqual(0, bot_management.cron_update_bot_info()) check([], [bot1_alive, bot2_alive]) # Just stale enough to trigger the dead logic. then = self.mock_now(self.now, timeout) # The cron job didn't run yet, so it still has ALIVE bit. check([], [bot1_alive, bot2_alive]) self.assertEqual(1, bot_management.cron_update_bot_info()) # The cron job ran, so it's now correct. check([bot1_dead], [bot2_alive]) # the last event should be bot_missing events = list(bot_management.get_events_query('id1', order=True)) event = events[0] bq_event = swarming_pb2.BotEvent() event.to_proto(bq_event) self.assertEqual(event.event_type, 'bot_missing') self.assertEqual(event.last_seen_ts, bot1_dead['last_seen_ts']) self.assertEqual(bq_event.event, swarming_pb2.BOT_MISSING) self.assertEqual(bq_event.bot.status, swarming_pb2.MISSING) last_seen_ts = timestamp_pb2.Timestamp() last_seen_ts.FromDatetime(bot1_dead['last_seen_ts']) self.assertEqual(bq_event.bot.info.last_seen_ts, last_seen_ts)