Cleaned up logging

This commit is contained in:
Tris 2021-03-12 16:21:03 +11:00
parent c077cf5cc4
commit 7334271f68
2 changed files with 12 additions and 10 deletions

View File

@ -13,15 +13,16 @@ logger = logging.getLogger(__name__)
@deconstructible @deconstructible
class CachedStorage(Storage): class CachedStorage(Storage):
CACHE_EXPIRES = 30 def __init__(self, remote=None, cachedir=None, expires=600):
def __init__(self, remote=None, cachedir=None):
if not remote: if not remote:
remote = settings.CACHED_STORAGE_REMOTE remote = settings.CACHED_STORAGE_REMOTE
if not cachedir: if not cachedir:
cachedir = settings.CACHED_STORAGE_DIR cachedir = settings.CACHED_STORAGE_DIR
self.remote = get_storage_class(remote)() self.remote = get_storage_class(remote)()
self.cachedir = cachedir self.cachedir = cachedir
self.expires = expires
os.makedirs(self.cachedir, exist_ok=True) os.makedirs(self.cachedir, exist_ok=True)
self.clean() self.clean()
@ -89,12 +90,12 @@ class CachedStorage(Storage):
def clean(self): def clean(self):
now = time.time() now = time.time()
threshold = now - self.CACHE_EXPIRES threshold = now - self.expires
logger.info("Removing cached files older than %d seconds", self.CACHE_EXPIRES) logger.info("Removing cached files older than %d seconds", self.expires)
for f in os.listdir(self.cachedir): for f in os.listdir(self.cachedir):
f = os.path.join(self.cachedir, f) f = os.path.join(self.cachedir, f)
s = os.stat(f) s = os.stat(f)
if s.st_atime < threshold: if s.st_atime < threshold:
logger.debug("Removing %s", f) logger.debug("Removing %s", f)
os.unlink(f) os.unlink(f)
self.next_check = now + 300 self.next_check = now + 300 # wait at least 5 minutes before running again

View File

@ -6,6 +6,9 @@ from django.utils.deconstruct import deconstructible
TODO: Create a signal to remove instances from cache if modified TODO: Create a signal to remove instances from cache if modified
''' '''
import logging
logger = logging.getLogger(__name__)
@deconstructible @deconstructible
class MultiStorage(Storage): class MultiStorage(Storage):
''' Django storage class that proxies multiple storage classes. ''' Django storage class that proxies multiple storage classes.
@ -36,17 +39,15 @@ class MultiStorage(Storage):
return self.DELIM.join((storage, name)) return self.DELIM.join((storage, name))
def _proxy(self, method, name, *args, **kwargs): def _proxy(self, method, name, *args, **kwargs):
print('PROXY', method, name, *args, **kwargs)
storage, p = self.split(name) storage, p = self.split(name)
result = getattr(self.get_storage(storage), method)(p, *args, **kwargs) result = getattr(self.get_storage(storage), method)(p, *args, **kwargs)
print("RESULT", result) logger.debug("Proxy: %s(%r, *%r, **%r) => %r", method, name, args, kwargs, result)
return result return result
def _proxy_name(self, method, name, *args, **kwargs): def _proxy_name(self, method, name, *args, **kwargs):
print('PROXY_NAME', method, name, *args, **kwargs)
storage, p = self.split(name) storage, p = self.split(name)
result = getattr(self.get_storage(storage), method)(p, *args, **kwargs) result = getattr(self.get_storage(storage), method)(p, *args, **kwargs)
print("RESULT", result) logger.debug("Proxy: %s(%r, *%r, **%r) => %s:%s", method, name, args, kwargs, storage, result)
return self.join(storage, result) return self.join(storage, result)
def _open(self, name, mode='rb'): def _open(self, name, mode='rb'):