Fix: Better error handling for webassets + cache

This commit is contained in:
Gina Häußge 2015-08-11 12:35:35 +02:00
parent 7f2476e513
commit 38be47c01b
2 changed files with 26 additions and 14 deletions

View file

@ -740,13 +740,10 @@ class Server():
for entry in ("webassets", ".webassets-cache"): for entry in ("webassets", ".webassets-cache"):
path = os.path.join(base_folder, entry) path = os.path.join(base_folder, entry)
self._logger.debug("Deleting {path}...".format(**locals())) self._logger.debug("Deleting {path}...".format(**locals()))
if os.path.isdir(path): shutil.rmtree(path, ignore_errors=True)
shutil.rmtree(path, ignore_errors=True) self._logger.debug("Creating {path}...".format(**locals()))
elif os.path.isfile(path): os.makedirs(path)
try: self._logger.info("Reset webasset folder {path}...".format(**locals()))
os.remove(path)
except:
self._logger.exception("Exception while trying to delete {entry} from {base_folder}".format(**locals()))
AdjustedEnvironment = type(Environment)(Environment.__name__, (Environment,), dict( AdjustedEnvironment = type(Environment)(Environment.__name__, (Environment,), dict(
resolver_class=util.flask.PluginAssetResolver resolver_class=util.flask.PluginAssetResolver

View file

@ -123,12 +123,19 @@ def enable_additional_translations(default_locale="en", additional_folders=None)
def fix_webassets_cache(): def fix_webassets_cache():
from webassets import cache from webassets import cache
import os
import tempfile error_logger = logging.getLogger(__name__ + ".fix_webassets_cache")
import pickle
import shutil
def fixed_set(self, key, data): def fixed_set(self, key, data):
import os
import tempfile
import pickle
import shutil
if not os.path.exists(self.directory):
error_logger.warn("Cache directory {} doesn't exist, not going "
"to attempt to write cache file".format(self.directory))
md5 = '%s' % cache.make_md5(self.V, key) md5 = '%s' % cache.make_md5(self.V, key)
filename = os.path.join(self.directory, md5) filename = os.path.join(self.directory, md5)
fd, temp_filename = tempfile.mkstemp(prefix='.' + md5, fd, temp_filename = tempfile.mkstemp(prefix='.' + md5,
@ -148,6 +155,11 @@ def fix_webassets_cache():
import warnings import warnings
from webassets.cache import make_md5 from webassets.cache import make_md5
if not os.path.exists(self.directory):
error_logger.warn("Cache directory {} doesn't exist, not going "
"to attempt to read cache file".format(self.directory))
return None
try: try:
hash = make_md5(self.V, key) hash = make_md5(self.V, key)
except IOError as e: except IOError as e:
@ -194,12 +206,15 @@ def fix_webassets_filtertool():
try: try:
content = func().getvalue() content = func().getvalue()
if self.cache: if self.cache:
log.debug('Storing result in cache with key %s', key,) try:
self.cache.set(key, content) log.debug('Storing result in cache with key %s', key,)
self.cache.set(key, content)
except:
error_logger.exception("Got an exception while trying to save file to cache, not caching")
return MemoryHunk(content) return MemoryHunk(content)
except: except:
error_logger.exception("Got an exception while trying to apply filter, ignoring file") error_logger.exception("Got an exception while trying to apply filter, ignoring file")
return MemoryHunk("") return MemoryHunk(u"")
FilterTool._wrap_cache = fixed_wrap_cache FilterTool._wrap_cache = fixed_wrap_cache