Skip to content

Commit cdf7840

Browse files
authored
Merge pull request #43454 from nextcloud/fix/cleanup-cron-php
Cleanup cron.php method calls
2 parents 898df41 + 1eb8942 commit cdf7840

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

cron.php

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* @copyright Copyright (c) 2016, ownCloud, Inc.
47
*
58
* @author Artem Sidorenko <artem@posteo.de>
69
* @author Christopher Schäpers <kondou@ts.unde.re>
710
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
11+
* @author Côme Chilliet <come.chilliet@nextcloud.com>
812
* @author Daniel Kesselberg <mail@danielkesselberg.de>
913
* @author hoellen <dev@hoellen.eu>
1014
* @author J0WI <J0WI@users.noreply.github.com>
@@ -37,44 +41,58 @@
3741
* along with this program. If not, see <http://www.gnu.org/licenses/>
3842
*
3943
*/
44+
4045
require_once __DIR__ . '/lib/versioncheck.php';
4146

47+
use OCP\App\IAppManager;
48+
use OCP\BackgroundJob\IJobList;
49+
use OCP\IAppConfig;
50+
use OCP\IConfig;
51+
use OCP\ISession;
52+
use OCP\ITempManager;
53+
use OCP\Server;
54+
use OCP\Util;
55+
use Psr\Log\LoggerInterface;
56+
4257
try {
4358
require_once __DIR__ . '/lib/base.php';
4459

45-
if (\OCP\Util::needUpgrade()) {
46-
\OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']);
60+
if (Util::needUpgrade()) {
61+
Server::get(LoggerInterface::class)->debug('Update required, skipping cron', ['app' => 'cron']);
4762
exit;
4863
}
49-
if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
50-
\OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
64+
65+
$config = Server::get(IConfig::class);
66+
67+
if ($config->getSystemValueBool('maintenance', false)) {
68+
Server::get(LoggerInterface::class)->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
5169
exit;
5270
}
5371

72+
// Don't do anything if Nextcloud has not been installed
73+
if (!$config->getSystemValueBool('installed', false)) {
74+
exit(0);
75+
}
76+
5477
// load all apps to get all api routes properly setup
55-
OC_App::loadApps();
78+
Server::get(IAppManager::class)->loadApps();
5679

57-
\OC::$server->getSession()->close();
80+
Server::get(ISession::class)->close();
5881

5982
// initialize a dummy memory session
6083
$session = new \OC\Session\Memory('');
6184
$cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
6285
$session = $cryptoWrapper->wrapSession($session);
6386
\OC::$server->setSession($session);
6487

65-
$logger = \OC::$server->getLogger();
66-
$config = \OC::$server->getConfig();
67-
$tempManager = \OC::$server->getTempManager();
68-
69-
// Don't do anything if Nextcloud has not been installed
70-
if (!$config->getSystemValue('installed', false)) {
71-
exit(0);
72-
}
88+
$logger = Server::get(LoggerInterface::class);
89+
$appConfig = Server::get(IAppConfig::class);
90+
$tempManager = Server::get(ITempManager::class);
7391

7492
$tempManager->cleanOld();
7593

7694
// Exit if background jobs are disabled!
77-
$appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax');
95+
$appMode = $appConfig->getValueString('core', 'backgroundjobs_mode', 'ajax');
7896
if ($appMode === 'none') {
7997
if (OC::$CLI) {
8098
echo 'Background Jobs are disabled!' . PHP_EOL;
@@ -108,7 +126,7 @@
108126

109127
// We call Nextcloud from the CLI (aka cron)
110128
if ($appMode !== 'cron') {
111-
$config->setAppValue('core', 'backgroundjobs_mode', 'cron');
129+
$appConfig->setValueString('core', 'backgroundjobs_mode', 'cron');
112130
}
113131

114132
// Low-load hours
@@ -134,7 +152,7 @@
134152
}
135153

136154
// Work
137-
$jobList = \OC::$server->getJobList();
155+
$jobList = Server::get(IJobList::class);
138156

139157
// We only ask for jobs for 14 minutes, because after 5 minutes the next
140158
// system cron task should spawn and we want to have at most three
@@ -160,14 +178,14 @@
160178
$memoryPeakAfter = memory_get_peak_usage();
161179

162180
if ($memoryAfter - $memoryBefore > 10_000_000) {
163-
$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']);
181+
$logger->warning('Used memory grew by more than 10 MB when executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryAfter). ' (before: ' . Util::humanFileSize($memoryBefore) . ')', ['app' => 'cron']);
164182
}
165183
if ($memoryPeakAfter > 300_000_000) {
166-
$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']);
184+
$logger->warning('Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryPeakAfter) . ' (before: ' . Util::humanFileSize($memoryPeakBefore) . ')', ['app' => 'cron']);
167185
}
168186

169187
// clean up after unclean jobs
170-
\OC_Util::tearDownFS();
188+
Server::get(\OC\Files\SetupManager::class)->tearDown();
171189
$tempManager->clean();
172190

173191
$jobList->setLastJob($job);
@@ -185,7 +203,7 @@
185203
OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]);
186204
} else {
187205
// Work and success :-)
188-
$jobList = \OC::$server->getJobList();
206+
$jobList = Server::get(IJobList::class);
189207
$job = $jobList->getNext();
190208
if ($job != null) {
191209
$logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
@@ -197,14 +215,20 @@
197215
}
198216

199217
// Log the successful cron execution
200-
$config->setAppValue('core', 'lastcron', time());
218+
$appConfig->setValueInt('core', 'lastcron', time());
201219
exit();
202220
} catch (Exception $ex) {
203-
\OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
221+
Server::get(LoggerInterface::class)->error(
222+
$ex->getMessage(),
223+
['app' => 'cron', 'exception' => $ex]
224+
);
204225
echo $ex . PHP_EOL;
205226
exit(1);
206227
} catch (Error $ex) {
207-
\OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
228+
Server::get(LoggerInterface::class)->error(
229+
$ex->getMessage(),
230+
['app' => 'cron', 'exception' => $ex]
231+
);
208232
echo $ex . PHP_EOL;
209233
exit(1);
210234
}

lib/private/Server.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,7 @@ public function getSession() {
16611661

16621662
/**
16631663
* @param \OCP\ISession $session
1664+
* @return void
16641665
*/
16651666
public function setSession(\OCP\ISession $session) {
16661667
$this->get(SessionStorage::class)->setSession($session);

lib/private/legacy/OC_App.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ public static function isAppLoaded(string $app): bool {
117117
* exists.
118118
*
119119
* if $types is set to non-empty array, only apps of those types will be loaded
120+
*
121+
* @deprecated 29.0.0 use IAppManager::loadApps instead
120122
*/
121123
public static function loadApps(array $types = []): bool {
122124
if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<directory name="lib"/>
4646
<directory name="ocs"/>
4747
<directory name="ocs-provider"/>
48+
<file name="cron.php"/>
4849
<file name="index.php"/>
4950
<file name="public.php"/>
5051
<file name="remote.php"/>

0 commit comments

Comments
 (0)