Browse Source

Adds a memory limit warning for console commands if the limit is below the recommended value

Signed-off-by: Michael Weimann <mail@michael-weimann.eu>
Michael Weimann 5 years ago
parent
commit
c164409ee7

+ 7 - 1
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) {

+ 1 - 1
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',

+ 29 - 1
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(
+			'<comment>The current PHP memory limit ' .
+			'is below the recommended value of 512MB.</comment>'
+		);
+	}
+
 	/**
 	 * Sets whether to automatically exit after a command execution or not.
 	 *

+ 13 - 0
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.
 	 *

+ 1 - 11
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(),
 			]
 		);
 	}