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) 2019 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 

16from contextlib import contextmanager 

17 

18import grpc 

19 

20from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2, remote_execution_pb2_grpc 

21 

22 

23@contextmanager 

24def query(channel, instance=None): 

25 """Context manager generator for the :class:`ActionCacheClient` class.""" 

26 client = ActionCacheClient(channel, instance=instance) 

27 try: 

28 yield client 

29 finally: 

30 client.close() 

31 

32 

33class ActionCacheClient: 

34 """Remote ActionCache service client helper. 

35 

36 The :class:`ActionCacheClient` class comes with a generator factory function 

37 that can be used together with the `with` statement for context management:: 

38 

39 from buildgrid.client.actioncache import query 

40 

41 with query(channel, instance='build') as action_cache: 

42 digest, action_result = action_cache.get(action_digest) 

43 """ 

44 

45 def __init__(self, channel, instance=None): 

46 """Initializes a new :class:`ActionCacheClient` instance. 

47 

48 Args: 

49 channel (grpc.Channel): a gRPC channel to the ActionCache endpoint. 

50 instance (str, optional): the targeted instance's name. 

51 """ 

52 self.channel = channel 

53 

54 self.instance_name = instance 

55 

56 self.__actioncache_stub = remote_execution_pb2_grpc.ActionCacheStub(self.channel) 

57 

58 # --- Public API --- 

59 

60 def get(self, action_digest): 

61 """Retrieves the cached :obj:`ActionResult` for a given :obj:`Action`. 

62 

63 Args: 

64 action_digest (:obj:`Digest`): the action's digest to query. 

65 

66 Returns: 

67 :obj:`ActionResult`: the cached result or None if not found. 

68 

69 Raises: 

70 grpc.RpcError: on any network or remote service error. 

71 """ 

72 request = remote_execution_pb2.GetActionResultRequest() 

73 if self.instance_name: 

74 request.instance_name = self.instance_name 

75 request.action_digest.CopyFrom(action_digest) 

76 

77 try: 

78 return self.__actioncache_stub.GetActionResult(request) 

79 

80 except grpc.RpcError as e: 

81 status_code = e.code() 

82 if status_code != grpc.StatusCode.NOT_FOUND: 

83 raise ConnectionError(e.details()) 

84 

85 return None 

86 

87 def update(self, action_digest, action_result): 

88 """Maps in cache an :obj:`Action` to an :obj:`ActionResult`. 

89 

90 Args: 

91 action_digest (:obj:`Digest`): the action's digest to update. 

92 action_result (:obj:`ActionResult`): the action's result. 

93 

94 Returns: 

95 :obj:`ActionResult`: the cached result or None on failure. 

96 

97 Raises: 

98 grpc.RpcError: on any network or remote service error. 

99 """ 

100 request = remote_execution_pb2.UpdateActionResultRequest() 

101 if self.instance_name: 

102 request.instance_name = self.instance_name 

103 request.action_digest.CopyFrom(action_digest) 

104 request.action_result.CopyFrom(action_result) 

105 

106 try: 

107 return self.__actioncache_stub.UpdateActionResult(request) 

108 

109 except grpc.RpcError as e: 

110 status_code = e.code() 

111 if status_code != grpc.StatusCode.NOT_FOUND: 

112 raise ConnectionError(e.details()) 

113 

114 return None 

115 

116 def close(self): 

117 """Closes the underlying connection stubs.""" 

118 self.__actioncache_stub = None