MDL-34057 improve styles_debug concurrency

Please note style debugging is not intended for busy production sites!
This commit is contained in:
Petr Škoda 2012-06-30 09:50:18 +02:00
parent 2cbdaa77ea
commit bcf7f9f36f

View File

@ -626,30 +626,38 @@ class theme_config {
if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
}
// Prepare the CSS optimiser if it is to be used
$optimiser = null;
$candidatesheet = "$CFG->cachedir/theme/$this->name/designer.ser";
if (!empty($CFG->enablecssoptimiser) && $this->supportscssoptimisation) {
require_once($CFG->dirroot.'/lib/csslib.php');
$optimiser = new css_optimiser;
}
if (!file_exists($candidatesheet)) {
$css = $this->css_content($optimiser);
check_dir_exists(dirname($candidatesheet));
file_put_contents($candidatesheet, serialize($css));
} else if (filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) {
$candidatedir = "$CFG->cachedir/theme/$this->name";
$candidatesheet = "$candidatedir/designer.ser";
$rebuild = true;
if (file_exists($candidatesheet) and filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) {
if ($css = file_get_contents($candidatesheet)) {
$css = unserialize($css);
} else {
unlink($candidatesheet);
$css = $this->css_content($optimiser);
if (is_array($css)) {
$rebuild = false;
}
}
}
if ($rebuild) {
// Prepare the CSS optimiser if it is to be used,
// please note that it may be very slow and is therefore strongly discouraged in theme designer mode.
$optimiser = null;
if (!empty($CFG->enablecssoptimiser) && $this->supportscssoptimisation) {
require_once($CFG->dirroot.'/lib/csslib.php');
$optimiser = new css_optimiser;
}
} else {
unlink($candidatesheet);
$css = $this->css_content($optimiser);
file_put_contents($candidatesheet, serialize($css));
// We do not want any errors here because this may fail easily because of the concurrent access.
$prevabort = ignore_user_abort(true);
check_dir_exists($candidatedir);
$tempfile = tempnam($candidatedir, 'tmpdesigner');
file_put_contents($tempfile, serialize($css));
$reporting = error_reporting(0);
chmod($tempfile, $CFG->filepermissions);
unlink($candidatesheet); // Do not rely on rename() deleting original, they may decide to change it at any time as usually.
rename($tempfile, $candidatesheet);
error_reporting($reporting);
ignore_user_abort($prevabort);
}
$baseurl = $CFG->httpswwwroot.'/theme/styles_debug.php';