Introduce Icinga\Util\Environment to manage limits

And replace the code in Icinga\File\Pdf with the new helper.
This commit is contained in:
Markus Frosch 2018-12-05 15:31:15 +01:00
parent 87da1e76cd
commit 4081a85d90
3 changed files with 88 additions and 10 deletions

View File

@ -8,7 +8,7 @@ use Dompdf\Dompdf;
use Dompdf\Options;
use Icinga\Application\Icinga;
use Icinga\Exception\ProgrammingError;
use Icinga\Util\Format;
use Icinga\Util\Environment;
use Icinga\Web\Hook;
use Icinga\Web\Url;
@ -47,15 +47,8 @@ class Pdf
{
$this->assertNoHeadersSent();
$memLimit = 512 * 1024 ** 2;
if (Format::unpackShorthandBytes(ini_get('memory_limit')) < $memLimit) {
ini_set('memory_limit', $memLimit);
}
$maxExecTime = 300;
if ((int) ini_get('max_execution_time') < $maxExecTime) {
ini_set('max_execution_time', $maxExecTime);
}
Environment::raiseMemoryLimit('512M');
Environment::raiseExecutionTime(300);
$viewRenderer = $controller->getHelper('viewRenderer');
$controller->render(

View File

@ -0,0 +1,42 @@
<?php
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */
namespace Icinga\Util;
/**
* Helper for configuring the PHP environment
*/
class Environment
{
/**
* Raise the PHP memory_limit
*
* Unless it is not already set to a higher limit
*
* @param string|int $minimum
*/
public static function raiseMemoryLimit($minimum = '512M')
{
if (is_string($minimum)) {
$minimum = Format::unpackShorthandBytes($minimum);
}
if (Format::unpackShorthandBytes(ini_get('memory_limit')) < $minimum) {
ini_set('memory_limit', $minimum);
}
}
/**
* Raise the PHP max_execution_time
*
* Unless it is not already configured to a higher value.
*
* @param int $minimum
*/
public static function raiseExecutionTime($minimum = 300)
{
if ((int) ini_get('max_execution_time') < $minimum) {
ini_set('max_execution_time', $minimum);
}
}
}

View File

@ -0,0 +1,43 @@
<?php
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */
namespace Tests\Icinga\Util;
use Icinga\Util\Environment;
use Icinga\Test\BaseTestCase;
class EnvironmentTest extends BaseTestCase
{
public function testRaiseMemoryLimit()
{
// set a low limit
ini_set('memory_limit', '128M');
Environment::raiseMemoryLimit('512M');
$this->assertEquals('536870912' /* 512M */, ini_get('memory_limit'));
Environment::raiseMemoryLimit('1G');
$this->assertEquals('1073741824' /* 1G */, ini_get('memory_limit'));
Environment::raiseMemoryLimit('512M');
$this->assertEquals('1073741824' /* 1G */, ini_get('memory_limit'));
// in phpunit usually there is no limit
ini_set('memory_limit', '-1');
}
public function testRaiseExecutionTime()
{
Environment::raiseExecutionTime(300);
$this->assertEquals(300, ini_get('max_execution_time'));
Environment::raiseExecutionTime(600);
$this->assertEquals(600, ini_get('max_execution_time'));
Environment::raiseExecutionTime(300);
$this->assertEquals(600, ini_get('max_execution_time'));
// in phpunit usually there is no limit
ini_set('max_execution_time', '0');
}
}