diff options
| author | Nakidai <nakidai@disroot.org> | 2024-10-27 22:58:23 +0300 |
|---|---|---|
| committer | Nakidai <nakidai@disroot.org> | 2024-10-27 22:58:23 +0300 |
| commit | 6c0d85a696f5622a5959a49b6be4bb4c0a67393e (patch) | |
| tree | f2ab0660a0d1be3390a4f178efc0ce87b4de4667 | |
| parent | a42d6702222c344d1f68cfd5aafc1dfb0e0fc2a5 (diff) | |
| download | petthecord-6c0d85a696f5622a5959a49b6be4bb4c0a67393e.tar.gz petthecord-6c0d85a696f5622a5959a49b6be4bb4c0a67393e.zip | |
Probably fix #1
| -rw-r--r-- | src/petthecord/server.py | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/src/petthecord/server.py b/src/petthecord/server.py index 47f449f..18ec2a2 100644 --- a/src/petthecord/server.py +++ b/src/petthecord/server.py @@ -2,7 +2,7 @@ from asyncio import sleep from json import load, dumps from io import BytesIO from logging import getLogger -from os import listdir, remove +from os import listdir, makedirs, remove from os.path import getmtime from pathlib import Path from time import time @@ -26,7 +26,7 @@ class Server(Application): ) -> None: self.client = client self.caching = caching - self.cache_path = Path(cache_path) + self.cache_path = Path(cache_path).resolve() self.cache_lifetime = cache_lifetime self.cache_gc_delay = cache_gc_delay super().__init__() @@ -36,10 +36,17 @@ class Server(Application): if self.caching: index_path = self.cache_path / "index.json" - if not index_path.exists(): - self._cache_logger.warning("Cache's index.json doesn't exist, creating...") - with open(index_path, "w") as f: - f.write("{}") + try: + if not index_path.exists(): + self._cache_logger.warning("index.json doesn't exist, creating...") + if not self.cache_path.exists(): + self._cache_logger.warning(f"{self.cache_path} doesn't exist, creating..") + makedirs(self.cache_path, mode=0o755, exist_ok=True) + with open(index_path, "w") as f: + f.write("{}") + except OSError: + self._cache_logger.error("Cannot create environment, turning caching off") + self.caching = False with open(index_path, "r") as f: self.cache = load(f) @@ -100,18 +107,19 @@ class Server(Application): return Response(body=f.read(), content_type="image/gif") async def clean_cache(self) -> None: - self._cache_logger.info("Starting new cache's gc iteration") - - for filename in listdir(self.cache_path): - path = (self.cache_path / filename) - if path.is_file() and filename != "index.json": - if (time() - getmtime(path) > self.cache_lifetime): - self._cache_logger.debug(f"Removing {filename}") - del self.cache[filename.split('_')[0]] - remove(path) - with open(self.cache_path / "index.json", "w") as f: - f.write(dumps(self.cache)) - - self._cache_logger.debug("Finished collecting old cache") + if self.caching: + self._cache_logger.info("Starting new cache's gc iteration") + + for filename in listdir(self.cache_path): + path = (self.cache_path / filename) + if path.is_file() and filename != "index.json": + if (time() - getmtime(path) > self.cache_lifetime): + self._cache_logger.debug(f"Removing {filename}") + del self.cache[filename.split('_')[0]] + remove(path) + with open(self.cache_path / "index.json", "w") as f: + f.write(dumps(self.cache)) + + self._cache_logger.debug("Finished collecting old cache") await sleep(self.cache_gc_delay) |