Skip to content

Commit

Permalink
Merge pull request #42391 from nextcloud/bugfix/noid/warn-on-excessiv…
Browse files Browse the repository at this point in the history
…e-memory-consumption-in-background-jobs

feat(cron): Warn on excessive memory consumption in background jobs
  • Loading branch information
nickvergessen authored Jan 10, 2024
2 parents ee710d1 + b8c57ef commit 7981a4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 16 additions & 1 deletion cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,24 @@
break;
}

$logger->debug('CLI cron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
$jobDetails = get_class($this) . ' (id: ' . $this->getId() . ', arguments: ' . json_encode($this->getArgument()) . ')';
$logger->debug('CLI cron call has selected job ' . $jobDetails, ['app' => 'cron']);

$memoryBefore = memory_get_usage();
$memoryPeakBefore = memory_get_peak_usage();

$job->execute($jobList, $logger);

$memoryAfter = memory_get_usage();
$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']);
}
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']);
}

// clean up after unclean jobs
\OC_Util::tearDownFS();
$tempManager->clean();
Expand Down
7 changes: 4 additions & 3 deletions lib/public/BackgroundJob/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ public function start(IJobList $jobList): void {
$logger = $this->logger ?? \OCP\Server::get(LoggerInterface::class);

try {
$jobDetails = get_class($this) . ' (id: ' . $this->getId() . ', arguments: ' . json_encode($this->getArgument()) . ')';
$jobStartTime = $this->time->getTime();
$logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
$logger->debug('Starting job ' . $jobDetails, ['app' => 'cron']);
$this->run($this->argument);
$timeTaken = $this->time->getTime() - $jobStartTime;

$logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
$logger->debug('Finished job ' . $jobDetails . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
$jobList->setExecutionTime($this, $timeTaken);
} catch (\Throwable $e) {
if ($logger) {
$logger->error('Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')', [
$logger->error('Error while running background job ' . $jobDetails, [
'app' => 'core',
'exception' => $e,
]);
Expand Down

0 comments on commit 7981a4b

Please sign in to comment.