|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OC\Core\Command\Background; |
| 11 | + |
| 12 | +use OC\BackgroundJob\JobClassesRegistry; |
| 13 | +use OC\BackgroundJob\JobRuns; |
| 14 | +use OC\Core\Command\Base; |
| 15 | +use OCP\BackgroundJob\JobStatus; |
| 16 | +use OCP\IConfig; |
| 17 | +use OCP\Util; |
| 18 | +use Override; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Console\Input\InputOption; |
| 21 | +use Symfony\Component\Console\Output\OutputInterface; |
| 22 | + |
| 23 | +final class JobsHistory extends Base { |
| 24 | + public function __construct( |
| 25 | + private readonly JobRuns $jobRuns, |
| 26 | + private readonly JobClassesRegistry $jobClassesRegistry, |
| 27 | + private IConfig $config, |
| 28 | + ) { |
| 29 | + parent::__construct(); |
| 30 | + } |
| 31 | + |
| 32 | + #[Override] |
| 33 | + protected function configure(): void { |
| 34 | + parent::configure(); |
| 35 | + |
| 36 | + $help = <<<EOF |
| 37 | + Display all currently running background jobs. |
| 38 | +
|
| 39 | + You can find the following informations: |
| 40 | + - <info>Run ID:</info> job identifier a found in database (Snowflake ID) |
| 41 | + - <info>Class:</info> class of the job |
| 42 | + - <info>Started at:</info> start time of the job |
| 43 | + - <info>Server ID:</info> server ID as defined in <options=bold>config.php</> (see `serverid`). Highlighted if it’s running on current server. |
| 44 | + - <info>PID:</info> PID of process executing the job |
| 45 | + - <info>Duration:</info> human readable duration |
| 46 | + - <info>Memory usage:</info> human readable memory usage peak |
| 47 | +
|
| 48 | + EOF; |
| 49 | + |
| 50 | + $this |
| 51 | + ->setName('background-job:history') |
| 52 | + ->setDescription('Show currently running jobs') |
| 53 | + ->setHelp($help) |
| 54 | + ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Maximum number of results returned by the command', 200) |
| 55 | + ->addOption('class', 'c', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Filter by class name', []) |
| 56 | + ->addOption('status', 's', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Filter by status', []); |
| 57 | + } |
| 58 | + |
| 59 | + #[Override] |
| 60 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 61 | + $limit = (int)$input->getOption('limit'); |
| 62 | + $classesId = array_map(fn (string $value) => $this->jobClassesRegistry->getId($value), $input->getOption('class')); |
| 63 | + $statuses = array_map(fn (string $value) => JobStatus::tryFrom((int)$value), $input->getOption('status')); |
| 64 | + $jobs = $this->jobRuns->completedJobs($statuses, $classesId, $limit); |
| 65 | + $this->writeStreamingTableInOutputFormat($input, $output, $this->formatLine($jobs), 20); |
| 66 | + |
| 67 | + return Base::SUCCESS; |
| 68 | + } |
| 69 | + |
| 70 | + private function formatLine(iterable $jobs): \Generator { |
| 71 | + $jobsInfo = []; |
| 72 | + $now = time(); |
| 73 | + $currentServerId = $this->config->getSystemValueInt('serverid', -1); |
| 74 | + foreach ($jobs as $job) { |
| 75 | + $status = match ($job->status) { |
| 76 | + JobStatus::RUNNING => 'Running', |
| 77 | + JobStatus::SUCCEEDED => '<info>Succeeded</info>', |
| 78 | + JobStatus::FAILED => '<question>Failed</question>', |
| 79 | + JobStatus::CRASHED => '<error>Crashed</error>', |
| 80 | + default => 'Unknown', |
| 81 | + }; |
| 82 | + yield [ |
| 83 | + 'Run ID' => $job->runId, |
| 84 | + 'Status' => $status, |
| 85 | + 'Class' => $job->className, |
| 86 | + 'Started at' => $job->startedAt->format('Y-m-d H:i:s'), |
| 87 | + 'Server ID' => $job->serverId === $currentServerId ? '<info>' . $job->serverId . '</info>' : $job->serverId, |
| 88 | + 'PID' => $job->pid, |
| 89 | + 'Duration' => $job->duration . ' ms', |
| 90 | + 'Memory usage' => Util::humanFileSize($job->memoryPeak * 1024), |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + return $jobsInfo; |
| 95 | + } |
| 96 | +} |
0 commit comments