From ff96eff17640e1987a7c36b603dd59a6890db86b Mon Sep 17 00:00:00 2001 From: Daniel Czerwonk Date: Mon, 30 Oct 2023 11:23:39 +0100 Subject: [PATCH] improvement: do not fail request when cache item can not be persisted --- thumbor/cache/expire_file.py | 2 +- thumbor/cache/file_cache.py | 2 +- thumbor/cache/prune_cache.py | 2 +- .../result_storages/file_storage_cache_control.py | 11 +++++++---- thumbor/storages/file_storage_cache_control.py | 13 +++++++++---- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/thumbor/cache/expire_file.py b/thumbor/cache/expire_file.py index 9f81b2321..df6ed35bf 100644 --- a/thumbor/cache/expire_file.py +++ b/thumbor/cache/expire_file.py @@ -55,7 +55,7 @@ def __write(self, _file): max_age = 0 _file.write(str.encode(str(max_age))) - + if self.max_age_shared is not None: _file.write(str.encode("," + str(self.max_age_shared))) diff --git a/thumbor/cache/file_cache.py b/thumbor/cache/file_cache.py index a8a2187b4..8d0d53239 100644 --- a/thumbor/cache/file_cache.py +++ b/thumbor/cache/file_cache.py @@ -52,7 +52,7 @@ def get(self, path): exists, max_age, max_age_shared = self.exists(path) if not exists: return FileCacheResult(False) - + with open(path, "rb") as source_file: return FileCacheResult(True, source_file.read(), max_age, max_age_shared) diff --git a/thumbor/cache/prune_cache.py b/thumbor/cache/prune_cache.py index 8d7e6386d..2492dc0e2 100644 --- a/thumbor/cache/prune_cache.py +++ b/thumbor/cache/prune_cache.py @@ -34,7 +34,7 @@ def prune_expired_links(dir: str, file_cache: FileCache): if os.path.isdir(f) and not name == "files": prune_expired_links(f, file_cache) continue - + if os.path.isfile(f) and f.endswith(file_cache.EXPIRE_EXT): prune_file_if_expired(f, file_cache) continue diff --git a/thumbor/result_storages/file_storage_cache_control.py b/thumbor/result_storages/file_storage_cache_control.py index 7fd9e1bc2..85cf6125f 100644 --- a/thumbor/result_storages/file_storage_cache_control.py +++ b/thumbor/result_storages/file_storage_cache_control.py @@ -42,10 +42,13 @@ async def put(self, image_bytes): return symlink_abspath = self.normalize_path(self.context.request.url) - self.cache.put(symlink_abspath, - image_bytes, - self.context.request.max_age, - self.context.request.max_age_shared) + try: + self.cache.put(symlink_abspath, + image_bytes, + self.context.request.max_age, + self.context.request.max_age_shared) + except IOError as e: + logger.error("[RESULT_STORAGE] error persisting item to result cache: %s", e.strerror) async def get(self): diff --git a/thumbor/storages/file_storage_cache_control.py b/thumbor/storages/file_storage_cache_control.py index 3d54bb654..6c97efc0d 100644 --- a/thumbor/storages/file_storage_cache_control.py +++ b/thumbor/storages/file_storage_cache_control.py @@ -36,10 +36,15 @@ async def put(self, path, file_bytes): return file_abspath = self.path_on_filesystem(path) - self.cache.put(file_abspath, - file_bytes, - self.context.request.max_age, - self.context.request.max_age_shared) + try: + self.cache.put(file_abspath, + file_bytes, + self.context.request.max_age, + self.context.request.max_age_shared) + except IOError as e: + logger.error("[STORAGE] error persisting cache item: %s", e.strerror) + return + return path