Skip to content

Commit 4bd739f

Browse files
authored
Merge pull request #8444 from nextcloud/backport/8421/stable32
[stable32] Optimize cleanup job
2 parents bf1de15 + b7e5793 commit 4bd739f

7 files changed

Lines changed: 94 additions & 8 deletions

File tree

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- **💾 Open format:** Files are saved as [Markdown](https://en.wikipedia.org/wiki/Markdown), so you can edit them from any other text app too.
1616
- **✊ Strong foundation:** We use [🐈 tiptap](https://tiptap.scrumpy.io) which is based on [🦉 ProseMirror](https://prosemirror.net) – huge thanks to them!
1717
]]></description>
18-
<version>6.0.1</version>
18+
<version>6.0.2</version>
1919
<licence>agpl</licence>
2020
<author mail="jus@bitgrid.net">Julius Härtl</author>
2121
<namespace>Text</namespace>

composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
'OCA\\Text\\Migration\\Version030901Date20231114150437' => $baseDir . '/../lib/Migration/Version030901Date20231114150437.php',
6464
'OCA\\Text\\Migration\\Version040100Date20240611165300' => $baseDir . '/../lib/Migration/Version040100Date20240611165300.php',
6565
'OCA\\Text\\Migration\\Version070000Date20250925110024' => $baseDir . '/../lib/Migration/Version070000Date20250925110024.php',
66+
'OCA\\Text\\Migration\\Version080000Date20260331132113' => $baseDir . '/../lib/Migration/Version080000Date20260331132113.php',
6667
'OCA\\Text\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
6768
'OCA\\Text\\Service\\ApiService' => $baseDir . '/../lib/Service/ApiService.php',
6869
'OCA\\Text\\Service\\AttachmentService' => $baseDir . '/../lib/Service/AttachmentService.php',

composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class ComposerStaticInitText
7878
'OCA\\Text\\Migration\\Version030901Date20231114150437' => __DIR__ . '/..' . '/../lib/Migration/Version030901Date20231114150437.php',
7979
'OCA\\Text\\Migration\\Version040100Date20240611165300' => __DIR__ . '/..' . '/../lib/Migration/Version040100Date20240611165300.php',
8080
'OCA\\Text\\Migration\\Version070000Date20250925110024' => __DIR__ . '/..' . '/../lib/Migration/Version070000Date20250925110024.php',
81+
'OCA\\Text\\Migration\\Version080000Date20260331132113' => __DIR__ . '/..' . '/../lib/Migration/Version080000Date20260331132113.php',
8182
'OCA\\Text\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
8283
'OCA\\Text\\Service\\ApiService' => __DIR__ . '/..' . '/../lib/Service/ApiService.php',
8384
'OCA\\Text\\Service\\AttachmentService' => __DIR__ . '/..' . '/../lib/Service/AttachmentService.php',

lib/Cron/Cleanup.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ public function __construct(
3636
*/
3737
protected function run($argument): void {
3838
$this->logger->debug('Run cleanup job for text documents');
39-
foreach ($this->documentService->getAll() as $document) {
40-
if ($this->sessionService->countAllSessions($document->getId()) > 0) {
41-
// Do not reset if there are any sessions left
42-
// Inactive sessions will get removed further down and will trigger a reset next time
43-
continue;
44-
}
45-
39+
foreach ($this->documentService->getAllWithNoActiveSession() as $document) {
4640
try {
4741
$this->documentService->resetDocument($document->getId());
4842
} catch (DocumentHasUnsavedChangesException) {

lib/Db/DocumentMapper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ public function findAll(): Generator {
5454
}
5555
}
5656

57+
public function findAllWithNoActiveSessions(): Generator {
58+
$qb = $this->db->getQueryBuilder();
59+
$result = $qb->select('d.*')
60+
->from($this->getTableName(), 'd')
61+
->leftJoin('d', 'text_sessions', 's', $qb->expr()->eq('s.document_id', 'd.id'))
62+
->where($qb->expr()->isNull('s.id'))
63+
->executeQuery();
64+
try {
65+
while ($row = $result->fetch()) {
66+
yield $this->mapRowToEntity($row);
67+
}
68+
} finally {
69+
$result->closeCursor();
70+
}
71+
}
72+
5773
public function countAll(): int {
5874
$qb = $this->db->getQueryBuilder();
5975
$qb->select($qb->func()->count('id'))
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Text\Migration;
11+
12+
use Closure;
13+
use OCP\DB\ISchemaWrapper;
14+
use OCP\IDBConnection;
15+
use OCP\Migration\Attributes\AddIndex;
16+
use OCP\Migration\Attributes\IndexType;
17+
use OCP\Migration\IOutput;
18+
use OCP\Migration\SimpleMigrationStep;
19+
use Override;
20+
21+
#[AddIndex(table: 'text_steps', type: IndexType::INDEX, description: 'Optimize cleanup job')]
22+
class Version080000Date20260331132113 extends SimpleMigrationStep {
23+
private bool $addIndexLater = false;
24+
25+
public function __construct(
26+
private readonly IDBConnection $connection,
27+
) {
28+
}
29+
30+
#[Override]
31+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
32+
/** @var ISchemaWrapper $schema */
33+
$schema = $schemaClosure();
34+
35+
$table = $schema->getTable('text_steps');
36+
if (!$table->hasIndex('text_steps_doc_id_id_index')) {
37+
if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MARIADB
38+
|| $this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL) {
39+
$table->addIndex(['document_id', 'id'], 'text_steps_doc_id_id_index');
40+
} else {
41+
$this->addIndexLater = true;
42+
}
43+
return $schema;
44+
}
45+
46+
return null;
47+
}
48+
49+
#[Override]
50+
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): ?ISchemaWrapper {
51+
/** @var ISchemaWrapper $schema */
52+
$schema = $schemaClosure();
53+
if ($this->addIndexLater) {
54+
// We need to add the index with a DESC manually
55+
// See https://github.com/doctrine/orm/issues/8128
56+
if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) {
57+
$stm = $this->connection->prepare('CREATE INDEX CONCURRENTLY text_steps_doc_id_id_index ON *PREFIX*text_steps (document_id, id DESC);');
58+
} elseif ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) {
59+
$stm = $this->connection->prepare('CREATE INDEX text_steps_doc_id_id_index ON *PREFIX*text_steps (document_id, id DESC) ONLINE;');
60+
} elseif ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE) {
61+
$stm = $this->connection->prepare('CREATE INDEX text_steps_doc_id_id_index ON *PREFIX*text_steps (document_id, id DESC);');
62+
} else {
63+
throw new \RuntimeException('Unsupported platform');
64+
}
65+
$stm->execute();
66+
return $schema;
67+
}
68+
return null;
69+
}
70+
}

lib/Service/DocumentService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@ public function getAll(): \Generator {
481481
return $this->documentMapper->findAll();
482482
}
483483

484+
public function getAllWithNoActiveSession(): \Generator {
485+
return $this->documentMapper->findAllWithNoActiveSessions();
486+
}
487+
484488
/**
485489
* @throws NotPermittedException
486490
* @throws NotFoundException

0 commit comments

Comments
 (0)