Skip to content

Commit 9e25cf3

Browse files
committed
perf(cleanup): Optimize cleanup job
Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
1 parent ada6b79 commit 9e25cf3

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

lib/Cron/Cleanup.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace OCA\Text\Cron;
1111

12+
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
1213
use OCA\Text\Service\AttachmentService;
1314
use OCA\Text\Service\DocumentService;
1415
use OCA\Text\Service\SessionService;
@@ -17,6 +18,8 @@
1718
use Psr\Log\LoggerInterface;
1819

1920
class Cleanup extends TimedJob {
21+
private const ABANDONED_UNSAVED_CHANGES_AGE = 30 * 24 * 60 * 60;
22+
2023
public function __construct(
2124
ITimeFactory $time,
2225
private readonly SessionService $sessionService,
@@ -34,7 +37,21 @@ public function __construct(
3437
protected function run($argument): void {
3538
$this->logger->debug('Run cleanup job for text documents');
3639
foreach ($this->documentService->getAllWithNoActiveSession() as $document) {
37-
$this->attachmentService->cleanupAttachments($document->getId());
40+
$documentId = $document->getId();
41+
try {
42+
$this->documentService->resetDocument($documentId);
43+
} catch (DocumentHasUnsavedChangesException) {
44+
$lastStepTime = $this->documentService->getLatestStepTimestamp($documentId);
45+
if ($lastStepTime === null || $lastStepTime >= $this->time->getTime() - self::ABANDONED_UNSAVED_CHANGES_AGE) {
46+
continue;
47+
}
48+
$this->documentService->resetDocument($documentId, true);
49+
$this->logger->warning('Force reset document with abandoned unsaved changes', [
50+
'documentId' => $documentId,
51+
'lastStepTime' => $lastStepTime,
52+
]);
53+
}
54+
$this->attachmentService->cleanupAttachments($documentId);
3855
}
3956

4057
$this->logger->debug('Run cleanup job for text sessions');

lib/Db/StepMapper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ public function getLatestVersion(int $documentId): ?int {
5656
return $data['id'];
5757
}
5858

59+
public function getLatestTimestamp(int $documentId): ?int {
60+
$qb = $this->db->getQueryBuilder();
61+
$result = $qb->select('timestamp')
62+
->from($this->getTableName())
63+
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
64+
->setMaxResults(1)
65+
->orderBy('id', 'DESC')
66+
->executeQuery();
67+
68+
$data = $result->fetch();
69+
$result->closeCursor();
70+
if ($data === false) {
71+
return null;
72+
}
73+
74+
return (int)$data['timestamp'];
75+
}
76+
5977
public function getBeforeVersion(int $documentId, int $version, int $offset): int {
6078
$qb = $this->db->getQueryBuilder();
6179
$result = $qb->select('id')

lib/Service/DocumentService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ public function autosave(Document $document, File $file, int $version, string $a
428428
return $document;
429429
}
430430

431+
public function getLatestStepTimestamp(int $documentId): ?int {
432+
return $this->stepMapper->getLatestTimestamp($documentId);
433+
}
434+
431435
/**
432436
* @throws DocumentHasUnsavedChangesException
433437
* @throws Exception

0 commit comments

Comments
 (0)