Skip to content

Commit

Permalink
clean out the cache when the files get deleted by the cron (bug 670578)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy McKay committed Jul 13, 2011
1 parent feaba3d commit 972e262
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
24 changes: 20 additions & 4 deletions apps/files/cron.py
@@ -1,9 +1,11 @@
import hashlib
import os
import shutil
import stat
import time

from django.conf import settings
from django.core.cache import cache

import cronjobs
import commonware.log
Expand All @@ -17,8 +19,22 @@ def cleanup_extracted_file():
log.info('Removing extracted files for file viewer.')
root = os.path.join(settings.TMP_PATH, 'file_viewer')
for path in os.listdir(root):
path = os.path.join(root, path)
age = time.time() - os.stat(path)[stat.ST_ATIME]
full = os.path.join(root, path)
age = time.time() - os.stat(full)[stat.ST_ATIME]
if (age) > (60 * 60):
log.info('Removing extracted files: %s, %dsecs old.' % (path, age))
shutil.rmtree(path)
log.info('Removing extracted files: %s, %dsecs old.' % (full, age))
shutil.rmtree(full)
# Nuke out the file and diff caches when the file gets removed.
id = os.path.basename(path)
try:
int(id)
except ValueError:
continue
for prefix in ['file-viewer-get-deleted-files',
'file-viewer-get-files',
'file-viewer']:
key = hashlib.md5()
key.update(str(id))
cache.delete('%s:memoize:%s:%s' % (settings.CACHE_PREFIX,
prefix, key.hexdigest()))
log.info('Removing cache file-viewer cache entries for: %s' % id)
8 changes: 6 additions & 2 deletions apps/files/helpers.py
Expand Up @@ -189,12 +189,16 @@ def get_files(self):
addon-file. Full of all the useful information you'll need to serve
this file, build templates etc.
"""
if self._files:
return self._files

if not self.is_extracted():
return {}
# In case a cron job comes along and deletes the files
# mid tree building.
try:
return self._get_files()
self._files = self._get_files()
return self._files
except (OSError, IOError):
return {}

Expand Down Expand Up @@ -281,7 +285,7 @@ def __init__(self, left, right):
self.key = None

def __str__(self):
return '%s:%s' % (self.left, self.right)
return str(self.left)

def extract(self):
self.left.extract(), self.right.extract()
Expand Down

0 comments on commit 972e262

Please sign in to comment.