Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/actioncache/instance.py: 66.67%
27 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-22 21:04 +0000
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-22 21:04 +0000
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.
16"""
17Action Cache
18============
20Implements an in-memory action Cache
21"""
23import logging
25from ...utils import get_hash_type
28class ActionCache:
30 def __init__(self, cache):
31 """Initialises a new ActionCache instance.
33 Args:
34 cache (ActionCacheABC): The cache to use to store results.
36 """
37 self._logger = logging.getLogger(__name__)
38 self._instance_name = None
40 self._cache = cache
42 # --- Public API ---
44 @property
45 def instance_name(self):
46 return self._instance_name
48 @instance_name.setter
49 def instance_name(self, instance_name):
50 self._instance_name = instance_name
52 def setup_grpc(self):
53 self._cache.setup_grpc()
55 @property
56 def allow_updates(self):
57 return self._cache.allow_updates
59 def hash_type(self):
60 return get_hash_type()
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)
67 self._instance_name = instance_name
69 else:
70 raise AssertionError("Instance already registered")
72 def get_action_result(self, action_digest):
73 """Retrieves the cached result for an Action.
75 If there is no cached result found, returns None.
77 Args:
78 action_digest (Digest): The digest of the Action to retrieve the
79 cached result of.
81 """
82 return self._cache.get_action_result(action_digest)
84 def update_action_result(self, action_digest, action_result):
85 """Stores a result for an Action in the cache.
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.
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.
96 """
97 self._cache.update_action_result(action_digest, action_result)