From c164409ee7f921a96f95e1fe95d86fe07a98963e Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Sat, 4 Aug 2018 21:53:50 +0200 Subject: [PATCH] Adds a memory limit warning for console commands if the limit is below the recommended value Signed-off-by: Michael Weimann --- console.php | 8 +++++- core/js/setupchecks.js | 2 +- lib/private/Console/Application.php | 30 +++++++++++++++++++- lib/private/MemoryInfo.php | 13 +++++++++ settings/Controller/CheckSetupController.php | 12 +------- 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/console.php b/console.php index 67856a17b3..1d5021edef 100644 --- a/console.php +++ b/console.php @@ -85,7 +85,13 @@ try { echo "The process control (PCNTL) extensions are required in case you want to interrupt long running commands - see http://php.net/manual/en/book.pcntl.php" . PHP_EOL; } - $application = new Application(\OC::$server->getConfig(), \OC::$server->getEventDispatcher(), \OC::$server->getRequest(), \OC::$server->getLogger()); + $application = new Application( + \OC::$server->getConfig(), + \OC::$server->getEventDispatcher(), + \OC::$server->getRequest(), + \OC::$server->getLogger(), + \OC::$server->query(\OC\MemoryInfo::class) + ); $application->loadCommands(new ArgvInput(), new ConsoleOutput()); $application->run(); } catch (Exception $ex) { diff --git a/core/js/setupchecks.js b/core/js/setupchecks.js index f7ee8c73c8..7dabefe9e8 100644 --- a/core/js/setupchecks.js +++ b/core/js/setupchecks.js @@ -316,7 +316,7 @@ type: OC.SetupChecks.MESSAGE_TYPE_WARNING }); } - if (!data.isTheMemoryLimitHighEnough) { + if (!data.isMemoryLimitSufficient) { messages.push({ msg: t( 'core', diff --git a/lib/private/Console/Application.php b/lib/private/Console/Application.php index 1de5fbd6ca..422e78ee06 100644 --- a/lib/private/Console/Application.php +++ b/lib/private/Console/Application.php @@ -29,6 +29,7 @@ */ namespace OC\Console; +use OC\MemoryInfo; use OC\NeedsUpdateException; use OC_App; use OCP\AppFramework\QueryException; @@ -52,20 +53,28 @@ class Application { private $request; /** @var ILogger */ private $logger; + /** @var MemoryInfo */ + private $memoryInfo; /** * @param IConfig $config * @param EventDispatcherInterface $dispatcher * @param IRequest $request * @param ILogger $logger + * @param MemoryInfo $memoryInfo */ - public function __construct(IConfig $config, EventDispatcherInterface $dispatcher, IRequest $request, ILogger $logger) { + public function __construct(IConfig $config, + EventDispatcherInterface $dispatcher, + IRequest $request, + ILogger $logger, + MemoryInfo $memoryInfo) { $defaults = \OC::$server->getThemingDefaults(); $this->config = $config; $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString()); $this->dispatcher = $dispatcher; $this->request = $request; $this->logger = $logger; + $this->memoryInfo = $memoryInfo; } /** @@ -97,6 +106,11 @@ class Application { if ($input->getOption('no-warnings')) { $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); } + + if ($this->memoryInfo->isMemoryLimitSufficient() === false) { + $this->writeMemoryLimitInfo($output); + } + try { require_once __DIR__ . '/../../../core/register_command.php'; if ($this->config->getSystemValue('installed', false)) { @@ -173,6 +187,20 @@ class Application { } } + /** + * Write a memory info output if the limit is below the recommended value. + * + * @param ConsoleOutputInterface $output + * @return void + */ + private function writeMemoryLimitInfo(ConsoleOutputInterface $output) { + $errOutput = $output->getErrorOutput(); + $errOutput->writeln( + 'The current PHP memory limit ' . + 'is below the recommended value of 512MB.' + ); + } + /** * Sets whether to automatically exit after a command execution or not. * diff --git a/lib/private/MemoryInfo.php b/lib/private/MemoryInfo.php index 14865ed682..0501d3fd04 100644 --- a/lib/private/MemoryInfo.php +++ b/lib/private/MemoryInfo.php @@ -6,6 +6,19 @@ namespace OC; * Helper class that covers memory info. */ class MemoryInfo { + + const RECOMMENDED_MEMORY_LIMIT = 512 * 1024 * 1024; + + /** + * Tests if the memory limit is greater or equal the recommended value. + * + * @return bool + */ + public function isMemoryLimitSufficient(): bool { + $memoryLimit = $this->getMemoryLimit(); + return $memoryLimit === -1 || $memoryLimit >= self::RECOMMENDED_MEMORY_LIMIT; + } + /** * Returns the php memory limit. * diff --git a/settings/Controller/CheckSetupController.php b/settings/Controller/CheckSetupController.php index eca6766747..a64d131648 100644 --- a/settings/Controller/CheckSetupController.php +++ b/settings/Controller/CheckSetupController.php @@ -534,16 +534,6 @@ Raw output return function_exists('opcache_get_status'); } - /** - * Tests if the php memory limit is high enough. - * - * @return bool True if more than 512 MB available, else false. - */ - protected function isTheMemoryLimitHighEnough(): bool { - $memoryLimit = $this->memoryInfo->getMemoryLimit(); - return $memoryLimit === -1 || $memoryLimit >= 512 * 1024 * 1024; - } - /** * @return DataResponse */ @@ -581,7 +571,7 @@ Raw output 'databaseConversionDocumentation' => $this->urlGenerator->linkToDocs('admin-db-conversion'), 'isPhpMailerUsed' => $this->isPhpMailerUsed(), 'mailSettingsDocumentation' => $this->urlGenerator->getAbsoluteURL('index.php/settings/admin'), - 'isTheMemoryLimitHighEnough' => $this->isTheMemoryLimitHighEnough(), + 'isMemoryLimitSufficient' => $this->memoryInfo->isMemoryLimitSufficient(), ] ); }