Skip to content

Commit f1e2458

Browse files
committed
feat(jobs): allow workers to keep track of executed jobs
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 66ed05a commit f1e2458

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

core/Command/Background/JobWorker.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
67
* SPDX-License-Identifier: AGPL-3.0-or-later
78
*/
89

910
namespace OC\Core\Command\Background;
1011

12+
use OC\BackgroundJob\JobClassesRegistry;
1113
use OC\Core\Command\InterruptedException;
1214
use OCP\BackgroundJob\IJobList;
15+
use OCP\BackgroundJob\IJobRuns;
1316
use OCP\Files\ISetupManager;
1417
use OCP\ITempManager;
1518
use Psr\Log\LoggerInterface;
@@ -19,15 +22,17 @@
1922
use Symfony\Component\Console\Output\OutputInterface;
2023

2124
class JobWorker extends JobBase {
22-
2325
public function __construct(
2426
protected IJobList $jobList,
2527
protected LoggerInterface $logger,
2628
private ITempManager $tempManager,
2729
private ISetupManager $setupManager,
30+
private readonly IJobRuns $jobRuns,
31+
private readonly JobClassesRegistry $jobClassesRegistry,
2832
) {
2933
parent::__construct($jobList, $logger);
3034
}
35+
3136
#[\Override]
3237
protected function configure(): void {
3338
parent::configure();
@@ -124,13 +129,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
124129
continue;
125130
}
126131

132+
$jobClassName = get_class($job);
127133
$output->writeln('Running job ' . get_class($job) . ' with ID ' . $job->getId());
128134

129135
if ($output->isVerbose()) {
130136
$this->printJobInfo($job->getId(), $job, $output);
131137
}
132138

139+
memory_reset_peak_usage();
140+
$jobClassId = $this->jobClassesRegistry->getId($jobClassName);
141+
$jobRunId = $this->jobRuns->started($jobClassId);
142+
$startTime = microtime(true);
133143
$job->start($this->jobList);
144+
$timeSpent = microtime(true) - $startTime;
145+
$jobMemoryPeak = memory_get_peak_usage();
146+
// TODO Job failure will never be catched here because exceptions are catched within $job->start method
147+
// The error will only be visible in server logs.
148+
// It should be a temporary state until a proper job runner is implemented.
149+
$this->jobRuns->finished($jobRunId, (int)($timeSpent * 1000), (int)($jobMemoryPeak / 1024));
150+
134151
$output->writeln('Job ' . $job->getId() . ' has finished', OutputInterface::VERBOSITY_VERBOSE);
135152

136153
// clean up after unclean jobs

0 commit comments

Comments
 (0)