Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/Cron/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\Text\Cron;

use OCA\Text\Exception\DocumentHasUnsavedChangesException;
use OCA\Text\Service\AttachmentService;
use OCA\Text\Service\DocumentService;
use OCA\Text\Service\SessionService;
Expand All @@ -17,6 +18,8 @@
use Psr\Log\LoggerInterface;

class Cleanup extends TimedJob {
private const ABANDONED_UNSAVED_CHANGES_AGE = 30 * 24 * 60 * 60;

Check failure on line 21 in lib/Cron/Cleanup.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MissingClassConstType

lib/Cron/Cleanup.php:21:16: MissingClassConstType: Class constant "OCA\Text\Cron\Cleanup::ABANDONED_UNSAVED_CHANGES_AGE" should have a declared type. (see https://psalm.dev/359)

public function __construct(
ITimeFactory $time,
private readonly SessionService $sessionService,
Expand All @@ -34,7 +37,21 @@
protected function run($argument): void {
$this->logger->debug('Run cleanup job for text documents');
foreach ($this->documentService->getAllWithNoActiveSession() as $document) {
$this->attachmentService->cleanupAttachments($document->getId());
$documentId = $document->getId();
try {
$this->documentService->resetDocument($documentId);
Comment on lines -37 to +42

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to investigate why resetDocument was removed before

} catch (DocumentHasUnsavedChangesException) {
$lastStepTime = $this->documentService->getLatestStepTimestamp($documentId);
if ($lastStepTime === null || $lastStepTime >= $this->time->getTime() - self::ABANDONED_UNSAVED_CHANGES_AGE) {
continue;
}
$this->documentService->resetDocument($documentId, true);
$this->logger->warning('Force reset document with abandoned unsaved changes', [
'documentId' => $documentId,
'lastStepTime' => $lastStepTime,
]);
}
$this->attachmentService->cleanupAttachments($documentId);
}

$this->logger->debug('Run cleanup job for text sessions');
Expand Down
18 changes: 18 additions & 0 deletions lib/Db/StepMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ public function getLatestVersion(int $documentId): ?int {
return $data['id'];
}

public function getLatestTimestamp(int $documentId): ?int {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('timestamp')
->from($this->getTableName())
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
->setMaxResults(1)
->orderBy('id', 'DESC')
->executeQuery();

$data = $result->fetch();
$result->closeCursor();
if ($data === false) {
return null;
}

return (int)$data['timestamp'];
}

public function getBeforeVersion(int $documentId, int $version, int $offset): int {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('id')
Expand Down
4 changes: 4 additions & 0 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ public function autosave(Document $document, File $file, int $version, string $a
return $document;
}

public function getLatestStepTimestamp(int $documentId): ?int {
return $this->stepMapper->getLatestTimestamp($documentId);
}

/**
* @throws DocumentHasUnsavedChangesException
* @throws Exception
Expand Down
Loading