Raoul Hidalgo Charman pushed to branch raoul/smarter-bot-calls at BuildGrid / buildgrid
Commits:
-
53e224e7
by Raoul Hidalgo Charman at 2018-11-28T11:51:54Z
4 changed files:
- buildgrid/bot/bot.py
- buildgrid/bot/interface.py
- buildgrid/bot/session.py
- buildgrid/bot/tenantmanager.py
Changes:
... | ... | @@ -35,7 +35,7 @@ class Bot: |
35 | 35 |
self.__bot_session.create_bot_session()
|
36 | 36 |
|
37 | 37 |
try:
|
38 |
- task = asyncio.ensure_future(self.__update_bot_session())
|
|
38 |
+ task = asyncio.ensure_future(self.__bot_session.run())
|
|
39 | 39 |
self.__loop.run_until_complete(task)
|
40 | 40 |
|
41 | 41 |
except KeyboardInterrupt:
|
... | ... | @@ -44,15 +44,6 @@ class Bot: |
44 | 44 |
self.__kill_everyone()
|
45 | 45 |
self.__logger.info("Bot shutdown.")
|
46 | 46 |
|
47 |
- async def __update_bot_session(self):
|
|
48 |
- """Calls the server periodically to inform the server the client has not died."""
|
|
49 |
- try:
|
|
50 |
- while True:
|
|
51 |
- self.__bot_session.update_bot_session()
|
|
52 |
- |
|
53 |
- except asyncio.CancelledError:
|
|
54 |
- pass
|
|
55 |
- |
|
56 | 47 |
def __kill_everyone(self):
|
57 | 48 |
"""Cancels and waits for them to stop."""
|
58 | 49 |
self.__logger.info("Cancelling remaining tasks...")
|
... | ... | @@ -36,25 +36,22 @@ class BotInterface: |
36 | 36 |
|
37 | 37 |
self.__logger.info(channel)
|
38 | 38 |
self._stub = bots_pb2_grpc.BotsStub(channel)
|
39 |
- self._interval = interval
|
|
39 |
+ self.interval = interval
|
|
40 | 40 |
|
41 | 41 |
def create_bot_session(self, parent, bot_session):
|
42 | 42 |
request = bots_pb2.CreateBotSessionRequest(parent=parent,
|
43 | 43 |
bot_session=bot_session)
|
44 |
- try:
|
|
45 |
- return self._stub.CreateBotSession(request)
|
|
46 |
- |
|
47 |
- except grpc.RpcError as e:
|
|
48 |
- self.__logger.error(e)
|
|
49 |
- raise
|
|
44 |
+ return self._bot_call(self._stub.CreateBotSession, request)
|
|
50 | 45 |
|
51 | 46 |
def update_bot_session(self, bot_session, update_mask=None):
|
52 | 47 |
request = bots_pb2.UpdateBotSessionRequest(name=bot_session.name,
|
53 | 48 |
bot_session=bot_session,
|
54 | 49 |
update_mask=update_mask)
|
55 |
- try:
|
|
56 |
- return self._stub.UpdateBotSession(request, timeout=self._interval)
|
|
50 |
+ return self._bot_call(self._stub.UpdateBotSession, request)
|
|
57 | 51 |
|
52 |
+ def _bot_call(self, call, request):
|
|
53 |
+ try:
|
|
54 |
+ return call(request, timeout=self.interval)
|
|
58 | 55 |
except grpc.RpcError as e:
|
59 | 56 |
self.__logger.error(e)
|
60 | 57 |
raise
|
... | ... | @@ -19,6 +19,7 @@ Bot Session |
19 | 19 |
|
20 | 20 |
Allows connections
|
21 | 21 |
"""
|
22 |
+import asyncio
|
|
22 | 23 |
import logging
|
23 | 24 |
import platform
|
24 | 25 |
|
... | ... | @@ -47,6 +48,9 @@ class BotSession: |
47 | 48 |
self._status = BotStatus.OK.value
|
48 | 49 |
self._tenant_manager = TenantManager()
|
49 | 50 |
|
51 |
+ self.connected = False
|
|
52 |
+ self._futures = {}
|
|
53 |
+ |
|
50 | 54 |
self.__parent = parent
|
51 | 55 |
self.__bot_id = '{}.{}'.format(parent, platform.node())
|
52 | 56 |
self.__name = None
|
... | ... | @@ -58,10 +62,33 @@ class BotSession: |
58 | 62 |
def bot_id(self):
|
59 | 63 |
return self.__bot_id
|
60 | 64 |
|
65 |
+ async def run(self):
|
|
66 |
+ """ Run a bot session
|
|
67 |
+ |
|
68 |
+ This connects and reconnects via create bot session and waits on update
|
|
69 |
+ bot session calls.
|
|
70 |
+ """
|
|
71 |
+ self.__logger.debug("Starting bot session")
|
|
72 |
+ interval = self._bots_interface.interval
|
|
73 |
+ while True:
|
|
74 |
+ if self.connected is False:
|
|
75 |
+ self.create_bot_session()
|
|
76 |
+ else:
|
|
77 |
+ self.update_bot_session()
|
|
78 |
+ |
|
79 |
+ if self.connected is False:
|
|
80 |
+ await asyncio.sleep(interval)
|
|
81 |
+ else:
|
|
82 |
+ await self._tenant_manager.wait_on_tenants(interval)
|
|
83 |
+ |
|
61 | 84 |
def create_bot_session(self):
|
62 | 85 |
self.__logger.debug("Creating bot session")
|
63 | 86 |
|
64 | 87 |
session = self._bots_interface.create_bot_session(self.__parent, self.get_pb2())
|
88 |
+ if session is None:
|
|
89 |
+ self.connected = False
|
|
90 |
+ return
|
|
91 |
+ self.connected = True
|
|
65 | 92 |
self.__name = session.name
|
66 | 93 |
|
67 | 94 |
self.__logger.info("Created bot session with name: [%s]", self.__name)
|
... | ... | @@ -73,6 +100,10 @@ class BotSession: |
73 | 100 |
self.__logger.debug("Updating bot session: [%s]", self.__bot_id)
|
74 | 101 |
|
75 | 102 |
session = self._bots_interface.update_bot_session(self.get_pb2())
|
103 |
+ if session is None:
|
|
104 |
+ self.connected = False
|
|
105 |
+ return
|
|
106 |
+ self.connected = True
|
|
76 | 107 |
server_ids = []
|
77 | 108 |
|
78 | 109 |
for lease in session.leases:
|
... | ... | @@ -150,6 +150,14 @@ class TenantManager: |
150 | 150 |
"""
|
151 | 151 |
return self._tenants[lease_id].tenant_completed
|
152 | 152 |
|
153 |
+ async def wait_on_tenants(self, timeout):
|
|
154 |
+ if self._tasks:
|
|
155 |
+ tasks = self._tasks.values()
|
|
156 |
+ print(type(tasks))
|
|
157 |
+ await asyncio.wait(tasks,
|
|
158 |
+ timeout=timeout,
|
|
159 |
+ return_when=asyncio.FIRST_COMPLETED)
|
|
160 |
+ |
|
153 | 161 |
def _update_lease_result(self, lease_id, result):
|
154 | 162 |
"""Updates the lease with the result."""
|
155 | 163 |
self._tenants[lease_id].update_lease_result(result)
|