Skip to content

Commit

Permalink
Add lock on plex server library cache refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiRigal committed May 23, 2022
1 parent 4f92abb commit 7d84120
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions plex_auto_languages/plex_server_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class PlexServerCache():

def __init__(self, plex: PlexServer):
self._is_refreshing = False
self._encoder = DateTimeEncoder()
self._plex = plex
self._cache_file_path = self._get_cache_file_path()
Expand Down Expand Up @@ -51,6 +52,10 @@ def should_process_recently_updated(self, episode_id: str):
return True

def refresh_library_cache(self):
if self._is_refreshing:
logger.debug("[Cache] The library cache is already being refreshed")
return [], []
self._is_refreshing = True
logger.debug("[Cache] Refreshing library cache")
added = []
updated = []
Expand All @@ -67,6 +72,7 @@ def refresh_library_cache(self):
logger.debug("[Cache] Done refreshing library cache")
self._last_refresh = datetime.now()
self.save()
self._is_refreshing = False
return added, updated

def _get_cache_file_path(self):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_plex_server_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def test_episode_parts(plex):
assert len(plex.cache.episode_parts) == 46


def test_is_refreshing(plex):
plex.cache._is_refreshing = True
assert plex.cache.refresh_library_cache() == ([], [])


def test_cache_dir(plex):
mocked_path = "/tmp/mocked_cache_dir"
plex.config._config["data_dir"] = mocked_path
Expand Down

0 comments on commit 7d84120

Please sign in to comment.