Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
fix: correct issue where database_cache may be updated in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Nov 13, 2023
1 parent 9057aed commit d64c0f4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
17 changes: 5 additions & 12 deletions Contents/Code/themerr_db_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

# standard imports
from threading import Lock
import time

# plex debugging
Expand All @@ -16,7 +17,6 @@
from typing import Union

database_cache = {}
cache_updating = False
last_cache_update = 0

db_field_name = dict(
Expand All @@ -27,6 +27,8 @@
movie_collections={'themoviedb': 'id'},
)

lock = Lock()


def update_cache():
# type: () -> None
Expand All @@ -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(
Expand Down Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion tests/unit/test_themerr_db_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit d64c0f4

Please sign in to comment.