feat(jobs): add cleanup job for job run history

Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
This commit is contained in:
Benjamin Gaussorgues 2026-06-03 18:27:46 +02:00
parent 60ce92a697
commit 3956e292b4
No known key found for this signature in database
5 changed files with 26 additions and 10 deletions

View file

@ -18,6 +18,7 @@ use OCP\BackgroundJob\TimedJob;
use OCP\IConfig;
use OCP\IServerInfo;
use Override;
use Psr\Log\LoggerInterface;
class CleanupBackgroundJobsJob extends TimedJob {
public function __construct(
@ -28,7 +29,7 @@ class CleanupBackgroundJobsJob extends TimedJob {
) {
parent::__construct($time);
$this->setInterval(60 * 60);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
$this->setTimeSensitivity(IJob::TIME_SENSITIVE);
}
#[Override]
@ -45,17 +46,24 @@ class CleanupBackgroundJobsJob extends TimedJob {
if ($job->serverId !== $currentServerId) {
continue;
}
$output = [];
$result = 0;
exec('ps -p ' . escapeshellarg((string)$job->pid) . ' -o cmd', $output, $result);
if (count($output) === 1 && $output[0] === 'CMD' && $result === 1) {
if (count($output) === 1 && current($output) === 'CMD' && $result === 1) {
// Process doesn't exists anymore
$maxDuration = (new DateTimeImmutable())->diff($job->startedAt);
$maxDuration =
($maxDuration->format('%a') * 24 * 60 * 60 * 1000)
+ ($maxDuration->format('%h') * 60 * 60 * 1000)
+ ($maxDuration->format('%m') * 60 * 1000)
+ ($maxDuration->format('%s') * 1000)
+ (int)($maxDuration->format('%f') / 1000);
$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,
]);
}
}
}

View file

@ -1312,6 +1312,7 @@ return array(
'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',
@ -2052,6 +2053,7 @@ return array(
'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',

View file

@ -1353,6 +1353,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',
@ -2093,6 +2094,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',

View file

@ -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;
@ -134,14 +135,14 @@ class Repair implements IOutput {
}
}
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;
@ -195,6 +196,7 @@ class Repair implements IOutput {
Server::get(SanitizeAccountProperties::class),
Server::get(AddMovePreviewJob::class),
Server::get(ConfigKeyMigration::class),
Server::get(AddCleanupBackgroundJobsJob::class),
];
if ($includeExpensive) {

View file

@ -16,6 +16,7 @@ use InvalidArgumentException;
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;
@ -532,6 +533,7 @@ class Setup {
$jobList->add(GenerateMetadataJob::class);
$jobList->add(PreviewMigrationJob::class);
$jobList->add(ExpirePreviewsJob::class);
$jobList->add(CleanupBackgroundJobsJob::class);
}
/**