-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Background jobs improvements #60765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Background jobs improvements #60765
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8db8776
feat(jobs): introduce background job classes registry
Altahrim e5d91cc
feat(jobs): introduce JobStatus for job running state
Altahrim 4ee7516
feat(jobs): allow to keep track of job executions
Altahrim 28d54ed
feat(jobs): log job execution in CronService
Altahrim f881e95
fix(db): ensure no autoincrement for Oracle
Altahrim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Core\Command\Background; | ||
|
|
||
| use OC\BackgroundJob\JobRuns; | ||
| use OC\Core\Command\Base; | ||
| use OCP\IConfig; | ||
| use Override; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| final class RunningJobs extends Base { | ||
| public function __construct( | ||
| private readonly JobRuns $jobRuns, | ||
| private IConfig $config, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| #[Override] | ||
| protected function configure(): void { | ||
| parent::configure(); | ||
|
|
||
| $help = <<<EOF | ||
| Display all currently running background jobs. | ||
|
|
||
| You can find the following informations: | ||
| - <info>Run ID:</info> job identifier a found in database (Snowflake ID) | ||
| - <info>Class:</info> class of the job | ||
| - <info>Started at:</info> start time of the job | ||
| - <info>Server ID:</info> server ID as defined in <options=bold>config.php</> (see `serverid`). Highlighted if it’s running on current server. | ||
| - <info>PID:</info> PID of process executing the job | ||
| - <info>Running since:</info> human readable elapsed time since job started | ||
|
|
||
| EOF; | ||
|
|
||
| $this | ||
| ->setName('background-job:running') | ||
| ->setDescription('Show currently running jobs') | ||
| ->setHelp($help) | ||
| ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Maximum number of results returned by the command', 200); | ||
| } | ||
|
|
||
| #[Override] | ||
| protected function execute(InputInterface $input, OutputInterface $output): int { | ||
| $limit = (int)$input->getOption('limit'); | ||
| $jobs = $this->jobRuns->runningJobs($limit); | ||
| $this->writeStreamingTableInOutputFormat($input, $output, $this->formatLine($jobs), 20); | ||
|
|
||
| return Base::SUCCESS; | ||
| } | ||
|
|
||
| private function formatLine(iterable $jobs): \Generator { | ||
| $now = time(); | ||
| $currentServerId = $this->config->getSystemValueInt('serverid', -1); | ||
| foreach ($jobs as $job) { | ||
| yield [ | ||
| 'Run ID' => $job->runId, | ||
| 'Class' => $job->className, | ||
| 'Started at' => $job->startedAt->format('Y-m-d H:i:s'), | ||
| 'Server ID' => $job->serverId === $currentServerId ? '<info>' . $job->serverId . '</info>' : $job->serverId, | ||
| 'PID' => $job->pid, | ||
| 'Running since' => $this->formatDuration($now - $job->startedAt->format('U')), | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * TODO Move this function to utils class with better formatting (plural, i18n…) | ||
| */ | ||
| private function formatDuration(int $seconds): string { | ||
| if ($seconds < 60) { | ||
| return sprintf('%d seconds', $seconds); | ||
| } | ||
| if ($seconds < 3600) { | ||
| return sprintf('%d minutes', $seconds / 60); | ||
| } | ||
| if ($seconds < (3600 * 24)) { | ||
| return sprintf('> %d hours', $seconds / 3600); | ||
| } | ||
|
|
||
| return sprintf('> %d days', $seconds / (3600 * 24)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Core\Migrations; | ||
|
|
||
| use Closure; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\Attributes\AddIndex; | ||
| use OCP\Migration\Attributes\CreateTable; | ||
| use OCP\Migration\Attributes\IndexType; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
| use Override; | ||
|
|
||
| #[CreateTable(table: 'job_classes_registry', columns: ['class_id', 'class_name'], description: 'New table to map job class name to an ID')] | ||
| #[AddIndex(table: 'job_classes_registry', type: IndexType::PRIMARY)] | ||
| #[AddIndex(table: 'job_classes_registry', type: IndexType::UNIQUE, description: 'Ensure each class is registered only once')] | ||
| class Version34000Date20260518163022 extends SimpleMigrationStep { | ||
| #[Override] | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
|
|
||
| if (!$schema->hasTable('job_classes_registry')) { | ||
| $table = $schema->createTable('job_classes_registry'); | ||
| $table->addColumn('class_id', Types::BIGINT, [ | ||
| 'autoincrement' => true, | ||
| 'notnull' => true, | ||
| 'unsigned' => true, | ||
| ]); | ||
| $table->addColumn('class_name', Types::STRING, ['notnull' => true, 'length' => 255]); | ||
| $table->setPrimaryKey(['class_id']); | ||
| $table->addUniqueConstraint(['class_name'], 'class_index'); | ||
| // Makes sure there is no auto-increment in Oracle | ||
| $schema->dropAutoincrementColumn('job_classes_registry', 'class_id'); | ||
|
|
||
| return $schema; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Core\Migrations; | ||
|
|
||
| use Closure; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\Attributes\AddIndex; | ||
| use OCP\Migration\Attributes\CreateTable; | ||
| use OCP\Migration\Attributes\IndexType; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
| use Override; | ||
|
|
||
| #[CreateTable( | ||
| table: 'job_runs', | ||
| columns: ['class_id', 'pid', 'status', 'duration', 'ram_peak_usage'], | ||
| description: 'New table to store executions of background jobs', | ||
| )] | ||
| #[AddIndex(table: 'job_runs', type: IndexType::PRIMARY)] | ||
| #[AddIndex(table: 'job_runs', type: IndexType::INDEX, description: 'Allows to search on job status')] | ||
| class Version34000Date20260521110333 extends SimpleMigrationStep { | ||
|
Altahrim marked this conversation as resolved.
|
||
| #[Override] | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
|
|
||
| if (!$schema->hasTable('job_runs')) { | ||
|
Altahrim marked this conversation as resolved.
|
||
| $table = $schema->createTable('job_runs'); | ||
| $table->addColumn('run_id', Types::BIGINT, [ | ||
| 'notnull' => true, | ||
| 'unsigned' => true, | ||
| ]); | ||
| $table->addColumn('class_id', Types::BIGINT, ['notnull' => true]); | ||
| $table->addColumn('pid', Types::INTEGER, ['notnull' => true]); // Should be MEDIUMINT | ||
| $table->addColumn('status', Types::SMALLINT, ['notnull' => true]); // Should be TINYINT | ||
| $table->addColumn('duration', Types::INTEGER, ['notnull' => true, 'default' => 0]); | ||
| $table->addColumn('ram_peak_usage', Types::INTEGER, ['notnull' => true, 'default' => 0]); // Should be MEDIUMINT | ||
| $table->setPrimaryKey(['run_id']); | ||
| $table->addIndex(['status'], 'status'); | ||
| // Makes sure there is no auto-increment in Oracle | ||
| $schema->dropAutoincrementColumn('job_runs', 'run_id'); | ||
|
|
||
| return $schema; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.