mirror of
https://github.com/nextcloud/server.git
synced 2026-10-03 16:58:44 +02:00
The previous implementation executes "ps" with flags -p and -o to check
whether a job process was still running. This approach has portability
issues regarding e.g. BusyBox ps (Alpine Linux) where `ps -p` fails or
BSD/macOS where `ps -o` fails.
Replace with `posix_kill($pid, 0)`, the standard POSIX way to probe
process existence without delivering a signal. EPERM is treated as
"process exists but not owned by current user". This should be available
on all supported platforms.
Fixes: 60ce92a697
Suggested-by: Michele Marcionelli
Signed-off-by: Stefan Kalscheuer <stefan@stklcode.de>
85 lines
2.4 KiB
PHP
85 lines
2.4 KiB
PHP
<?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 OCP\IServerInfo;
|
|
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,
|
|
private readonly IConfig $config,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
parent::__construct($time);
|
|
$this->setInterval(60 * 60);
|
|
$this->setTimeSensitivity(IJob::TIME_SENSITIVE);
|
|
}
|
|
|
|
#[Override]
|
|
protected function run($argument): void {
|
|
$this->reapCrashedJobs();
|
|
$this->cleanOldestRuns();
|
|
}
|
|
|
|
private function reapCrashedJobs(): void {
|
|
$currentServerId = $this->serverInfo->getServerId();
|
|
|
|
foreach ($this->jobRuns->runningJobs(1000) as $job) {
|
|
if ($job->serverId !== $currentServerId) {
|
|
continue;
|
|
}
|
|
$processExists = posix_kill($job->pid, 0) || posix_get_last_error() === 1 /* EPERM */;
|
|
if (!$processExists) {
|
|
// Process doesn't exist 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.',
|
|
);
|
|
}
|
|
}
|
|
}
|