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:
parent
0ba55d466d
commit
05759314f0
@ -11,7 +11,15 @@ def set_aux_mode():
|
|||||||
global AUX_MODE
|
global AUX_MODE
|
||||||
AUX_MODE = True
|
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
|
# rest of the project
|
||||||
from ..cleanup import CleanerAgent
|
from ..cleanup import CleanerAgent
|
||||||
from .. import images
|
from .. import images
|
||||||
|
@ -10,7 +10,7 @@ from doreah.regular import runhourly
|
|||||||
from doreah.logging import log
|
from doreah.logging import log
|
||||||
|
|
||||||
from ..pkg_global.conf import malojaconfig
|
from ..pkg_global.conf import malojaconfig
|
||||||
|
from . import no_aux_mode
|
||||||
|
|
||||||
|
|
||||||
if malojaconfig['USE_GLOBAL_CACHE']:
|
if malojaconfig['USE_GLOBAL_CACHE']:
|
||||||
@ -21,15 +21,12 @@ if malojaconfig['USE_GLOBAL_CACHE']:
|
|||||||
|
|
||||||
|
|
||||||
@runhourly
|
@runhourly
|
||||||
|
@no_aux_mode
|
||||||
def maintenance():
|
def maintenance():
|
||||||
from . import AUX_MODE
|
|
||||||
if AUX_MODE: return
|
|
||||||
print_stats()
|
print_stats()
|
||||||
trim_cache()
|
trim_cache()
|
||||||
|
|
||||||
def print_stats():
|
def print_stats():
|
||||||
from . import AUX_MODE
|
|
||||||
if AUX_MODE: return
|
|
||||||
for name,c in (('Cache',cache),('Entity Cache',entitycache)):
|
for name,c in (('Cache',cache),('Entity Cache',entitycache)):
|
||||||
hits, misses = c.get_stats()
|
hits, misses = c.get_stats()
|
||||||
log(f"{name}: Size: {len(c)} | Hits: {hits}/{hits+misses} | Estimated Memory: {human_readable_size(c)}")
|
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):
|
def cached_wrapper(inner_func):
|
||||||
from . import AUX_MODE
|
|
||||||
if AUX_MODE: return inner_func
|
|
||||||
|
|
||||||
def outer_func(*args,**kwargs):
|
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
|
# 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
|
# cache that's aware of what we're calling
|
||||||
def cached_wrapper_individual(inner_func):
|
def cached_wrapper_individual(inner_func):
|
||||||
from . import AUX_MODE
|
|
||||||
if AUX_MODE: return
|
|
||||||
|
|
||||||
def outer_func(set_arg,**kwargs):
|
def outer_func(set_arg,**kwargs):
|
||||||
if 'dbconn' in kwargs:
|
if 'dbconn' in kwargs:
|
||||||
@ -88,9 +81,8 @@ if malojaconfig['USE_GLOBAL_CACHE']:
|
|||||||
|
|
||||||
return outer_func
|
return outer_func
|
||||||
|
|
||||||
|
@no_aux_mode
|
||||||
def invalidate_caches(scrobbletime=None):
|
def invalidate_caches(scrobbletime=None):
|
||||||
from . import AUX_MODE
|
|
||||||
if AUX_MODE: return
|
|
||||||
|
|
||||||
cleared, kept = 0, 0
|
cleared, kept = 0, 0
|
||||||
for k in cache.keys():
|
for k in cache.keys():
|
||||||
@ -102,16 +94,12 @@ if malojaconfig['USE_GLOBAL_CACHE']:
|
|||||||
kept += 1
|
kept += 1
|
||||||
log(f"Invalidated {cleared} of {cleared+kept} DB cache entries")
|
log(f"Invalidated {cleared} of {cleared+kept} DB cache entries")
|
||||||
|
|
||||||
|
@no_aux_mode
|
||||||
def invalidate_entity_cache():
|
def invalidate_entity_cache():
|
||||||
from . import AUX_MODE
|
|
||||||
if AUX_MODE: return
|
|
||||||
entitycache.clear()
|
entitycache.clear()
|
||||||
|
|
||||||
|
@no_aux_mode
|
||||||
def trim_cache():
|
def trim_cache():
|
||||||
from . import AUX_MODE
|
|
||||||
if AUX_MODE: return
|
|
||||||
ramprct = psutil.virtual_memory().percent
|
ramprct = psutil.virtual_memory().percent
|
||||||
if ramprct > malojaconfig["DB_MAX_MEMORY"]:
|
if ramprct > malojaconfig["DB_MAX_MEMORY"]:
|
||||||
log(f"{ramprct}% RAM usage, clearing cache!")
|
log(f"{ramprct}% RAM usage, clearing cache!")
|
||||||
|
@ -8,6 +8,7 @@ from threading import Lock
|
|||||||
from ..pkg_global.conf import data_dir
|
from ..pkg_global.conf import data_dir
|
||||||
from .dbcache import cached_wrapper, cached_wrapper_individual, invalidate_caches, invalidate_entity_cache
|
from .dbcache import cached_wrapper, cached_wrapper_individual, invalidate_caches, invalidate_entity_cache
|
||||||
from . import exceptions as exc
|
from . import exceptions as exc
|
||||||
|
from . import no_aux_mode
|
||||||
|
|
||||||
from doreah.logging import log
|
from doreah.logging import log
|
||||||
from doreah.regular import runhourly, runmonthly
|
from doreah.regular import runhourly, runmonthly
|
||||||
@ -1361,11 +1362,9 @@ def search_album(searchterm,dbconn=None):
|
|||||||
|
|
||||||
@runhourly
|
@runhourly
|
||||||
@connection_provider
|
@connection_provider
|
||||||
|
@no_aux_mode
|
||||||
def clean_db(dbconn=None):
|
def clean_db(dbconn=None):
|
||||||
|
|
||||||
from . import AUX_MODE
|
|
||||||
if AUX_MODE: return
|
|
||||||
|
|
||||||
with SCROBBLE_LOCK:
|
with SCROBBLE_LOCK:
|
||||||
log(f"Database Cleanup...")
|
log(f"Database Cleanup...")
|
||||||
|
|
||||||
@ -1410,11 +1409,9 @@ def clean_db(dbconn=None):
|
|||||||
|
|
||||||
|
|
||||||
@runmonthly
|
@runmonthly
|
||||||
|
@no_aux_mode
|
||||||
def renormalize_names():
|
def renormalize_names():
|
||||||
|
|
||||||
from . import AUX_MODE
|
|
||||||
if AUX_MODE: return
|
|
||||||
|
|
||||||
with SCROBBLE_LOCK:
|
with SCROBBLE_LOCK:
|
||||||
with engine.begin() as conn:
|
with engine.begin() as conn:
|
||||||
rows = conn.execute(DB['artists'].select()).all()
|
rows = conn.execute(DB['artists'].select()).all()
|
||||||
|
Loading…
Reference in New Issue
Block a user