Skip to content

Commit 3956e29

Browse files
committed
feat(jobs): add cleanup job for job run history
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 60ce92a commit 3956e29

5 files changed

Lines changed: 26 additions & 10 deletions

File tree

core/BackgroundJobs/CleanupBackgroundJobsJob.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\IConfig;
1919
use OCP\IServerInfo;
2020
use Override;
21+
use Psr\Log\LoggerInterface;
2122

2223
class CleanupBackgroundJobsJob extends TimedJob {
2324
public function __construct(
@@ -28,7 +29,7 @@ public function __construct(
2829
) {
2930
parent::__construct($time);
3031
$this->setInterval(60 * 60);
31-
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
32+
$this->setTimeSensitivity(IJob::TIME_SENSITIVE);
3233
}
3334

3435
#[Override]
@@ -45,17 +46,24 @@ private function reapCrashedJobs(): void {
4546
if ($job->serverId !== $currentServerId) {
4647
continue;
4748
}
49+
$output = [];
50+
$result = 0;
4851
exec('ps -p ' . escapeshellarg((string)$job->pid) . ' -o cmd', $output, $result);
49-
if (count($output) === 1 && $output[0] === 'CMD' && $result === 1) {
52+
if (count($output) === 1 && current($output) === 'CMD' && $result === 1) {
5053
// Process doesn't exists anymore
5154
$maxDuration = (new DateTimeImmutable())->diff($job->startedAt);
52-
$maxDuration =
53-
($maxDuration->format('%a') * 24 * 60 * 60 * 1000)
54-
+ ($maxDuration->format('%h') * 60 * 60 * 1000)
55-
+ ($maxDuration->format('%m') * 60 * 1000)
56-
+ ($maxDuration->format('%s') * 1000)
57-
+ (int)($maxDuration->format('%f') / 1000);
55+
$maxDuration
56+
= ($maxDuration->days * 24 * 60 * 60 * 1000)
57+
+ ($maxDuration->h * 60 * 60 * 1000)
58+
+ ($maxDuration->i * 60 * 1000)
59+
+ ($maxDuration->s * 1000)
60+
+ (int)($maxDuration->f * 1000);
5861
$this->jobRuns->finished($job->runId, $maxDuration, 0, JobStatus::CRASHED);
62+
$this->logger->warning('No process matching PID {pid} found on server {serverId}. Job {runId} was marked as crashed', [
63+
'pid' => $job->pid,
64+
'serverId' => $job->serverId,
65+
'runId' => $job->runId,
66+
]);
5967
}
6068
}
6169
}

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,7 @@
13121312
'OC\\Core\\AppInfo\\ConfigLexicon' => $baseDir . '/core/AppInfo/ConfigLexicon.php',
13131313
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => $baseDir . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
13141314
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => $baseDir . '/core/BackgroundJobs/CheckForUserCertificates.php',
1315+
'OC\\Core\\BackgroundJobs\\CleanupBackgroundJobsJob' => $baseDir . '/core/BackgroundJobs/CleanupBackgroundJobsJob.php',
13151316
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => $baseDir . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
13161317
'OC\\Core\\BackgroundJobs\\ExpirePreviewsJob' => $baseDir . '/core/BackgroundJobs/ExpirePreviewsJob.php',
13171318
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => $baseDir . '/core/BackgroundJobs/GenerateMetadataJob.php',
@@ -2052,6 +2053,7 @@
20522053
'OC\\Repair' => $baseDir . '/lib/private/Repair.php',
20532054
'OC\\RepairException' => $baseDir . '/lib/private/RepairException.php',
20542055
'OC\\Repair\\AddBruteForceCleanupJob' => $baseDir . '/lib/private/Repair/AddBruteForceCleanupJob.php',
2056+
'OC\\Repair\\AddCleanupBackgroundJobsJob' => $baseDir . '/lib/private/Repair/AddCleanupBackgroundJobsJob.php',
20552057
'OC\\Repair\\AddCleanupDeletedUsersBackgroundJob' => $baseDir . '/lib/private/Repair/AddCleanupDeletedUsersBackgroundJob.php',
20562058
'OC\\Repair\\AddCleanupUpdaterBackupsJob' => $baseDir . '/lib/private/Repair/AddCleanupUpdaterBackupsJob.php',
20572059
'OC\\Repair\\AddMetadataGenerationJob' => $baseDir . '/lib/private/Repair/AddMetadataGenerationJob.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
13531353
'OC\\Core\\AppInfo\\ConfigLexicon' => __DIR__ . '/../../..' . '/core/AppInfo/ConfigLexicon.php',
13541354
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
13551355
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CheckForUserCertificates.php',
1356+
'OC\\Core\\BackgroundJobs\\CleanupBackgroundJobsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CleanupBackgroundJobsJob.php',
13561357
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
13571358
'OC\\Core\\BackgroundJobs\\ExpirePreviewsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/ExpirePreviewsJob.php',
13581359
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/GenerateMetadataJob.php',
@@ -2093,6 +2094,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
20932094
'OC\\Repair' => __DIR__ . '/../../..' . '/lib/private/Repair.php',
20942095
'OC\\RepairException' => __DIR__ . '/../../..' . '/lib/private/RepairException.php',
20952096
'OC\\Repair\\AddBruteForceCleanupJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddBruteForceCleanupJob.php',
2097+
'OC\\Repair\\AddCleanupBackgroundJobsJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddCleanupBackgroundJobsJob.php',
20962098
'OC\\Repair\\AddCleanupDeletedUsersBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddCleanupDeletedUsersBackgroundJob.php',
20972099
'OC\\Repair\\AddCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddCleanupUpdaterBackupsJob.php',
20982100
'OC\\Repair\\AddMetadataGenerationJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddMetadataGenerationJob.php',

lib/private/Repair.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OC;
1010

1111
use OC\Repair\AddBruteForceCleanupJob;
12+
use OC\Repair\AddCleanupBackgroundJobsJob;
1213
use OC\Repair\AddCleanupDeletedUsersBackgroundJob;
1314
use OC\Repair\AddCleanupUpdaterBackupsJob;
1415
use OC\Repair\AddMetadataGenerationJob;
@@ -134,14 +135,14 @@ public function addStep(IRepairStep|string $repairStep, bool $includeExpensive =
134135
}
135136
}
136137

137-
if (!($s instanceof IRepairStep)) {
138+
if (!$s instanceof IRepairStep) {
138139
throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
139140
}
140141

141142
$repairStep = $s;
142143
}
143144

144-
if (($repairStep instanceof IRepairStepExpensive) && !$includeExpensive) {
145+
if ($repairStep instanceof IRepairStepExpensive && !$includeExpensive) {
145146
$this->debug("Skipping expensive repair step '" . $repairStep::class . "'");
146147
} else {
147148
$this->repairSteps[] = $repairStep;
@@ -195,6 +196,7 @@ public static function getRepairSteps(bool $includeExpensive = false): array {
195196
Server::get(SanitizeAccountProperties::class),
196197
Server::get(AddMovePreviewJob::class),
197198
Server::get(ConfigKeyMigration::class),
199+
Server::get(AddCleanupBackgroundJobsJob::class),
198200
];
199201

200202
if ($includeExpensive) {

lib/private/Setup.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OC\AppFramework\Bootstrap\Coordinator;
1717
use OC\Authentication\Token\PublicKeyTokenProvider;
1818
use OC\Authentication\Token\TokenCleanupJob;
19+
use OC\Core\BackgroundJobs\CleanupBackgroundJobsJob;
1920
use OC\Core\BackgroundJobs\ExpirePreviewsJob;
2021
use OC\Core\BackgroundJobs\GenerateMetadataJob;
2122
use OC\Core\BackgroundJobs\PreviewMigrationJob;
@@ -532,6 +533,7 @@ public static function installBackgroundJobs(): void {
532533
$jobList->add(GenerateMetadataJob::class);
533534
$jobList->add(PreviewMigrationJob::class);
534535
$jobList->add(ExpirePreviewsJob::class);
536+
$jobList->add(CleanupBackgroundJobsJob::class);
535537
}
536538

537539
/**

0 commit comments

Comments
 (0)