Skip to content

Commit dc5499a

Browse files
committed
feat(jobs): clean old job runs
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 28d32d8 commit dc5499a

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

config/config.sample.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,4 +3002,12 @@
30023002
* Defaults to ``0``.
30033003
*/
30043004
'preview_expiration_days' => 0,
3005+
3006+
/**
3007+
* Delete job runs older than a certain number of days.
3008+
* Less than one day is not allowed.
3009+
*
3010+
* Defaults to ``60``.
3011+
*/
3012+
'background_jobs_expiration_days' => 60,
30053013
];

core/BackgroundJobs/CleanupBackgroundJobsJob.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
use OCP\IServerInfo;
2020
use Override;
2121
use Psr\Log\LoggerInterface;
22+
use RuntimeException;
2223

2324
class CleanupBackgroundJobsJob extends TimedJob {
2425
public function __construct(
2526
ITimeFactory $time,
2627
private readonly JobRuns $jobRuns,
2728
private readonly IServerInfo $serverInfo,
29+
private readonly IConfig $config,
2830
private readonly LoggerInterface $logger,
2931
) {
3032
parent::__construct($time);
@@ -35,8 +37,7 @@ public function __construct(
3537
#[Override]
3638
protected function run($argument): void {
3739
$this->reapCrashedJobs();
38-
39-
// TODO Clean oldest jobs
40+
$this->cleanOldestRuns();
4041
}
4142

4243
private function reapCrashedJobs(): void {
@@ -67,4 +68,19 @@ private function reapCrashedJobs(): void {
6768
}
6869
}
6970
}
71+
72+
private function cleanOldestRuns(): void {
73+
$daysToKeep = $this->config->getSystemValueInt('background_jobs_expiration_days', 60);
74+
if ($daysToKeep < 1) {
75+
throw new RuntimeException('Invalid number of days');
76+
}
77+
$cleanBeforeTimestamp = time() - ($daysToKeep * 24 * 3600);
78+
79+
$cleanedJobs = $this->jobRuns->deleteBefore($cleanBeforeTimestamp);
80+
if ($cleanedJobs > 0) {
81+
$this->logger->info(
82+
'Cleanup of old background jobs. Number of jobs removed: ' . $cleanedJobs . 'Reason: older than ' . $daysToKeep . ' days.',
83+
);
84+
}
85+
}
7086
}

lib/private/BackgroundJob/JobRuns.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ public function finished(int|string $runId, int $duration, int $memoryPeakUsage,
6262
return $result === 1;
6363
}
6464

65+
public function deleteBefore(int $timestamp): int {
66+
$beforeSnowflake = $this->snowflakeGenerator->minForTimeId($timestamp);
67+
$beforeSnowflake = '91480652934574081';
68+
$qb = $this->connection->getQueryBuilder();
69+
$result = $qb
70+
->delete(self::TABLE)
71+
->where($qb->expr()->lt('run_id', $qb->createNamedParameter($beforeSnowflake)))
72+
->executeStatement();
73+
74+
return $result;
75+
}
76+
6577
#[Override]
6678
public function runningJobs(int $limit = 200): \Generator {
6779
$qb = $this->connection->getQueryBuilder();

0 commit comments

Comments
 (0)