Commit f093b3fc authored by Hugh Davenport's avatar Hugh Davenport

Add cronjob to clean up stale cache files

Bug #547803

Change-Id: I625e90b780505ebf3a42d4ab2419321d444c1cbd
Signed-off-by: default avatarHugh Davenport <hugh@catalyst.net.nz>
parent b6930393
......@@ -35,6 +35,7 @@ require_once(get_config('docroot') . 'artefact/lib.php');
require_once(get_config('docroot') . 'import/lib.php');
require_once(get_config('docroot') . 'export/lib.php');
require_once(get_config('docroot') . 'lib/activity.php');
require_once(get_config('docroot') . 'lib/file.php');
// This is here for debugging purposes, it allows us to fake the time to test
// cron behaviour
......
......@@ -2615,5 +2615,17 @@ function xmldb_core_upgrade($oldversion=0) {
}
}
if ($oldversion < 2011082400) {
// Add cron entry for cache cleanup
$cron = new StdClass;
$cron->callfunction = 'file_cleanup_old_cached_files';
$cron->minute = '0';
$cron->hour = '1';
$cron->day = '*';
$cron->month = '*';
$cron->dayofweek = '*';
insert_record('cron', $cron);
}
return $status;
}
......@@ -787,3 +787,59 @@ function copyr($source, $dest)
$dir->close();
return true;
}
function file_cleanup_old_cached_files() {
global $THEME;
$dirs = array('', '/profileicons');
foreach (get_all_theme_objects() as $basename => $theme) {
$dirs[] = '/profileicons/no_userphoto/' . $basename;
}
foreach ($dirs as $dir) {
$basedir = get_config('dataroot') . 'artefact/file' . $dir . '/resized/';
if (!check_dir_exists($basedir, false)) {
continue;
}
$mintime = time() - (7 * 24 * 60 * 60); // delete caches older than 1 week
// Cached files are stored in a three tier md5sum layout
// The actual files are stored in the third directory
// This loops through all three directories, then checks the files for age
// It cleans up any empty directories on the way down again
$iter1 = new DirectoryIterator($basedir);
foreach ($iter1 as $dir1) {
if ($dir1->isDot()) continue;
$dir1path = $dir1->getPath() . '/' . $dir1->getFilename();
$iter2 = new DirectoryIterator($dir1path);
foreach ($iter2 as $dir2) {
if ($dir2->isDot()) continue;
$dir2path = $dir2->getPath() . '/' . $dir2->getFilename();
$iter3 = new DirectoryIterator($dir2path);
foreach ($iter3 as $dir3) {
if ($dir3->isDot()) continue;
$dir3path = $dir3->getPath() . '/' . $dir3->getFilename();
$fileiter = new DirectoryIterator($dir3path);
foreach ($fileiter as $file) {
if ($file->isFile() && $file->getCTime() < $mintime) {
log_debug('Deleting stale cache file ' . $file->getPath() . '/' . $file->getFilename());
unlink($file->getPath() . '/' . $file->getFilename());
}
}
if (sizeof(scandir($dir3path)) <= 2) { // first 2 entries are . and ..
log_debug('Deleting empty folder ' . $dir3path);
rmdir($dir3path);
}
}
if (sizeof(scandir($dir2path)) <= 2) { // first 2 entries are . and ..
log_debug('Deleting empty folder ' . $dir2path);
rmdir($dir2path);
}
}
if (sizeof(scandir($dir1path)) <= 2) { // first 2 entries are . and ..
log_debug('Deleting empty folder ' . $dir1path);
rmdir($dir1path);
}
}
}
}
......@@ -846,6 +846,7 @@ function core_install_firstcoredata_defaults() {
'cron_check_for_updates' => array(rand(0, 59), rand(0, 23), '*', '*', '*'),
'cron_clean_internal_activity_notifications'=> array(45, 22, '*', '*', '*'),
'cron_sitemap_daily' => array(0, 1, '*', '*', '*'),
'file_cleanup_old_cached_files' => array(0, 1, '*', '*', '*'),
);
foreach ($cronjobs as $callfunction => $times) {
$cron = new StdClass;
......
......@@ -28,7 +28,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2011082300;
$config->version = 2011082400;
$config->release = '1.5.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment