From 5b9b9cbfa7d3f7fbc0dba61f03a624c089d489c5 Mon Sep 17 00:00:00 2001 From: Thorsten Reiter Date: Tue, 10 Dec 2024 17:09:08 +0100 Subject: [PATCH 1/5] IBX-9316: Fixed cpu count for cloud --- src/bundle/Core/Command/ReindexCommand.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bundle/Core/Command/ReindexCommand.php b/src/bundle/Core/Command/ReindexCommand.php index bd8d6cc246..57822a8924 100644 --- a/src/bundle/Core/Command/ReindexCommand.php +++ b/src/bundle/Core/Command/ReindexCommand.php @@ -450,7 +450,13 @@ private function getPhpPath() private function getNumberOfCPUCores() { $cores = 1; - if (is_file('/proc/cpuinfo')) { + if (isset($_SERVER['PLATFORM_BRANCH']) && is_file( '/run/config.json')) { + // Ibexa Cloud: read #cpus from config + $config_json_encoded = file_get_contents('/run/config.json'); + $config_json = json_decode($config_json_encoded); + $cores = isset($config_json->info->limits->cpu) ? max(1, floor($config_json->info->limits->cpu)) : 1; + return $cores; + } elseif (is_file('/proc/cpuinfo')) { // Linux (and potentially Windows with linux sub systems) $cpuinfo = file_get_contents('/proc/cpuinfo'); preg_match_all('/^processor/m', $cpuinfo, $matches); From f3b1baaf424cd3233b63e89ff0a173e604f4b308 Mon Sep 17 00:00:00 2001 From: Thorsten Reiter Date: Tue, 10 Dec 2024 17:24:13 +0100 Subject: [PATCH 2/5] fix-cs --- src/bundle/Core/Command/ReindexCommand.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/bundle/Core/Command/ReindexCommand.php b/src/bundle/Core/Command/ReindexCommand.php index 57822a8924..971f319f29 100644 --- a/src/bundle/Core/Command/ReindexCommand.php +++ b/src/bundle/Core/Command/ReindexCommand.php @@ -450,12 +450,16 @@ private function getPhpPath() private function getNumberOfCPUCores() { $cores = 1; - if (isset($_SERVER['PLATFORM_BRANCH']) && is_file( '/run/config.json')) { + if (isset($_SERVER['PLATFORM_BRANCH']) && is_file('/run/config.json')) { // Ibexa Cloud: read #cpus from config $config_json_encoded = file_get_contents('/run/config.json'); + if ($config_json_encoded == false) { + return 1; + } $config_json = json_decode($config_json_encoded); $cores = isset($config_json->info->limits->cpu) ? max(1, floor($config_json->info->limits->cpu)) : 1; - return $cores; + + return (int)$cores; } elseif (is_file('/proc/cpuinfo')) { // Linux (and potentially Windows with linux sub systems) $cpuinfo = file_get_contents('/proc/cpuinfo'); From c6ec8064884ac64a3ed373222cb90ad9554dda72 Mon Sep 17 00:00:00 2001 From: Thorsten Reiter Date: Wed, 11 Dec 2024 13:43:58 +0100 Subject: [PATCH 3/5] suggestions from code review --- src/bundle/Core/Command/ReindexCommand.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/bundle/Core/Command/ReindexCommand.php b/src/bundle/Core/Command/ReindexCommand.php index 971f319f29..cc80d668bf 100644 --- a/src/bundle/Core/Command/ReindexCommand.php +++ b/src/bundle/Core/Command/ReindexCommand.php @@ -450,17 +450,15 @@ private function getPhpPath() private function getNumberOfCPUCores() { $cores = 1; - if (isset($_SERVER['PLATFORM_BRANCH']) && is_file('/run/config.json')) { + if (isset($_SERVER['PLATFORM_BRANCH']) && is_readable('/run/config.json')) { // Ibexa Cloud: read #cpus from config - $config_json_encoded = file_get_contents('/run/config.json'); - if ($config_json_encoded == false) { + $configJsonEncoded = file_get_contents('/run/config.json'); + if ($configJsonEncoded === false) { return 1; } - $config_json = json_decode($config_json_encoded); - $cores = isset($config_json->info->limits->cpu) ? max(1, floor($config_json->info->limits->cpu)) : 1; - - return (int)$cores; - } elseif (is_file('/proc/cpuinfo')) { + $configJson = json_decode($configJsonEncoded); + $cores = isset($configJson->info->limits->cpu) ? max(1, (int) ($configJson->info->limits->cpu)) : 1; + } elseif (is_readable('/proc/cpuinfo')) { // Linux (and potentially Windows with linux sub systems) $cpuinfo = file_get_contents('/proc/cpuinfo'); preg_match_all('/^processor/m', $cpuinfo, $matches); From 009411e8b13f35e3015577616415fafbab87ab8f Mon Sep 17 00:00:00 2001 From: Thorsten Reiter Date: Wed, 11 Dec 2024 14:15:17 +0100 Subject: [PATCH 4/5] add is_file check --- src/bundle/Core/Command/ReindexCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bundle/Core/Command/ReindexCommand.php b/src/bundle/Core/Command/ReindexCommand.php index cc80d668bf..936425d21f 100644 --- a/src/bundle/Core/Command/ReindexCommand.php +++ b/src/bundle/Core/Command/ReindexCommand.php @@ -450,7 +450,7 @@ private function getPhpPath() private function getNumberOfCPUCores() { $cores = 1; - if (isset($_SERVER['PLATFORM_BRANCH']) && is_readable('/run/config.json')) { + if (isset($_SERVER['PLATFORM_BRANCH']) && is_readable('/run/config.json') && is_file('/run/config.json')) { // Ibexa Cloud: read #cpus from config $configJsonEncoded = file_get_contents('/run/config.json'); if ($configJsonEncoded === false) { @@ -458,7 +458,7 @@ private function getNumberOfCPUCores() } $configJson = json_decode($configJsonEncoded); $cores = isset($configJson->info->limits->cpu) ? max(1, (int) ($configJson->info->limits->cpu)) : 1; - } elseif (is_readable('/proc/cpuinfo')) { + } elseif (is_readable('/proc/cpuinfo') && is_file('/proc/cpuinfo')) { // Linux (and potentially Windows with linux sub systems) $cpuinfo = file_get_contents('/proc/cpuinfo'); preg_match_all('/^processor/m', $cpuinfo, $matches); From d39232793a2a692cd575ba984cddfb39f6b61abd Mon Sep 17 00:00:00 2001 From: Thorsten Reiter Date: Wed, 11 Dec 2024 14:44:45 +0100 Subject: [PATCH 5/5] apply SonarCloud suggestions --- src/bundle/Core/Command/ReindexCommand.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/bundle/Core/Command/ReindexCommand.php b/src/bundle/Core/Command/ReindexCommand.php index 936425d21f..8af26d403b 100644 --- a/src/bundle/Core/Command/ReindexCommand.php +++ b/src/bundle/Core/Command/ReindexCommand.php @@ -28,6 +28,9 @@ class ReindexCommand extends Command implements BackwardCompatibleCommand { + private const IBEXA_CLOUD_CONFIG_FILE = '/run/config.json'; + private const LINUX_CPUINFO_FILE = '/proc/cpuinfo'; + /** @var \Ibexa\Core\Search\Common\Indexer|\Ibexa\Core\Search\Common\IncrementalIndexer */ private $searchIndexer; @@ -450,17 +453,17 @@ private function getPhpPath() private function getNumberOfCPUCores() { $cores = 1; - if (isset($_SERVER['PLATFORM_BRANCH']) && is_readable('/run/config.json') && is_file('/run/config.json')) { + if (isset($_SERVER['PLATFORM_BRANCH']) && is_readable(self::IBEXA_CLOUD_CONFIG_FILE) && is_file(self::IBEXA_CLOUD_CONFIG_FILE)) { // Ibexa Cloud: read #cpus from config - $configJsonEncoded = file_get_contents('/run/config.json'); + $configJsonEncoded = file_get_contents(self::IBEXA_CLOUD_CONFIG_FILE); if ($configJsonEncoded === false) { return 1; } $configJson = json_decode($configJsonEncoded); $cores = isset($configJson->info->limits->cpu) ? max(1, (int) ($configJson->info->limits->cpu)) : 1; - } elseif (is_readable('/proc/cpuinfo') && is_file('/proc/cpuinfo')) { + } elseif (is_readable(self::LINUX_CPUINFO_FILE) && is_file(self::LINUX_CPUINFO_FILE)) { // Linux (and potentially Windows with linux sub systems) - $cpuinfo = file_get_contents('/proc/cpuinfo'); + $cpuinfo = file_get_contents(self::LINUX_CPUINFO_FILE); preg_match_all('/^processor/m', $cpuinfo, $matches); $cores = count($matches[0]); } elseif (DIRECTORY_SEPARATOR === '\\') {