Browse Source

Merge pull request #43454 from nextcloud/fix/cleanup-cron-php

Cleanup cron.php method calls
tags/v29.0.0beta1
Ferdinand Thiessen 4 months ago
parent
commit
cdf7840d00
No account linked to committer's email address
4 changed files with 52 additions and 24 deletions
  1. 48
    24
      cron.php
  2. 1
    0
      lib/private/Server.php
  3. 2
    0
      lib/private/legacy/OC_App.php
  4. 1
    0
      psalm.xml

+ 48
- 24
cron.php View File

<?php <?php

declare(strict_types=1);

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

require_once __DIR__ . '/lib/versioncheck.php'; 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 { try {
require_once __DIR__ . '/lib/base.php'; 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; 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; 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 // 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 // initialize a dummy memory session
$session = new \OC\Session\Memory(''); $session = new \OC\Session\Memory('');
$session = $cryptoWrapper->wrapSession($session); $session = $cryptoWrapper->wrapSession($session);
\OC::$server->setSession($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(); $tempManager->cleanOld();


// Exit if background jobs are disabled! // Exit if background jobs are disabled!
$appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax');
$appMode = $appConfig->getValueString('core', 'backgroundjobs_mode', 'ajax');
if ($appMode === 'none') { if ($appMode === 'none') {
if (OC::$CLI) { if (OC::$CLI) {
echo 'Background Jobs are disabled!' . PHP_EOL; echo 'Background Jobs are disabled!' . PHP_EOL;


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


// Low-load hours // Low-load hours
} }


// Work // Work
$jobList = \OC::$server->getJobList();
$jobList = Server::get(IJobList::class);


// We only ask for jobs for 14 minutes, because after 5 minutes the next // 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 // system cron task should spawn and we want to have at most three
$memoryPeakAfter = memory_get_peak_usage(); $memoryPeakAfter = memory_get_peak_usage();


if ($memoryAfter - $memoryBefore > 10_000_000) { 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) { 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 // clean up after unclean jobs
\OC_Util::tearDownFS();
Server::get(\OC\Files\SetupManager::class)->tearDown();
$tempManager->clean(); $tempManager->clean();


$jobList->setLastJob($job); $jobList->setLastJob($job);
OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]); OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]);
} else { } else {
// Work and success :-) // Work and success :-)
$jobList = \OC::$server->getJobList();
$jobList = Server::get(IJobList::class);
$job = $jobList->getNext(); $job = $jobList->getNext();
if ($job != null) { if ($job != null) {
$logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']); $logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
} }


// Log the successful cron execution // Log the successful cron execution
$config->setAppValue('core', 'lastcron', time());
$appConfig->setValueInt('core', 'lastcron', time());
exit(); exit();
} catch (Exception $ex) { } 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; echo $ex . PHP_EOL;
exit(1); exit(1);
} catch (Error $ex) { } 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; echo $ex . PHP_EOL;
exit(1); exit(1);
} }

+ 1
- 0
lib/private/Server.php View File



/** /**
* @param \OCP\ISession $session * @param \OCP\ISession $session
* @return void
*/ */
public function setSession(\OCP\ISession $session) { public function setSession(\OCP\ISession $session) {
$this->get(SessionStorage::class)->setSession($session); $this->get(SessionStorage::class)->setSession($session);

+ 2
- 0
lib/private/legacy/OC_App.php View File

* exists. * exists.
* *
* if $types is set to non-empty array, only apps of those types will be loaded * 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 { public static function loadApps(array $types = []): bool {
if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {

+ 1
- 0
psalm.xml View File

<directory name="lib"/> <directory name="lib"/>
<directory name="ocs"/> <directory name="ocs"/>
<directory name="ocs-provider"/> <directory name="ocs-provider"/>
<file name="cron.php"/>
<file name="index.php"/> <file name="index.php"/>
<file name="public.php"/> <file name="public.php"/>
<file name="remote.php"/> <file name="remote.php"/>

Loading…
Cancel
Save