From eb89da9f30e6aeed832bc7e6eb07aa3496cf4a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 8 Feb 2024 13:57:48 +0100 Subject: [PATCH 1/6] chore(cron.php): Migrate away from ILogger and \OC::$server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- cron.php | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/cron.php b/cron.php index e24219bb7900e..428158f629d11 100644 --- a/cron.php +++ b/cron.php @@ -1,10 +1,14 @@ * @author Christopher Schäpers * @author Christoph Wurst + * @author Côme Chilliet * @author Daniel Kesselberg * @author hoellen * @author J0WI @@ -37,24 +41,34 @@ * along with this program. If not, see * */ + require_once __DIR__ . '/lib/versioncheck.php'; +use OC\SystemConfig; +use OCP\BackgroundJob\IJobList; +use OCP\IConfig; +use OCP\ISession; +use OCP\ITempManager; +use OCP\Server; +use OCP\Util; +use Psr\Log\LoggerInterface; + try { require_once __DIR__ . '/lib/base.php'; - if (\OCP\Util::needUpgrade()) { - \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']); + if (Util::needUpgrade()) { + Server::get(LoggerInterface::class)->debug('Update required, skipping cron', ['app' => 'cron']); exit; } - if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) { - \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); + if ((bool) Server::get(SystemConfig::class)->getValue('maintenance', false)) { + Server::get(LoggerInterface::class)->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); exit; } // load all apps to get all api routes properly setup OC_App::loadApps(); - \OC::$server->getSession()->close(); + Server::get(ISession::class)->close(); // initialize a dummy memory session $session = new \OC\Session\Memory(''); @@ -62,12 +76,12 @@ $session = $cryptoWrapper->wrapSession($session); \OC::$server->setSession($session); - $logger = \OC::$server->getLogger(); - $config = \OC::$server->getConfig(); - $tempManager = \OC::$server->getTempManager(); + $logger = Server::get(LoggerInterface::class); + $config = Server::get(IConfig::class); + $tempManager = Server::get(ITempManager::class); // Don't do anything if Nextcloud has not been installed - if (!$config->getSystemValue('installed', false)) { + if (!$config->getSystemValueBool('installed', false)) { exit(0); } @@ -134,7 +148,7 @@ } // Work - $jobList = \OC::$server->getJobList(); + $jobList = Server::get(IJobList::class); // We only ask for jobs for 14 minutes, because after 5 minutes the next // system cron task should spawn and we want to have at most three @@ -160,10 +174,10 @@ $memoryPeakAfter = memory_get_peak_usage(); if ($memoryAfter - $memoryBefore > 10_000_000) { - $logger->warning('Used memory grew by more than 10 MB when executing job ' . $jobDetails . ': ' . \OCP\Util::humanFileSize($memoryAfter). ' (before: ' . \OCP\Util::humanFileSize($memoryBefore) . ')', ['app' => 'cron']); + $logger->warning('Used memory grew by more than 10 MB when executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryAfter). ' (before: ' . Util::humanFileSize($memoryBefore) . ')', ['app' => 'cron']); } if ($memoryPeakAfter > 300_000_000) { - $logger->warning('Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . \OCP\Util::humanFileSize($memoryPeakAfter) . ' (before: ' . \OCP\Util::humanFileSize($memoryPeakBefore) . ')', ['app' => 'cron']); + $logger->warning('Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryPeakAfter) . ' (before: ' . Util::humanFileSize($memoryPeakBefore) . ')', ['app' => 'cron']); } // clean up after unclean jobs @@ -185,7 +199,7 @@ OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]); } else { // Work and success :-) - $jobList = \OC::$server->getJobList(); + $jobList = Server::get(IJobList::class); $job = $jobList->getNext(); if ($job != null) { $logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']); @@ -200,11 +214,17 @@ $config->setAppValue('core', 'lastcron', time()); exit(); } catch (Exception $ex) { - \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); + Server::get(LoggerInterface::class)->error( + $ex->getMessage(), + ['app' => 'cron', 'exception' => $ex] + ); echo $ex . PHP_EOL; exit(1); } catch (Error $ex) { - \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); + Server::get(LoggerInterface::class)->error( + $ex->getMessage(), + ['app' => 'cron', 'exception' => $ex] + ); echo $ex . PHP_EOL; exit(1); } From 05de5e27eb1f04f586e2c66070e8d6a93bc01346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 8 Feb 2024 13:58:49 +0100 Subject: [PATCH 2/6] chore(psalm): Include cron.php into psalm checked files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- psalm.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/psalm.xml b/psalm.xml index 4f7c479a15226..ed840f89059d0 100644 --- a/psalm.xml +++ b/psalm.xml @@ -45,6 +45,7 @@ + From 65d013b9aefc2e8c5ae3d1f4c02368f79201e209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 8 Feb 2024 14:16:44 +0100 Subject: [PATCH 3/6] fix(cron.php): Use new IAppConfig interface in cron.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- cron.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cron.php b/cron.php index 428158f629d11..1624dcdd6d0de 100644 --- a/cron.php +++ b/cron.php @@ -46,6 +46,7 @@ use OC\SystemConfig; use OCP\BackgroundJob\IJobList; +use OCP\IAppConfig; use OCP\IConfig; use OCP\ISession; use OCP\ITempManager; @@ -78,6 +79,7 @@ $logger = Server::get(LoggerInterface::class); $config = Server::get(IConfig::class); + $appConfig = Server::get(IAppConfig::class); $tempManager = Server::get(ITempManager::class); // Don't do anything if Nextcloud has not been installed @@ -88,7 +90,7 @@ $tempManager->cleanOld(); // Exit if background jobs are disabled! - $appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax'); + $appMode = $appConfig->getValueString('core', 'backgroundjobs_mode', 'ajax'); if ($appMode === 'none') { if (OC::$CLI) { echo 'Background Jobs are disabled!' . PHP_EOL; @@ -122,7 +124,7 @@ // We call Nextcloud from the CLI (aka cron) if ($appMode !== 'cron') { - $config->setAppValue('core', 'backgroundjobs_mode', 'cron'); + $appConfig->setValueString('core', 'backgroundjobs_mode', 'cron'); } // Low-load hours @@ -211,7 +213,7 @@ } // Log the successful cron execution - $config->setAppValue('core', 'lastcron', time()); + $appConfig->setValueInt('core', 'lastcron', time()); exit(); } catch (Exception $ex) { Server::get(LoggerInterface::class)->error( From ecfa7c3ca359e732b8bef395edd6be9bd9107b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 8 Feb 2024 14:31:34 +0100 Subject: [PATCH 4/6] chore(cron.php): Migrate uses of legacy classes from lib/private/legacy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- cron.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cron.php b/cron.php index 1624dcdd6d0de..e599f460c13af 100644 --- a/cron.php +++ b/cron.php @@ -45,6 +45,7 @@ require_once __DIR__ . '/lib/versioncheck.php'; use OC\SystemConfig; +use OCP\App\IAppManager; use OCP\BackgroundJob\IJobList; use OCP\IAppConfig; use OCP\IConfig; @@ -66,8 +67,15 @@ exit; } + $config = Server::get(IConfig::class); + + // Don't do anything if Nextcloud has not been installed + if (!$config->getSystemValueBool('installed', false)) { + exit(0); + } + // load all apps to get all api routes properly setup - OC_App::loadApps(); + Server::get(IAppManager::class)->loadApps(); Server::get(ISession::class)->close(); @@ -78,15 +86,9 @@ \OC::$server->setSession($session); $logger = Server::get(LoggerInterface::class); - $config = Server::get(IConfig::class); $appConfig = Server::get(IAppConfig::class); $tempManager = Server::get(ITempManager::class); - // Don't do anything if Nextcloud has not been installed - if (!$config->getSystemValueBool('installed', false)) { - exit(0); - } - $tempManager->cleanOld(); // Exit if background jobs are disabled! @@ -183,7 +185,7 @@ } // clean up after unclean jobs - \OC_Util::tearDownFS(); + Server::get(\OC\Files\SetupManager::class)->tearDown(); $tempManager->clean(); $jobList->setLastJob($job); From cf6a0e083302bec3a387b9c27be42f64a8bf6b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 8 Feb 2024 14:34:01 +0100 Subject: [PATCH 5/6] chore: Deprecate OC_App::loadApps and add missing return type in Server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/Server.php | 1 + lib/private/legacy/OC_App.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/private/Server.php b/lib/private/Server.php index b31fb9cd55c65..0ff8e6fb717ee 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -1661,6 +1661,7 @@ public function getSession() { /** * @param \OCP\ISession $session + * @return void */ public function setSession(\OCP\ISession $session) { $this->get(SessionStorage::class)->setSession($session); diff --git a/lib/private/legacy/OC_App.php b/lib/private/legacy/OC_App.php index 841ad54167421..c652fcfb9bae6 100644 --- a/lib/private/legacy/OC_App.php +++ b/lib/private/legacy/OC_App.php @@ -117,6 +117,8 @@ public static function isAppLoaded(string $app): bool { * exists. * * if $types is set to non-empty array, only apps of those types will be loaded + * + * @deprecated 29.0.0 use IAppManager::loadApps instead */ public static function loadApps(array $types = []): bool { if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { From 1eb89421cce61a9a3d1b2c4c494fe6b3db16e681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 8 Feb 2024 14:38:03 +0100 Subject: [PATCH 6/6] fix(cron.php): Avoid pulling configuration twice MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IConfig is a small wrapper around SystemConfig, no need to pull both of them Signed-off-by: Côme Chilliet --- cron.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cron.php b/cron.php index e599f460c13af..4e95481deb624 100644 --- a/cron.php +++ b/cron.php @@ -44,7 +44,6 @@ require_once __DIR__ . '/lib/versioncheck.php'; -use OC\SystemConfig; use OCP\App\IAppManager; use OCP\BackgroundJob\IJobList; use OCP\IAppConfig; @@ -62,13 +61,14 @@ Server::get(LoggerInterface::class)->debug('Update required, skipping cron', ['app' => 'cron']); exit; } - if ((bool) Server::get(SystemConfig::class)->getValue('maintenance', false)) { + + $config = Server::get(IConfig::class); + + if ($config->getSystemValueBool('maintenance', false)) { Server::get(LoggerInterface::class)->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); exit; } - $config = Server::get(IConfig::class); - // Don't do anything if Nextcloud has not been installed if (!$config->getSystemValueBool('installed', false)) { exit(0);