99
1010namespace OCA \Text \Cron ;
1111
12+ use OCA \Text \Exception \DocumentHasUnsavedChangesException ;
1213use OCA \Text \Service \AttachmentService ;
1314use OCA \Text \Service \DocumentService ;
1415use OCA \Text \Service \SessionService ;
1718use Psr \Log \LoggerInterface ;
1819
1920class 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 ' );
0 commit comments