Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/actioncache/instance.py: 81.48%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

27 statements  

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""" 

17Action Cache 

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

19 

20Implements an in-memory action Cache 

21""" 

22 

23import logging 

24 

25from ...utils import get_hash_type 

26 

27 

28class ActionCache: 

29 

30 def __init__(self, cache): 

31 """Initialises a new ActionCache instance. 

32 

33 Args: 

34 cache (ActionCacheABC): The cache to use to store results. 

35 

36 """ 

37 self._logger = logging.getLogger(__name__) 

38 self._instance_name = None 

39 

40 self._cache = cache 

41 

42 # --- Public API --- 

43 

44 @property 

45 def instance_name(self): 

46 return self._instance_name 

47 

48 @instance_name.setter 

49 def instance_name(self, instance_name): 

50 self._instance_name = instance_name 

51 

52 def setup_grpc(self): 

53 self._cache.setup_grpc() 

54 

55 @property 

56 def allow_updates(self): 

57 return self._cache.allow_updates 

58 

59 def hash_type(self): 

60 return get_hash_type() 

61 

62 def register_instance_with_server(self, instance_name, server): 

63 """Names and registers the action-cache instance with a given server.""" 

64 if self._instance_name is None: 

65 server.add_action_cache_instance(self, instance_name) 

66 

67 self._instance_name = instance_name 

68 

69 else: 

70 raise AssertionError("Instance already registered") 

71 

72 def get_action_result(self, action_digest): 

73 """Retrieves the cached result for an Action. 

74 

75 If there is no cached result found, returns None. 

76 

77 Args: 

78 action_digest (Digest): The digest of the Action to retrieve the 

79 cached result of. 

80 

81 """ 

82 return self._cache.get_action_result(action_digest) 

83 

84 def update_action_result(self, action_digest, action_result): 

85 """Stores a result for an Action in the cache. 

86 

87 If the result has a non-zero exit code and `cache_failed_actions` is False 

88 for this cache, the result is not cached. 

89 

90 Args: 

91 action_digest (Digest): The digest of the Action whose result is 

92 being cached. 

93 action_result (ActionResult): The result to cache for the given 

94 Action digest. 

95 

96 """ 

97 self._cache.update_action_result(action_digest, action_result)