1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Turned aux mode check into decorator

This commit is contained in:
krateng 2023-03-31 15:19:17 +02:00
parent 0ba55d466d
commit 05759314f0
3 changed files with 17 additions and 24 deletions

View File

@ -11,7 +11,15 @@ def set_aux_mode():
global AUX_MODE
AUX_MODE = True
# decorator that makes sure this function is only run in normal operation,
# not when we run a task that needs to access the database
def no_aux_mode(func):
def wrapper(*args,**kwargs):
if AUX_MODE: return
return func(*args,**kwargs)
return wrapper
# rest of the project
from ..cleanup import CleanerAgent
from .. import images

View File

@ -10,7 +10,7 @@ from doreah.regular import runhourly
from doreah.logging import log
from ..pkg_global.conf import malojaconfig
from . import no_aux_mode
if malojaconfig['USE_GLOBAL_CACHE']:
@ -21,15 +21,12 @@ if malojaconfig['USE_GLOBAL_CACHE']:
@runhourly
@no_aux_mode
def maintenance():
from . import AUX_MODE
if AUX_MODE: return
print_stats()
trim_cache()
def print_stats():
from . import AUX_MODE
if AUX_MODE: return
for name,c in (('Cache',cache),('Entity Cache',entitycache)):
hits, misses = c.get_stats()
log(f"{name}: Size: {len(c)} | Hits: {hits}/{hits+misses} | Estimated Memory: {human_readable_size(c)}")
@ -37,8 +34,6 @@ if malojaconfig['USE_GLOBAL_CACHE']:
def cached_wrapper(inner_func):
from . import AUX_MODE
if AUX_MODE: return inner_func
def outer_func(*args,**kwargs):
@ -63,8 +58,6 @@ if malojaconfig['USE_GLOBAL_CACHE']:
# we don't want a new cache entry for every single combination, but keep a common
# cache that's aware of what we're calling
def cached_wrapper_individual(inner_func):
from . import AUX_MODE
if AUX_MODE: return
def outer_func(set_arg,**kwargs):
if 'dbconn' in kwargs:
@ -88,9 +81,8 @@ if malojaconfig['USE_GLOBAL_CACHE']:
return outer_func
@no_aux_mode
def invalidate_caches(scrobbletime=None):
from . import AUX_MODE
if AUX_MODE: return
cleared, kept = 0, 0
for k in cache.keys():
@ -102,16 +94,12 @@ if malojaconfig['USE_GLOBAL_CACHE']:
kept += 1
log(f"Invalidated {cleared} of {cleared+kept} DB cache entries")
@no_aux_mode
def invalidate_entity_cache():
from . import AUX_MODE
if AUX_MODE: return
entitycache.clear()
@no_aux_mode
def trim_cache():
from . import AUX_MODE
if AUX_MODE: return
ramprct = psutil.virtual_memory().percent
if ramprct > malojaconfig["DB_MAX_MEMORY"]:
log(f"{ramprct}% RAM usage, clearing cache!")

View File

@ -8,6 +8,7 @@ from threading import Lock
from ..pkg_global.conf import data_dir
from .dbcache import cached_wrapper, cached_wrapper_individual, invalidate_caches, invalidate_entity_cache
from . import exceptions as exc
from . import no_aux_mode
from doreah.logging import log
from doreah.regular import runhourly, runmonthly
@ -1361,11 +1362,9 @@ def search_album(searchterm,dbconn=None):
@runhourly
@connection_provider
@no_aux_mode
def clean_db(dbconn=None):
from . import AUX_MODE
if AUX_MODE: return
with SCROBBLE_LOCK:
log(f"Database Cleanup...")
@ -1410,11 +1409,9 @@ def clean_db(dbconn=None):
@runmonthly
@no_aux_mode
def renormalize_names():
from . import AUX_MODE
if AUX_MODE: return
with SCROBBLE_LOCK:
with engine.begin() as conn:
rows = conn.execute(DB['artists'].select()).all()