Skip to content

Commit

Permalink
feat: add verbose mode to cron.php
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Sep 15, 2024
1 parent 6d6baec commit 6fcd5c0
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
Run the background job routine
Usage:
php -f cron.php -- [-h] [<job-classes>...]
php -f cron.php -- [-h] [--verbose] [<job-classes>...]
Arguments:
job-classes Optional job class list to only run those jobs
Options:
-h, --help Display this help message' . PHP_EOL;
-h, --help Display this help message
-v, --verbose Output more information' . PHP_EOL;
exit(0);
}

Expand All @@ -57,9 +58,14 @@

// load all apps to get all api routes properly setup
Server::get(IAppManager::class)->loadApps();

Server::get(ISession::class)->close();

$verbose = false;
if (isset($argv[1]) && ($argv[1] === '-v' || $argv[1] === '--verbose')) {
$verbose = true;
$argv = array_slice($argv, 1);
}

// initialize a dummy memory session
$session = new \OC\Session\Memory();
$cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
Expand Down Expand Up @@ -160,6 +166,10 @@

/** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
$job->execute($jobList);

if ($verbose) {
echo 'Starting job ' . $jobDetails . PHP_EOL;
}

$timeAfter = time();
$memoryAfter = memory_get_usage();
Expand All @@ -183,16 +193,28 @@
}

if ($memoryAfter - $memoryBefore > 50_000_000) {
$logger->warning('Used memory grew by more than 50 MB when executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryAfter). ' (before: ' . Util::humanFileSize($memoryBefore) . ')', ['app' => 'cron']);
$message = 'Used memory grew by more than 50 MB when executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryAfter). ' (before: ' . Util::humanFileSize($memoryBefore) . ')';
$logger->warning($message, ['app' => 'cron']);
if ($verbose) {
echo $message . PHP_EOL;
}
}
if ($memoryPeakAfter > 300_000_000 && $memoryPeakBefore <= 300_000_000) {
$logger->warning('Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryPeakAfter) . ' (before: ' . Util::humanFileSize($memoryPeakBefore) . ')', ['app' => 'cron']);
$message = 'Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryPeakAfter) . ' (before: ' . Util::humanFileSize($memoryPeakBefore) . ')';
$logger->warning($message, ['app' => 'cron']);
if ($verbose) {
echo $message . PHP_EOL;
}
}

// clean up after unclean jobs
Server::get(\OC\Files\SetupManager::class)->tearDown();
$tempManager->clean();

if ($verbose) {
echo 'Job ' . $jobDetails . ' done in ' . ($timeAfter - $timeBefore) . ' seconds' . PHP_EOL;
}

$jobList->setLastJob($job);
$executedJobs[$job->getId()] = true;
unset($job);
Expand Down

0 comments on commit 6fcd5c0

Please sign in to comment.