|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
| 4 | + |
4 | 5 | /** |
5 | 6 | * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors |
6 | 7 | * SPDX-License-Identifier: AGPL-3.0-or-later |
7 | 8 | */ |
8 | 9 |
|
9 | 10 | namespace OC\Core\Command\Background; |
10 | 11 |
|
| 12 | +use OC\BackgroundJob\JobClassesRegistry; |
11 | 13 | use OC\Core\Command\InterruptedException; |
12 | 14 | use OCP\BackgroundJob\IJobList; |
| 15 | +use OCP\BackgroundJob\IJobRuns; |
13 | 16 | use OCP\Files\ISetupManager; |
14 | 17 | use OCP\ITempManager; |
15 | 18 | use Psr\Log\LoggerInterface; |
|
19 | 22 | use Symfony\Component\Console\Output\OutputInterface; |
20 | 23 |
|
21 | 24 | class JobWorker extends JobBase { |
22 | | - |
23 | 25 | public function __construct( |
24 | 26 | protected IJobList $jobList, |
25 | 27 | protected LoggerInterface $logger, |
26 | 28 | private ITempManager $tempManager, |
27 | 29 | private ISetupManager $setupManager, |
| 30 | + private readonly IJobRuns $jobRuns, |
| 31 | + private readonly JobClassesRegistry $jobClassesRegistry, |
28 | 32 | ) { |
29 | 33 | parent::__construct($jobList, $logger); |
30 | 34 | } |
| 35 | + |
31 | 36 | #[\Override] |
32 | 37 | protected function configure(): void { |
33 | 38 | parent::configure(); |
@@ -124,13 +129,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int |
124 | 129 | continue; |
125 | 130 | } |
126 | 131 |
|
127 | | - $output->writeln('Running job ' . get_class($job) . ' with ID ' . $job->getId()); |
| 132 | + $jobClassName = get_class($job); |
| 133 | + $output->writeln('Running job ' . $jobClassName . ' with ID ' . $job->getId()); |
128 | 134 |
|
129 | 135 | if ($output->isVerbose()) { |
130 | 136 | $this->printJobInfo($job->getId(), $job, $output); |
131 | 137 | } |
132 | 138 |
|
| 139 | + memory_reset_peak_usage(); |
| 140 | + $jobClassId = $this->jobClassesRegistry->getId($jobClassName); |
| 141 | + $jobRunId = $this->jobRuns->started($jobClassId); |
| 142 | + $startTime = microtime(true); |
133 | 143 | $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 | + |
134 | 151 | $output->writeln('Job ' . $job->getId() . ' has finished', OutputInterface::VERBOSITY_VERBOSE); |
135 | 152 |
|
136 | 153 | // clean up after unclean jobs |
|
0 commit comments