Skip to content

Commit c73c59d

Browse files
fix(cron): Log long running jobs
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent cb63256 commit c73c59d

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
@@ -149,7 +149,26 @@
149149
}
150150

151151
$logger->debug('CLI cron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
152+
$timeBefore = time();
152153
$job->execute($jobList, $logger);
154+
$timeAfter = time();
155+
$cronInterval = 5 * 60;
156+
$timeSpent = $timeAfter - $timeBefore;
157+
if ($timeSpent > $cronInterval) {
158+
$logLevel = match (true) {
159+
$timeSpent > $cronInterval * 128 => \OCP\ILogger::FATAL,
160+
$timeSpent > $cronInterval * 64 => \OCP\ILogger::ERROR,
161+
$timeSpent > $cronInterval * 16 => \OCP\ILogger::WARN,
162+
$timeSpent > $cronInterval * 8 => \OCP\ILogger::INFO,
163+
default => \OCP\ILogger::DEBUG,
164+
};
165+
$logger->log(
166+
$logLevel,
167+
'Background job ' . $jobDetails . ' ran for ' . $timeSpent . ' seconds',
168+
['app' => 'cron']
169+
);
170+
}
171+
153172

154173
// clean up after unclean jobs
155174
\OC_Util::tearDownFS();
@@ -159,7 +178,7 @@
159178
$executedJobs[$job->getId()] = true;
160179
unset($job);
161180

162-
if (time() > $endTime) {
181+
if ($timeAfter > $endTime) {
163182
break;
164183
}
165184
}

0 commit comments

Comments
 (0)