Skip to content

Commit

Permalink
Improve management of cache loading errors (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiRigal committed Dec 14, 2022
1 parent d81bf5c commit 7ceba6b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions plex_auto_languages/plex_server_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,17 @@ def _get_cache_file_path(self):
return os.path.join(cache_dir, self._plex.unique_id)

def _load(self):
if not os.path.exists(self._cache_file_path):
if not os.path.exists(self._cache_file_path) or not os.path.isfile(self._cache_file_path):
return False
logger.debug("[Cache] Loading server cache from file")
with open(self._cache_file_path, "r", encoding="utf-8") as stream:
cache = json.load(stream)
try:
with open(self._cache_file_path, "r", encoding="utf-8") as stream:
cache = json.load(stream)
except json.JSONDecodeError:
logger.warning("[Cache] The cache is corrupted, clearing the cache before trying again")
if os.path.exists(self._cache_file_path) and os.path.isfile(self._cache_file_path):
os.remove(self._cache_file_path)
return False
self.newly_updated = cache.get("newly_updated", self.newly_updated)
self.newly_updated = {key: isoparse(value) for key, value in self.newly_updated.items()}
self.newly_added = cache.get("newly_added", self.newly_added)
Expand Down
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import pytest
import plexapi

from plex_auto_languages.utils.logger import init_logger
from plex_auto_languages.plex_server import PlexServer
from plex_auto_languages.utils.configuration import Configuration


init_logger()

SERVER_BASEURL = plexapi.CONFIG.get("auth.server_baseurl")
SERVER_TOKEN = plexapi.CONFIG.get("auth.server_token")

Expand Down
8 changes: 8 additions & 0 deletions tests/test_plex_server_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def test_load_save():

assert old_episode_parts == cache.episode_parts

with open(mocked_path, "w") as stream:
stream.write("Not a JSON object")
assert os.path.exists(mocked_path)
mocked_refresh.reset_mock()
cache = PlexServerCache(None)
mocked_refresh.assert_called_once()
assert not os.path.exists(mocked_path)


def test_instance_users(plex):
assert plex.cache.get_instance_users() is None
Expand Down

1 comment on commit 7ceba6b

@CardcaptorRLH85
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this works, my cache managed to corrupt itself again since manually clearing the issue.

Please sign in to comment.