Coverage for /builds/BuildGrid/buildgrid/buildgrid/bot/bot.py: 0.00%
22 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-22 21:04 +0000
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-22 21:04 +0000
1# Copyright (C) 2018 Bloomberg LP
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# <http://www.apache.org/licenses/LICENSE-2.0>
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
16import asyncio
17import logging
20class Bot:
21 """Creates a local BotSession."""
23 def __init__(self, bot_session):
24 self.__logger = logging.getLogger(__name__)
26 self.__bot_session = bot_session
28 self.__loop = None
30 def session(self):
31 """Will create a session and periodically call the server."""
33 self.__loop = asyncio.get_event_loop()
35 try:
36 task = asyncio.ensure_future(self.__bot_session.run())
37 self.__loop.run_until_complete(task)
38 except KeyboardInterrupt:
39 pass
41 self.__kill_everyone()
42 self.__logger.info("Bot shutdown.")
44 def __kill_everyone(self):
45 """Cancels and waits for them to stop."""
46 self.__logger.info("Cancelling remaining tasks...")
47 if self.__loop:
48 for task in asyncio.all_tasks(loop=self.__loop):
49 task.cancel()
50 self.__loop.run_until_complete(task)