Repository navigation
perf(cleanup): Optimize cleanup job #9166
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
Merged
Merged
Changes from all commits
Commits
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
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,36 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Text\Migration; | ||
|
|
||
| use Closure; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\Attributes\AddColumn; | ||
| use OCP\Migration\Attributes\ColumnType; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
|
|
||
| #[AddColumn(table: 'text_documents', name: 'last_attachment_cleanup', type: ColumnType::BIGINT, description: 'Last saved timestamp processed by attachment cleanup')] | ||
| class Version100000Date20261011000000 extends SimpleMigrationStep { | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| $schema = $schemaClosure(); | ||
| $table = $schema->getTable('text_documents'); | ||
| if (!$table->hasColumn('last_attachment_cleanup')) { | ||
| $table->addColumn('last_attachment_cleanup', Types::BIGINT, [ | ||
| 'notnull' => true, | ||
| 'default' => 0, | ||
| 'unsigned' => true, | ||
| ]); | ||
| return $schema; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
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,118 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Text\Tests; | ||
|
|
||
| use OCA\Text\Cron\Cleanup; | ||
| use OCA\Text\Db\Document; | ||
| use OCA\Text\Service\AttachmentService; | ||
| use OCA\Text\Service\DocumentService; | ||
| use OCA\Text\Service\FileService; | ||
| use OCA\Text\Service\SessionService; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\Files\IFilenameValidator; | ||
| use OCP\Files\IMimeTypeDetector; | ||
| use OCP\Files\IRootFolder; | ||
| use OCP\Files\ISetupManager; | ||
| use OCP\Files\Node; | ||
| use OCP\FilesMetadata\IFilesMetadataManager; | ||
| use OCP\IPreview; | ||
| use OCP\ISession; | ||
| use OCP\IURLGenerator; | ||
| use OCP\Share\IManager; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class CleanupTest extends \PHPUnit\Framework\TestCase { | ||
| private Cleanup $cleanup; | ||
| private Document $document; | ||
| private DocumentService $documentService; | ||
| private AttachmentService $attachmentService; | ||
| private SessionService $sessionService; | ||
| private IRootFolder $rootFolder; | ||
| private ISetupManager $setupManager; | ||
|
|
||
| public function setUp(): void { | ||
| $this->document = Document::fromRow([ | ||
| 'id' => '123456789012345678', | ||
| 'context_type' => 'file', | ||
| 'context_id' => 123, | ||
| 'last_saved_version_time' => 1000, | ||
| ]); | ||
| $this->documentService = $this->createMock(DocumentService::class); | ||
| $this->rootFolder = $this->createMock(IRootFolder::class); | ||
| $this->attachmentService = new AttachmentService( | ||
| $this->rootFolder, | ||
| $this->createMock(IManager::class), | ||
| $this->createMock(IPreview::class), | ||
| $this->createMock(IMimeTypeDetector::class), | ||
| $this->createMock(IURLGenerator::class), | ||
| $this->createMock(IFilenameValidator::class), | ||
| $this->createMock(IFilesMetadataManager::class), | ||
| $this->createMock(ISession::class), | ||
| $this->createMock(FileService::class), | ||
| ); | ||
| $this->sessionService = $this->createMock(SessionService::class); | ||
| $this->setupManager = $this->createMock(ISetupManager::class); | ||
| $this->documentService->expects(self::never())->method('resetDocument'); | ||
| $this->documentService->expects(self::once()) | ||
| ->method('getAllPendingAttachmentCleanup') | ||
| ->willReturn((function (): \Generator { | ||
| yield $this->document; | ||
| })()); | ||
| $this->cleanup = new class($this->createMock(ITimeFactory::class), $this->sessionService, $this->documentService, $this->attachmentService, $this->createMock(LoggerInterface::class), $this->setupManager, ) extends Cleanup { | ||
| public function run($argument): void { | ||
| parent::run($argument); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public function testMarksCleanupOnlyAfterAttachmentsAreCleaned(): void { | ||
| $this->documentService->expects(self::once()) | ||
| ->method('hasUnsavedChanges')->with($this->document)->willReturn(false); | ||
| $this->setupManager->expects(self::once())->method('tearDown'); | ||
| $cleaned = false; | ||
| $this->rootFolder->expects(self::once()) | ||
| ->method('getFirstNodeById')->with(123) | ||
| ->willReturnCallback(function () use (&$cleaned): ?Node { | ||
| $cleaned = true; | ||
| return null; | ||
| }); | ||
| $this->documentService->expects(self::once()) | ||
| ->method('markAttachmentCleanup')->with($this->document) | ||
| ->willReturnCallback(function () use (&$cleaned): void { | ||
| self::assertTrue($cleaned); | ||
| }); | ||
| $this->sessionService->expects(self::once())->method('removeInactiveSessionsWithoutSteps'); | ||
| $this->sessionService->expects(self::once())->method('removeOldSessions'); | ||
| $this->sessionService->expects(self::once())->method('removeOrphanedSteps'); | ||
| $this->documentService->expects(self::once())->method('cleanupOldDocumentsFolders'); | ||
|
|
||
| $this->cleanup->run([]); | ||
| } | ||
|
|
||
| public function testMarksUnsavedChangesWithoutCleanup(): void { | ||
| $this->documentService->expects(self::once()) | ||
| ->method('hasUnsavedChanges')->with($this->document)->willReturn(true); | ||
| $this->setupManager->expects(self::never())->method('tearDown'); | ||
| $this->rootFolder->expects(self::never())->method('getFirstNodeById'); | ||
| $this->documentService->expects(self::once())->method('markAttachmentCleanup')->with($this->document); | ||
|
|
||
| $this->cleanup->run([]); | ||
| } | ||
|
|
||
| public function testFailedCleanupRemainsPending(): void { | ||
| $this->documentService->method('hasUnsavedChanges')->willReturn(false); | ||
| $this->rootFolder->expects(self::once()) | ||
| ->method('getFirstNodeById')->with(123) | ||
| ->willThrowException(new \RuntimeException('Cleanup failed')); | ||
| $this->documentService->expects(self::never())->method('markAttachmentCleanup'); | ||
| $this->expectException(\RuntimeException::class); | ||
| $this->expectExceptionMessage('Cleanup failed'); | ||
|
|
||
| $this->cleanup->run([]); | ||
| } | ||
| } |
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,79 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Text\Db; | ||
|
|
||
| use OCP\Server; | ||
|
|
||
| /** | ||
| * @group DB | ||
| */ | ||
| class DocumentMapperTest extends \Test\TestCase { | ||
| private DocumentMapper $documentMapper; | ||
| private SessionMapper $sessionMapper; | ||
| private Document $document; | ||
|
|
||
| public function setUp(): void { | ||
| parent::setUp(); | ||
| $this->documentMapper = Server::get(DocumentMapper::class); | ||
| $this->sessionMapper = Server::get(SessionMapper::class); | ||
| $this->document = $this->documentMapper->insert(Document::fromParams([ | ||
| 'contextType' => 'file', | ||
| 'contextId' => 900001, | ||
| 'lastSavedVersionTime' => 1000, | ||
| 'lastSavedVersionEtag' => 'saved-etag', | ||
| 'baseVersionEtag' => 'base-etag', | ||
| ])); | ||
| } | ||
|
|
||
| public function tearDown(): void { | ||
| $this->sessionMapper->deleteByDocumentId($this->document->getId()); | ||
| $this->documentMapper->delete($this->document); | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| private function pendingDocumentIds(): array { | ||
| return array_map( | ||
| static fn (Document $document): string => $document->getId(), | ||
| iterator_to_array($this->documentMapper->findAllPendingAttachmentCleanup()), | ||
| ); | ||
| } | ||
|
|
||
| public function testUnprocessedDocumentsArePending(): void { | ||
| self::assertSame(0, $this->documentMapper->find($this->document->getId())->getLastAttachmentCleanup()); | ||
| self::assertContains($this->document->getId(), $this->pendingDocumentIds()); | ||
| } | ||
|
|
||
| public function testDocumentsWithSessionsAreExcluded(): void { | ||
| $session = $this->sessionMapper->insert(Session::fromParams([ | ||
| 'documentId' => $this->document->getId(), | ||
| 'userId' => 'admin', | ||
| 'token' => uniqid(), | ||
| 'lastContact' => time(), | ||
| ])); | ||
| self::assertNotContains($this->document->getId(), $this->pendingDocumentIds()); | ||
| $this->sessionMapper->delete($session); | ||
| self::assertContains($this->document->getId(), $this->pendingDocumentIds()); | ||
| } | ||
|
|
||
| public function testCleanedDocumentsAreExcludedUntilSavedAgain(): void { | ||
| $this->document->setLastAttachmentCleanup(1000); | ||
| $this->documentMapper->update($this->document); | ||
| self::assertNotContains($this->document->getId(), $this->pendingDocumentIds()); | ||
|
|
||
| $this->document->setLastSavedVersionTime(1001); | ||
| $this->documentMapper->update($this->document); | ||
| self::assertContains($this->document->getId(), $this->pendingDocumentIds()); | ||
| } | ||
|
|
||
| public function testOlderSavedTimestampsAreExcluded(): void { | ||
| $this->document->setLastAttachmentCleanup(1001); | ||
| $this->documentMapper->update($this->document); | ||
|
|
||
| self::assertNotContains($this->document->getId(), $this->pendingDocumentIds()); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.