Skip to content

Commit

Permalink
Merge pull request #43454 from nextcloud/fix/cleanup-cron-php
Browse files Browse the repository at this point in the history
Cleanup cron.php method calls
  • Loading branch information
susnux authored Feb 8, 2024
2 parents 898df41 + 1eb8942 commit cdf7840
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
72 changes: 48 additions & 24 deletions cron.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Artem Sidorenko <artem@posteo.de>
* @author Christopher Schäpers <kondou@ts.unde.re>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
* @author Daniel Kesselberg <mail@danielkesselberg.de>
* @author hoellen <dev@hoellen.eu>
* @author J0WI <J0WI@users.noreply.github.com>
Expand Down Expand Up @@ -37,44 +41,58 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

require_once __DIR__ . '/lib/versioncheck.php';

use OCP\App\IAppManager;
use OCP\BackgroundJob\IJobList;
use OCP\IAppConfig;
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']);

$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;
}

// 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();

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

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

$logger = \OC::$server->getLogger();
$config = \OC::$server->getConfig();
$tempManager = \OC::$server->getTempManager();

// Don't do anything if Nextcloud has not been installed
if (!$config->getSystemValue('installed', false)) {
exit(0);
}
$logger = Server::get(LoggerInterface::class);
$appConfig = Server::get(IAppConfig::class);
$tempManager = Server::get(ITempManager::class);

$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;
Expand Down Expand Up @@ -108,7 +126,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
Expand All @@ -134,7 +152,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
Expand All @@ -160,14 +178,14 @@
$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
\OC_Util::tearDownFS();
Server::get(\OC\Files\SetupManager::class)->tearDown();
$tempManager->clean();

$jobList->setLastJob($job);
Expand All @@ -185,7 +203,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']);
Expand All @@ -197,14 +215,20 @@
}

// Log the successful cron execution
$config->setAppValue('core', 'lastcron', time());
$appConfig->setValueInt('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);
}
1 change: 1 addition & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions lib/private/legacy/OC_App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<directory name="lib"/>
<directory name="ocs"/>
<directory name="ocs-provider"/>
<file name="cron.php"/>
<file name="index.php"/>
<file name="public.php"/>
<file name="remote.php"/>
Expand Down

0 comments on commit cdf7840

Please sign in to comment.