Skip to content

Commit ba46d03

Browse files
Merge pull request #45813 from nextcloud/backport/45804/stable29
[stable29] fix(cron): Log long running jobs
2 parents a51237a + ed95c03 commit ba46d03

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

cron.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,34 @@
169169
$jobDetails = get_class($job) . ' (id: ' . $job->getId() . ', arguments: ' . json_encode($job->getArgument()) . ')';
170170
$logger->debug('CLI cron call has selected job ' . $jobDetails, ['app' => 'cron']);
171171

172+
$timeBefore = time();
172173
$memoryBefore = memory_get_usage();
173174
$memoryPeakBefore = memory_get_peak_usage();
174175

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

179+
$timeAfter = time();
178180
$memoryAfter = memory_get_usage();
179181
$memoryPeakAfter = memory_get_peak_usage();
180182

183+
$cronInterval = 5 * 60;
184+
$timeSpent = $timeAfter - $timeBefore;
185+
if ($timeSpent > $cronInterval) {
186+
$logLevel = match (true) {
187+
$timeSpent > $cronInterval * 128 => \OCP\ILogger::FATAL,
188+
$timeSpent > $cronInterval * 64 => \OCP\ILogger::ERROR,
189+
$timeSpent > $cronInterval * 16 => \OCP\ILogger::WARN,
190+
$timeSpent > $cronInterval * 8 => \OCP\ILogger::INFO,
191+
default => \OCP\ILogger::DEBUG,
192+
};
193+
$logger->log(
194+
$logLevel,
195+
'Background job ' . $jobDetails . ' ran for ' . $timeSpent . ' seconds',
196+
['app' => 'cron']
197+
);
198+
}
199+
181200
if ($memoryAfter - $memoryBefore > 10_000_000) {
182201
$logger->warning('Used memory grew by more than 10 MB when executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryAfter). ' (before: ' . Util::humanFileSize($memoryBefore) . ')', ['app' => 'cron']);
183202
}
@@ -193,7 +212,7 @@
193212
$executedJobs[$job->getId()] = true;
194213
unset($job);
195214

196-
if (time() > $endTime) {
215+
if ($timeAfter > $endTime) {
197216
break;
198217
}
199218
}

0 commit comments

Comments
 (0)