Skip to content

Commit 66ed05a

Browse files
committed
feat(jobs): log job execution in CronService
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 524b2c1 commit 66ed05a

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

core/Service/CronService.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use OC;
1515
use OC\Authentication\LoginCredentials\Store;
16+
use OC\BackgroundJob\JobClassesRegistry;
1617
use OC\DB\Connection;
1718
use OC\Security\CSRF\TokenStorage\SessionStorage;
1819
use OC\Session\CryptoWrapper;
@@ -21,6 +22,7 @@
2122
use OCP\App\IAppManager;
2223
use OCP\BackgroundJob\IJob;
2324
use OCP\BackgroundJob\IJobList;
25+
use OCP\BackgroundJob\IJobRuns;
2426
use OCP\Files\ISetupManager;
2527
use OCP\IAppConfig;
2628
use OCP\IConfig;
@@ -49,6 +51,8 @@ public function __construct(
4951
private readonly ITempManager $tempManager,
5052
private readonly IAppConfig $appConfig,
5153
private readonly IJobList $jobList,
54+
private readonly IJobRuns $jobRuns,
55+
private readonly JobClassesRegistry $jobClassesRegistry,
5256
private readonly ISetupManager $setupManager,
5357
private readonly bool $isCLI,
5458
) {
@@ -185,20 +189,27 @@ private function runCli(string $appMode, ?array $jobClasses): void {
185189
break;
186190
}
187191

188-
$jobDetails = get_class($job) . ' (id: ' . $job->getId() . ', arguments: ' . json_encode($job->getArgument()) . ')';
192+
$jobClass = get_class($job);
193+
$jobDetails = $jobClass . ' (id: ' . $job->getId() . ', arguments: ' . json_encode($job->getArgument()) . ')';
189194
$this->logger->debug('CLI cron call has selected job ' . $jobDetails, ['app' => 'cron']);
190195

191196
$this->verboseOutput('Starting job ' . $jobDetails);
192197

193-
$startTime = microtime(true);
194198
$referenceMemory = memory_get_usage();
195199
memory_reset_peak_usage();
196200

201+
$jobClassId = $this->jobClassesRegistry->getId($jobClass);
202+
$jobRunId = $this->jobRuns->started($jobClassId);
203+
$startTime = microtime(true);
197204
$job->start($this->jobList);
198205

199206
$memoryIncrease = memory_get_usage() - $referenceMemory;
200207
$timeSpent = microtime(true) - $startTime;
201-
$jobMemoryPeak = memory_get_peak_usage() - $referenceMemory;
208+
$jobMemoryPeak = memory_get_peak_usage();
209+
// TODO Job failure will never be catched here because exceptions are catched within $job->start method
210+
// The error will only be visible in server logs.
211+
// It should be a temporary state until a proper job runner is implemented.
212+
$this->jobRuns->finished($jobRunId, (int)($timeSpent * 1000), (int)($jobMemoryPeak / 1024));
202213

203214
$cronInterval = 5 * 60;
204215
if ($timeSpent > $cronInterval) {

lib/private/Server.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use OC\Authentication\TwoFactorAuth\Registry;
2626
use OC\Avatar\AvatarManager;
2727
use OC\BackgroundJob\JobList;
28+
use OC\BackgroundJob\JobRuns;
2829
use OC\Blurhash\Listener\GenerateBlurhashMetadata;
2930
use OC\Collaboration\Collaborators\GroupPlugin;
3031
use OC\Collaboration\Collaborators\MailByMailPlugin;
@@ -170,6 +171,7 @@
170171
use OCP\Authentication\TwoFactorAuth\IRegistry;
171172
use OCP\AutoloadNotAllowedException;
172173
use OCP\BackgroundJob\IJobList;
174+
use OCP\BackgroundJob\IJobRuns;
173175
use OCP\Collaboration\Collaborators\ISearch;
174176
use OCP\Collaboration\Collaborators\ISearchResult;
175177
use OCP\Collaboration\Reference\IReferenceManager;
@@ -1319,6 +1321,7 @@ public function __construct(
13191321
return $c->get(FileSequence::class);
13201322
}, false);
13211323
$this->registerAlias(ISnowflakeDecoder::class, SnowflakeDecoder::class);
1324+
$this->registerAlias(IJobRuns::class, JobRuns::class);
13221325

13231326
$this->connectDispatcher();
13241327
}

0 commit comments

Comments
 (0)