Coverage for /builds/BuildGrid/buildgrid/buildgrid/client/logstream.py: 38.46%

26 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-22 21:04 +0000

1# Copyright (C) 2020 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 

17import logging 

18 

19from grpc import RpcError 

20 

21from buildgrid._protos.build.bazel.remote.logstream.v1.remote_logstream_pb2 import CreateLogStreamRequest 

22from buildgrid._protos.build.bazel.remote.logstream.v1.remote_logstream_pb2_grpc import LogStreamServiceStub 

23 

24 

25@contextmanager 

26def logstream_client(channel, instance_name): 

27 client = LogStreamClient(channel, instance_name) 

28 try: 

29 yield client 

30 finally: 

31 client.close() 

32 

33 

34class LogStreamClient: 

35 

36 def __init__(self, channel, instance_name=''): 

37 self._channel = channel 

38 self._instance_name = instance_name 

39 self._logger = logging.getLogger(__name__) 

40 self._logstream_stub = LogStreamServiceStub(self._channel) 

41 

42 def create(self, parent): 

43 parent = f"{self._instance_name}/{parent}" 

44 request = CreateLogStreamRequest(parent=parent) 

45 try: 

46 return self._logstream_stub.CreateLogStream(request) 

47 except RpcError as e: 

48 self._logger.exception(f"Error creating a LogStream: {e.details()}") 

49 raise ConnectionError(e.details()) 

50 

51 def close(self): 

52 self._logstream_stub = None