Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -3011,4 +3011,12 @@
* Defaults to ``0``.
*/
'preview_expiration_days' => 0,

/**
* Delete job runs older than a certain number of days.
* Less than one day is not allowed.
*
* Defaults to ``60``.
*/
'background_jobs_expiration_days' => 60,
];
85 changes: 85 additions & 0 deletions core/BackgroundJobs/CleanupBackgroundJobsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\BackgroundJobs;

use DateTimeImmutable;
use OC\BackgroundJob\JobRuns;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\JobStatus;
use OCP\BackgroundJob\TimedJob;
use OCP\IConfig;
use Override;
use Psr\Log\LoggerInterface;
use RuntimeException;

class CleanupBackgroundJobsJob extends TimedJob {
public function __construct(
ITimeFactory $time,
private readonly JobRuns $jobRuns,
private readonly IServerInfo $serverInfo,

Check failure on line 27 in core/BackgroundJobs/CleanupBackgroundJobsJob.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

core/BackgroundJobs/CleanupBackgroundJobsJob.php:27:3: UndefinedClass: Class, interface or enum named OC\Core\BackgroundJobs\IServerInfo does not exist (see https://psalm.dev/019)
private readonly IConfig $config,
private readonly LoggerInterface $logger,
) {
parent::__construct($time);
$this->setInterval(60 * 60);
Comment thread
come-nc marked this conversation as resolved.
$this->setTimeSensitivity(IJob::TIME_SENSITIVE);
}

#[Override]
protected function run($argument): void {
$this->reapCrashedJobs();
$this->cleanOldestRuns();
}

private function reapCrashedJobs(): void {
$currentServerId = $this->serverInfo->serverId();

Check failure on line 43 in core/BackgroundJobs/CleanupBackgroundJobsJob.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

core/BackgroundJobs/CleanupBackgroundJobsJob.php:43:22: UndefinedClass: Class, interface or enum named OC\Core\BackgroundJobs\IServerInfo does not exist (see https://psalm.dev/019)

foreach ($this->jobRuns->runningJobs(1000) as $job) {
if ($job->serverId !== $currentServerId) {
continue;
}
$output = [];
$result = 0;
exec('ps -p ' . escapeshellarg((string)$job->pid) . ' -o cmd', $output, $result);
if (count($output) === 1 && current($output) === 'CMD' && $result === 1) {
// Process doesn't exists anymore
$maxDuration = (new DateTimeImmutable())->diff($job->startedAt);
$maxDuration
= ($maxDuration->days * 24 * 60 * 60 * 1000)
+ ($maxDuration->h * 60 * 60 * 1000)
+ ($maxDuration->i * 60 * 1000)
+ ($maxDuration->s * 1000)
+ (int)($maxDuration->f * 1000);
$this->jobRuns->finished($job->runId, $maxDuration, 0, JobStatus::CRASHED);
$this->logger->warning('No process matching PID {pid} found on server {serverId}. Job {runId} was marked as crashed', [
'pid' => $job->pid,
'serverId' => $job->serverId,
'runId' => $job->runId,
]);
}
}
}

private function cleanOldestRuns(): void {
$daysToKeep = $this->config->getSystemValueInt('background_jobs_expiration_days', 60);
if ($daysToKeep < 1) {
throw new RuntimeException('Invalid number of days');
}
$cleanBeforeTimestamp = time() - ($daysToKeep * 24 * 3600);

$cleanedJobs = $this->jobRuns->deleteBefore($cleanBeforeTimestamp);
if ($cleanedJobs > 0) {
$this->logger->info(
'Cleanup of old background jobs. Number of jobs removed: ' . $cleanedJobs . 'Reason: older than ' . $daysToKeep . ' days.',
);
}
}
}
6 changes: 3 additions & 3 deletions core/Command/Background/JobsHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use OC\BackgroundJob\JobRuns;
use OC\Core\Command\Base;
use OCP\BackgroundJob\JobStatus;
use OCP\IConfig;
use OCP\IServerInfo;
use OCP\Util;
use Override;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -23,7 +23,7 @@
final class JobsHistory extends Base {
public function __construct(
private readonly JobRuns $jobRuns,
private IConfig $config,
private readonly IServerInfo $serverInfo,
) {
parent::__construct();
}
Expand Down Expand Up @@ -75,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function formatLine(iterable $jobs): \Generator {
$jobsInfo = [];
$now = time();
$currentServerId = $this->config->getSystemValueInt('serverid', -1);
$currentServerId = $this->serverInfo->serverId();
foreach ($jobs as $job) {
$status = match ($job->status) {
JobStatus::RUNNING => 'Running',
Expand Down
6 changes: 3 additions & 3 deletions core/Command/Background/RunningJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use OC\BackgroundJob\JobRuns;
use OC\Core\Command\Base;
use OCP\IConfig;
use OCP\IServerInfo;
use Override;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -20,7 +20,7 @@
final class RunningJobs extends Base {
public function __construct(
private readonly JobRuns $jobRuns,
private IConfig $config,
private readonly IServerInfo $serverInfo,
) {
parent::__construct();
}
Expand Down Expand Up @@ -60,7 +60,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function formatLine(iterable $jobs): \Generator {
$now = time();
$currentServerId = $this->config->getSystemValueInt('serverid', -1);
$currentServerId = $this->serverInfo->serverId();
foreach ($jobs as $job) {
yield [
'Run ID' => $job->runId,
Expand Down
4 changes: 4 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@
'OCP\\IRequest' => $baseDir . '/lib/public/IRequest.php',
'OCP\\IRequestId' => $baseDir . '/lib/public/IRequestId.php',
'OCP\\IServerContainer' => $baseDir . '/lib/public/IServerContainer.php',
'OCP\\IServerInfo' => $baseDir . '/lib/public/IServerInfo.php',
'OCP\\ISession' => $baseDir . '/lib/public/ISession.php',
'OCP\\IStreamImage' => $baseDir . '/lib/public/IStreamImage.php',
'OCP\\ITagManager' => $baseDir . '/lib/public/ITagManager.php',
Expand Down Expand Up @@ -1310,6 +1311,7 @@
'OC\\Core\\AppInfo\\ConfigLexicon' => $baseDir . '/core/AppInfo/ConfigLexicon.php',
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => $baseDir . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => $baseDir . '/core/BackgroundJobs/CheckForUserCertificates.php',
'OC\\Core\\BackgroundJobs\\CleanupBackgroundJobsJob' => $baseDir . '/core/BackgroundJobs/CleanupBackgroundJobsJob.php',
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => $baseDir . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
'OC\\Core\\BackgroundJobs\\ExpirePreviewsJob' => $baseDir . '/core/BackgroundJobs/ExpirePreviewsJob.php',
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => $baseDir . '/core/BackgroundJobs/GenerateMetadataJob.php',
Expand Down Expand Up @@ -2045,6 +2047,7 @@
'OC\\Repair' => $baseDir . '/lib/private/Repair.php',
'OC\\RepairException' => $baseDir . '/lib/private/RepairException.php',
'OC\\Repair\\AddBruteForceCleanupJob' => $baseDir . '/lib/private/Repair/AddBruteForceCleanupJob.php',
'OC\\Repair\\AddCleanupBackgroundJobsJob' => $baseDir . '/lib/private/Repair/AddCleanupBackgroundJobsJob.php',
'OC\\Repair\\AddCleanupDeletedUsersBackgroundJob' => $baseDir . '/lib/private/Repair/AddCleanupDeletedUsersBackgroundJob.php',
'OC\\Repair\\AddCleanupUpdaterBackupsJob' => $baseDir . '/lib/private/Repair/AddCleanupUpdaterBackupsJob.php',
'OC\\Repair\\AddMetadataGenerationJob' => $baseDir . '/lib/private/Repair/AddMetadataGenerationJob.php',
Expand Down Expand Up @@ -2167,6 +2170,7 @@
'OC\\Security\\VerificationToken\\VerificationToken' => $baseDir . '/lib/private/Security/VerificationToken/VerificationToken.php',
'OC\\Server' => $baseDir . '/lib/private/Server.php',
'OC\\ServerContainer' => $baseDir . '/lib/private/ServerContainer.php',
'OC\\ServerInfo' => $baseDir . '/lib/private/ServerInfo.php',
'OC\\ServerNotAvailableException' => $baseDir . '/lib/private/ServerNotAvailableException.php',
'OC\\ServiceUnavailableException' => $baseDir . '/lib/private/ServiceUnavailableException.php',
'OC\\Session\\CryptoSessionData' => $baseDir . '/lib/private/Session/CryptoSessionData.php',
Expand Down
4 changes: 4 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\IRequest' => __DIR__ . '/../../..' . '/lib/public/IRequest.php',
'OCP\\IRequestId' => __DIR__ . '/../../..' . '/lib/public/IRequestId.php',
'OCP\\IServerContainer' => __DIR__ . '/../../..' . '/lib/public/IServerContainer.php',
'OCP\\IServerInfo' => __DIR__ . '/../../..' . '/lib/public/IServerInfo.php',
'OCP\\ISession' => __DIR__ . '/../../..' . '/lib/public/ISession.php',
'OCP\\IStreamImage' => __DIR__ . '/../../..' . '/lib/public/IStreamImage.php',
'OCP\\ITagManager' => __DIR__ . '/../../..' . '/lib/public/ITagManager.php',
Expand Down Expand Up @@ -1351,6 +1352,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\AppInfo\\ConfigLexicon' => __DIR__ . '/../../..' . '/core/AppInfo/ConfigLexicon.php',
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CheckForUserCertificates.php',
'OC\\Core\\BackgroundJobs\\CleanupBackgroundJobsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CleanupBackgroundJobsJob.php',
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
'OC\\Core\\BackgroundJobs\\ExpirePreviewsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/ExpirePreviewsJob.php',
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/GenerateMetadataJob.php',
Expand Down Expand Up @@ -2086,6 +2088,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Repair' => __DIR__ . '/../../..' . '/lib/private/Repair.php',
'OC\\RepairException' => __DIR__ . '/../../..' . '/lib/private/RepairException.php',
'OC\\Repair\\AddBruteForceCleanupJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddBruteForceCleanupJob.php',
'OC\\Repair\\AddCleanupBackgroundJobsJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddCleanupBackgroundJobsJob.php',
'OC\\Repair\\AddCleanupDeletedUsersBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddCleanupDeletedUsersBackgroundJob.php',
'OC\\Repair\\AddCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddCleanupUpdaterBackupsJob.php',
'OC\\Repair\\AddMetadataGenerationJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddMetadataGenerationJob.php',
Expand Down Expand Up @@ -2208,6 +2211,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Security\\VerificationToken\\VerificationToken' => __DIR__ . '/../../..' . '/lib/private/Security/VerificationToken/VerificationToken.php',
'OC\\Server' => __DIR__ . '/../../..' . '/lib/private/Server.php',
'OC\\ServerContainer' => __DIR__ . '/../../..' . '/lib/private/ServerContainer.php',
'OC\\ServerInfo' => __DIR__ . '/../../..' . '/lib/private/ServerInfo.php',
'OC\\ServerNotAvailableException' => __DIR__ . '/../../..' . '/lib/private/ServerNotAvailableException.php',
'OC\\ServiceUnavailableException' => __DIR__ . '/../../..' . '/lib/private/ServiceUnavailableException.php',
'OC\\Session\\CryptoSessionData' => __DIR__ . '/../../..' . '/lib/private/Session/CryptoSessionData.php',
Expand Down
12 changes: 12 additions & 0 deletions lib/private/BackgroundJob/JobRuns.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public function finished(int|string $runId, int $duration, int $memoryPeakUsage,
return $result === 1;
}

public function deleteBefore(int $timestamp): int {
$beforeSnowflake = $this->snowflakeGenerator->minForTimeId($timestamp);
$beforeSnowflake = '91480652934574081';
$qb = $this->connection->getQueryBuilder();
$result = $qb
->delete(self::TABLE)
->where($qb->expr()->lt('run_id', $qb->createNamedParameter($beforeSnowflake)))
->executeStatement();

return $result;
}

#[Override]
public function runningJobs(int $limit = 200): \Generator {
$qb = $this->connection->getQueryBuilder();
Expand Down
6 changes: 4 additions & 2 deletions lib/private/Repair.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OC;

use OC\Repair\AddBruteForceCleanupJob;
use OC\Repair\AddCleanupBackgroundJobsJob;
use OC\Repair\AddCleanupDeletedUsersBackgroundJob;
use OC\Repair\AddCleanupUpdaterBackupsJob;
use OC\Repair\AddMetadataGenerationJob;
Expand Down Expand Up @@ -134,14 +135,14 @@ public function addStep(IRepairStep|string $repairStep, bool $includeExpensive =
}
}

if (!($s instanceof IRepairStep)) {
if (!$s instanceof IRepairStep) {
throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
}

$repairStep = $s;
}

if (($repairStep instanceof IRepairStepExpensive) && !$includeExpensive) {
if ($repairStep instanceof IRepairStepExpensive && !$includeExpensive) {
$this->debug("Skipping expensive repair step '" . $repairStep::class . "'");
} else {
$this->repairSteps[] = $repairStep;
Expand Down Expand Up @@ -195,6 +196,7 @@ public static function getRepairSteps(bool $includeExpensive = false): array {
Server::get(SanitizeAccountProperties::class),
Server::get(AddMovePreviewJob::class),
Server::get(ConfigKeyMigration::class),
Server::get(AddCleanupBackgroundJobsJob::class),
];

if ($includeExpensive) {
Expand Down
33 changes: 33 additions & 0 deletions lib/private/Repair/AddCleanupBackgroundJobsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Repair;

use OC\Core\BackgroundJobs\CleanupBackgroundJobsJob;
use OCP\BackgroundJob\IJobList;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use Override;

class AddCleanupBackgroundJobsJob implements IRepairStep {
public function __construct(
private readonly IJobList $jobList,
) {
}

#[\Override]
public function getName(): string {
return 'Cleanup completed background jobs';
}

#[Override]
public function run(IOutput $output): void {
$this->jobList->add(CleanupBackgroundJobsJob::class);
}
}
2 changes: 2 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
use OCP\IRequest;
use OCP\IRequestId;
use OCP\IServerContainer;
use OCP\IServerInfo;
use OCP\ISession;
use OCP\ITagManager;
use OCP\ITempManager;
Expand Down Expand Up @@ -1307,6 +1308,7 @@ public function __construct(
}, false);
$this->registerAlias(ISnowflakeDecoder::class, SnowflakeDecoder::class);
$this->registerAlias(IJobRuns::class, JobRuns::class);
$this->registerAlias(IServerInfo::class, ServerInfo::class);

$this->connectDispatcher();
}
Expand Down
45 changes: 45 additions & 0 deletions lib/private/ServerInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC;

use OCP\IConfig;
use OCP\IServerInfo;
use Override;

readonly class ServerInfo implements IServerInfo {
public function __construct(
private IConfig $config,
) {
}

#[Override]
public function serverId(): int {
$serverid = $this->config->getSystemValueInt('serverid', -1);
if ($serverid < 1) {
// Fallback: generates a server ID based on hostname
/** @var int<0,max> */
$serverid = hexdec(hash('xxh32', $this->hostname()));
}

/** @var int<0,511> */
return $serverid & 0x1FF;
}

#[Override]
public function hostname(): string {
$hostname = gethostname();
if ($hostname === false) {
// Use a random hostname
return bin2hex(random_bytes(8));
}

return $hostname;
}
}
2 changes: 2 additions & 0 deletions lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OC\AppFramework\Bootstrap\Coordinator;
use OC\Authentication\Token\PublicKeyTokenProvider;
use OC\Authentication\Token\TokenCleanupJob;
use OC\Core\BackgroundJobs\CleanupBackgroundJobsJob;
use OC\Core\BackgroundJobs\ExpirePreviewsJob;
use OC\Core\BackgroundJobs\GenerateMetadataJob;
use OC\Core\BackgroundJobs\PreviewMigrationJob;
Expand Down Expand Up @@ -532,6 +533,7 @@ public static function installBackgroundJobs(): void {
$jobList->add(GenerateMetadataJob::class);
$jobList->add(PreviewMigrationJob::class);
$jobList->add(ExpirePreviewsJob::class);
$jobList->add(CleanupBackgroundJobsJob::class);
}

/**
Expand Down
Loading
Loading