diffs: make validation of version, so we can change diffs and force re-cache if diffs are in old version.

This commit is contained in:
Marcin Kuzminski 2018-09-24 15:59:38 +02:00
parent 5a4a98b40e
commit b4ef64817c

View file

@ -1134,10 +1134,23 @@ class DiffLimitExceeded(Exception):
pass
# NOTE(marcink): if diffs.mako change, probably this
# needs a bump to next version
CURRENT_DIFF_VERSION = 'v1'
def _cleanup_cache_file(cached_diff_file):
# cleanup file to not store it "damaged"
try:
os.remove(cached_diff_file)
except Exception:
log.exception('Failed to cleanup path %s', cached_diff_file)
def cache_diff(cached_diff_file, diff, commits):
struct = {
'version': 'v1',
'version': CURRENT_DIFF_VERSION,
'diff': diff,
'commits': commits
}
@ -1148,17 +1161,13 @@ def cache_diff(cached_diff_file, diff, commits):
log.debug('Saved diff cache under %s', cached_diff_file)
except Exception:
log.warn('Failed to save cache', exc_info=True)
# cleanup file to not store it "damaged"
try:
os.remove(cached_diff_file)
except Exception:
log.exception('Failed to cleanup path %s', cached_diff_file)
_cleanup_cache_file(cached_diff_file)
def load_cached_diff(cached_diff_file):
default_struct = {
'version': 'v1',
'version': CURRENT_DIFF_VERSION,
'diff': None,
'commits': None
}
@ -1182,6 +1191,12 @@ def load_cached_diff(cached_diff_file):
# old version of data ?
data = default_struct
# check version
if data.get('version') != CURRENT_DIFF_VERSION:
# purge cache
_cleanup_cache_file(cached_diff_file)
return default_struct
return data