Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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. 

14 

15 

16""" 

17Bot Interface 

18============= 

19 

20Interface to grpc 

21""" 

22 

23import logging 

24import grpc 

25 

26from buildgrid._protos.google.devtools.remoteworkers.v1test2 import bots_pb2, bots_pb2_grpc 

27from buildgrid.settings import NETWORK_TIMEOUT 

28 

29 

30class BotInterface: 

31 """ 

32 Interface handles calls to the server. 

33 """ 

34 

35 def __init__(self, channel, interval, executing_interval): 

36 self.__logger = logging.getLogger(__name__) 

37 

38 self._stub = bots_pb2_grpc.BotsStub(channel) 

39 self.__interval = interval 

40 self.__executing_interval = executing_interval 

41 

42 @property 

43 def interval(self): 

44 return self.__interval 

45 

46 @property 

47 def executing_interval(self): 

48 return self.__executing_interval 

49 

50 def create_bot_session(self, parent, bot_session): 

51 """ Create bot session request 

52 Returns BotSession if correct else a grpc StatusCode 

53 """ 

54 request = bots_pb2.CreateBotSessionRequest(parent=parent, 

55 bot_session=bot_session) 

56 return self._bot_call(self._stub.CreateBotSession, request) 

57 

58 def update_bot_session(self, bot_session, update_mask=None): 

59 """ Update bot session request 

60 Returns BotSession if correct else a grpc StatusCode 

61 """ 

62 request = bots_pb2.UpdateBotSessionRequest(name=bot_session.name, 

63 bot_session=bot_session, 

64 update_mask=update_mask) 

65 return self._bot_call(self._stub.UpdateBotSession, request) 

66 

67 def _bot_call(self, call, request): 

68 try: 

69 return call(request, timeout=self.interval + NETWORK_TIMEOUT) 

70 except grpc.RpcError as e: 

71 self.__logger.error(e) 

72 return e.code()