-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Job run history cleanup #60972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Altahrim
wants to merge
4
commits into
master
Choose a base branch
from
feat/clean_crashed_jobs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Job run history cleanup #60972
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e958a01
feat(utils): add getter for serverid with proper default
Altahrim 81559ea
feat(jobs): add cleanup job for job run history
Altahrim e75324d
feat(snowflake): allows to generate Snowflake IDs matching a timestamp
Altahrim bd26562
feat(jobs): clean old job runs
Altahrim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 OCP\Util; | ||
| use Override; | ||
| use Psr\Log\LoggerInterface; | ||
| use RuntimeException; | ||
|
|
||
| class CleanupBackgroundJobsJob extends TimedJob { | ||
| public function __construct( | ||
| ITimeFactory $time, | ||
| private readonly JobRuns $jobRuns, | ||
| 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 = Util::getServerId(); | ||
|
|
||
| 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.', | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s one hour? Should be one day, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought one hour would be better so the duration will be closer to the real one (± 1 hour) but then it should be time sensitive