Skip to content

Commit

Permalink
swatbotrest: Invalidate all stepfailures lists when pushing new triage
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Dubois-Briand <[email protected]>
  • Loading branch information
mbriand committed Nov 8, 2024
1 parent 6f4b3a5 commit de4cdbd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion swattool/swatbotrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def invalidate_stepfailures_cache():
This can be used to force fetching failures on next build, when we suspect
it might have changed remotely.
"""
Session().invalidate_cache(f"{REST_BASE_URL}/stepfailure/")
Session().invalidate_cache(f"{REST_BASE_URL}/stepfailure/", allparams=True)


def get_stepfailures(status: Optional[TriageStatus] = None,
Expand Down
24 changes: 17 additions & 7 deletions swattool/webrequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,35 @@ def save_cookies(self):
with COOKIESFILE.open('wb') as file:
pickle.dump(self.session.cookies, file)

def invalidate_cache(self, url: str):
def invalidate_cache(self, url: str, allparams: bool = False):
"""Invalidate cache for a given URL."""
with cache_lock:
for file in self._get_cache_file_candidates(url):
file.unlink(missing_ok=True)
if allparams:
prefix = self._get_cache_file_prefix(url)
for file in prefix.parent.glob(f"{prefix.name}*"):
file.unlink()
else:
for file in self._get_cache_file_candidates(url):
file.unlink(missing_ok=True)

def _get_cache_file_candidates(self, url: str) -> list[pathlib.Path]:
def _get_cache_file_prefix(self, url: str) -> pathlib.Path:
filestem = url.split('://', 1)[1].replace('/', '_').replace(':', '_')

if len(filestem) > 100:
hashname = hashlib.sha256(filestem.encode(), usedforsecurity=False)
filestem = hashname.hexdigest()

return utils.CACHEDIR / filestem

def _get_cache_file_candidates(self, url: str) -> list[pathlib.Path]:
prefix = self._get_cache_file_prefix(url)

candidates = [
utils.CACHEDIR / f"{filestem}.gz",
utils.CACHEDIR / filestem,
prefix.with_suffix('.gz'),
prefix,

# For compatibility with old cache files
utils.CACHEDIR / f"{filestem}.json",
prefix.with_suffix('.json'),
]

return candidates
Expand Down

0 comments on commit de4cdbd

Please sign in to comment.