API CHANGE: Added increase_time_limit_to(), which respects safe_mode.

API CHANGE: Added no-arg option to increase_memory_limit_to()

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@80241 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-06-28 02:36:46 +00:00
parent 66b25f1e62
commit 73cfe3ab84
8 changed files with 63 additions and 21 deletions

View File

@ -293,11 +293,16 @@ function _t($entity, $string = "", $priority = 40, $context = "") {
/**
* Increase the memory limit to the given level if it's currently too low.
* @param A memory limit string, such as "64M"
* @param A memory limit string, such as "64M". If omitted, unlimited memory will be set.
*/
function increase_memory_limit_to($memoryLimit) {
function increase_memory_limit_to($memoryLimit = -1) {
$curLimit = ini_get('memory_limit');
// Can't go higher than infinite
if($curLimit == -1) return;
// Increase the memory limit if it's too low
if(translate_memstring($memoryLimit) > translate_memstring(ini_get('memory_limit'))) {
if($memoryLimit == -1 || translate_memstring($memoryLimit) > translate_memstring($curLimit)) {
ini_set('memory_limit', $memoryLimit);
}
}
@ -315,4 +320,22 @@ function translate_memstring($memString) {
}
}
/**
* Increase the time limit of this script. By default, the time will be unlimited.
* @param $timeLimit The time limit in seconds. If omitted, no time limit will be set.
*/
function increase_time_limit_to($timeLimit = null) {
if(!ini_get('safe_mode')) {
if(!$timeLimit) {
set_time_limit(0);
} else {
$currTimeLimit = ini_get('max_execution_time');
if($currTimeLimit && $currTimeLimit < $timeLimit) {
set_time_limit($timeLimit);
}
}
}
}
?>

View File

@ -882,7 +882,7 @@ class Requirements_Backend {
if(stripos($file, '.js') && $this->combine_js_with_jsmin) {
require_once('thirdparty/jsmin/JSMin.php');
set_time_limit(0);
increase_time_limit_to();
$fileContent = JSMin::minify($fileContent);
}
// write a header comment for each file for easier identification and debugging

View File

@ -25,7 +25,7 @@ class i18nTextCollectorTask extends BuildTask {
* @uses DataObject->collectI18nStatics()
*/
public function run($request) {
set_time_limit(0);
increase_time_limit_to();
$c = new i18nTextCollector();
$restrictModules = ($request->getVar('module')) ? explode(',', $request->getVar('module')) : null;
return $c->run($restrictModules);

View File

@ -83,9 +83,7 @@ class DatabaseAdmin extends Controller {
*/
function build() {
// The default time limit of 30 seconds is normally not enough
if(ini_get("safe_mode") != "1") {
set_time_limit(600);
}
increase_time_limit_to(600);
// Get all our classes
ManifestBuilder::create_manifest_file();

View File

@ -7,9 +7,7 @@
class QueuedEmailDispatchTask extends DailyTask {
public function process() {
if(ini_get("safe_mode") != "1") {
set_time_limit(0);
}
increase_time_limit_to();
echo "SENDING QUEUED EMAILS\n";

View File

@ -12,9 +12,7 @@ class GD extends Object {
function __construct($filename = null) {
// If we're working with image resampling, things could take a while. Bump up the time-limit
if(ini_get("safe_mode") != "1") {
set_time_limit(300);
}
increase_time_limit_to(300);
if($filename) {
// We use getimagesize instead of extension checking, because sometimes extensions are wrong.

View File

@ -995,14 +995,12 @@ JS
unset($this->cachedSourceItems);
$oldShowPagination = $this->showPagination;
$this->showPagination = false;
$oldLimit = ini_get('max_execution_time');
set_time_limit(0);
increase_time_limit_to();
$result = $this->renderWith(array($this->template . '_printable', 'TableListField_printable'));
$this->showPagination = $oldShowPagination;
set_time_limit($oldLimit);
return $result;
}

View File

@ -22,17 +22,44 @@ class MemoryLimitTest extends SapphireTest {
increase_memory_limit_to('1G');
$this->assertEquals('1G', ini_get('memory_limit'));
// No argument means unlimited
increase_memory_limit_to();
$this->assertEquals(-1, ini_get('memory_limit'));
}
function testIncreaseTimeLimitTo() {
set_time_limit(6000);
// It can go up
increase_time_limit_to(7000);
$this->assertEquals(7000, ini_get('max_execution_time'));
// But not down
increase_time_limit_to(5000);
$this->assertEquals(7000, ini_get('max_execution_time'));
// 0/nothing means infinity
increase_time_limit_to();
$this->assertEquals(0, ini_get('max_execution_time'));
// Can't go down from there
increase_time_limit_to(10000);
$this->assertEquals(0, ini_get('max_execution_time'));
}
///////////////////
private $origLimit;
private $origMemLimit, $origTimeLimit;
function setUp() {
$this->origLimit = ini_get('memory_limit');
$this->origMemLimit = ini_get('memory_limit');
$this->origTimeLimit = ini_get('max_execution_time');
}
function tearDown() {
ini_set('memory_limit', $this->origLimit);
ini_set('memory_limit', $this->origMemLimit);
set_time_limit($this->origTimeLimit);
}
}