From d64c0f4289aded95d51ec43bdfeb3b368acd7e09 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sun, 12 Nov 2023 22:52:36 -0500 Subject: [PATCH] fix: correct issue where database_cache may be updated in parallel --- Contents/Code/themerr_db_helper.py | 17 +++++------------ tests/conftest.py | 1 - tests/unit/test_themerr_db_helper.py | 1 - 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/Contents/Code/themerr_db_helper.py b/Contents/Code/themerr_db_helper.py index 5d307580..2a3b2ad5 100644 --- a/Contents/Code/themerr_db_helper.py +++ b/Contents/Code/themerr_db_helper.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # standard imports +from threading import Lock import time # plex debugging @@ -16,7 +17,6 @@ from typing import Union database_cache = {} -cache_updating = False last_cache_update = 0 db_field_name = dict( @@ -27,6 +27,8 @@ movie_collections={'themoviedb': 'id'}, ) +lock = Lock() + def update_cache(): # type: () -> None @@ -43,20 +45,13 @@ def update_cache(): """ Log.Info('Updating ThemerrDB cache') - global database_cache, cache_updating, last_cache_update + global last_cache_update if time.time() - last_cache_update < 3600: Log.Info('Cache updated less than an hour ago, skipping') return - if cache_updating: - while cache_updating: - Log.Info('Cache updating...') - time.sleep(1) - - cache_updating = True - - try: + with lock: for database_type, databases in db_field_name.items(): try: pages = JSON.ObjectFromURL( @@ -87,8 +82,6 @@ def update_cache(): database_cache[database_type] = {} last_cache_update = time.time() - finally: - cache_updating = False def item_exists(database_type, database, id): diff --git a/tests/conftest.py b/tests/conftest.py index efeb19b5..789825aa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -184,6 +184,5 @@ def collection_themoviedb_agent(plex, movies_themoviedb_agent, movie_themoviedb_ @pytest.fixture(scope='function') def empty_themerr_db_cache(): themerr_db_helper.database_cache = {} # reset the cache - themerr_db_helper.cache_updating = False themerr_db_helper.last_cache_update = 0 return diff --git a/tests/unit/test_themerr_db_helper.py b/tests/unit/test_themerr_db_helper.py index 97bffb19..c1bf53bc 100644 --- a/tests/unit/test_themerr_db_helper.py +++ b/tests/unit/test_themerr_db_helper.py @@ -8,7 +8,6 @@ def test_update_cache(empty_themerr_db_cache): themerr_db_helper.update_cache() assert themerr_db_helper.last_cache_update > 0, 'Cache update did not complete' - assert themerr_db_helper.cache_updating is False, 'Cache update did not complete' assert "movies" in themerr_db_helper.database_cache, 'Cache does not contain movies' assert "movie_collections" in themerr_db_helper.database_cache, 'Cache does not contain movie_collections'