fix: inability to reload provider dynamically when using cached sessions

This commit is contained in:
Benex254
2024-09-29 21:19:15 +03:00
parent d0f5366908
commit ef422ed6fd
2 changed files with 11 additions and 4 deletions

View File

@@ -1,7 +1,4 @@
"""An abstraction over all providers offering added features with a simple and well typed api
[TODO:description]
"""
"""An abstraction over all providers offering added features with a simple and well typed api"""
import importlib
import logging
@@ -40,6 +37,10 @@ class AnimeProvider:
def lazyload_provider(self, provider):
"""updates the current provider being used"""
try:
self.anime_provider.session.kill_connection_to_db()
except Exception:
pass
_, anime_provider_cls_name = anime_sources[provider].split(".", 1)
package = f"fastanime.libs.anime_provider.{provider}"
provider_api = importlib.import_module(".api", package)

View File

@@ -60,6 +60,7 @@ class CachedRequestsSession(requests.Session):
):
super().__init__(*args, **kwargs)
self.lockfile_path = cache_db_lock_file
self.cache_db_path = cache_db_path
self.max_lifetime = max_lifetime
self.max_size = max_size
@@ -201,6 +202,11 @@ class CachedRequestsSession(requests.Session):
content in caching_mimetypes[mime] for content in contents.split("+")
)
def kill_connection_to_db(self):
lockfile_path = pathlib.Path(self.lockfile_path)
self.connection.close()
lockfile_path.unlink(missing_ok=True)
@staticmethod
def acquirer_lock(lock_file: str):
"""the function creates a lock file preventing other instances of the cacher from running at the same time"""