Skip to content
Snippets Groups Projects
Commit 2d9cb0c0 authored by Tiago Gomes's avatar Tiago Gomes
Browse files

artifactcache: replace set_cache_size()

Replace set_cache_size() with subtract_cache_size(), for symmetry with
add_artifact_size().
parent fccb722a
No related branches found
No related tags found
Loading
...@@ -234,7 +234,7 @@ class ArtifactCache(): ...@@ -234,7 +234,7 @@ class ArtifactCache():
# Clean the artifact cache as much as possible. # Clean the artifact cache as much as possible.
# #
# Returns: # Returns:
# (int): The size of the cache after having cleaned up # (int): Amount of bytes cleaned from the cache.
# #
def clean(self): def clean(self):
artifacts = self.list_artifacts() artifacts = self.list_artifacts()
...@@ -252,7 +252,7 @@ class ArtifactCache(): ...@@ -252,7 +252,7 @@ class ArtifactCache():
]) ])
# Do a real computation of the cache size once, just in case # Do a real computation of the cache size once, just in case
self.compute_cache_size() old_cache_size = self.compute_cache_size()
while self.get_cache_size() >= self._cache_lower_threshold: while self.get_cache_size() >= self._cache_lower_threshold:
try: try:
...@@ -280,12 +280,9 @@ class ArtifactCache(): ...@@ -280,12 +280,9 @@ class ArtifactCache():
# Remove the actual artifact, if it's not required. # Remove the actual artifact, if it's not required.
size = self.remove(to_remove) size = self.remove(to_remove)
self._cache_size -= size
# Remove the size from the removed size return old_cache_size - self._cache_size
self.set_cache_size(self._cache_size - size)
# This should be O(1) if implemented correctly
return self.get_cache_size()
# compute_cache_size() # compute_cache_size()
# #
...@@ -302,17 +299,24 @@ class ArtifactCache(): ...@@ -302,17 +299,24 @@ class ArtifactCache():
# add_artifact_size() # add_artifact_size()
# #
# Adds the reported size of a newly cached artifact to the # Adds given artifact size to the cache size
# current cache size.
# #
# Args: # Args:
# artifact_size (int): The size to add. # artifact_size (int): The artifact size to add.
# #
def add_artifact_size(self, artifact_size): def add_artifact_size(self, artifact_size):
cache_size = self.get_cache_size() self._cache_size = self.get_cache_size() + artifact_size
cache_size += artifact_size self._write_cache_size(self._cache_size)
self.set_cache_size(cache_size) # subtract_artifact_size()
#
# Subtracts given artifact size from the cache size
#
# Args:
# artifact_size (int): The artifact size to subtract.
#
def subtract_artifact_size(self, artifact_size):
self.add_artifact_size(artifact_size * -1)
# get_cache_size() # get_cache_size()
# #
...@@ -330,23 +334,6 @@ class ArtifactCache(): ...@@ -330,23 +334,6 @@ class ArtifactCache():
return self._cache_size return self._cache_size
# set_cache_size()
#
# Forcefully set the overall cache size.
#
# This is used to update the size in the main process after
# having calculated in a cleanup or a cache size calculation job.
#
# Args:
# cache_size (int): The size to set.
#
def set_cache_size(self, cache_size):
assert cache_size is not None
self._cache_size = cache_size
self._write_cache_size(self._cache_size)
# has_quota_exceeded() # has_quota_exceeded()
# #
# Checks if the current artifact cache size exceeds the quota. # Checks if the current artifact cache size exceeds the quota.
......
...@@ -32,4 +32,4 @@ class CleanupJob(Job): ...@@ -32,4 +32,4 @@ class CleanupJob(Job):
def parent_complete(self, success, result): def parent_complete(self, success, result):
if success: if success:
self._artifacts.set_cache_size(result) self._artifacts.subtract_artifact_size(result)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment