@@ -112,7 +112,7 @@ public function getPreviewsForMimeTypes(array $mimeTypes): \Generator {
112112 public function deleteAll (): void {
113113 $ lastId = 0 ;
114114 while (true ) {
115- $ previews = $ this ->previewMapper ->getPreviews ($ lastId , 1000 );
115+ $ previews = $ this ->previewMapper ->getPreviews ($ lastId , PreviewMapper:: MAX_CHUNK_SIZE );
116116 $ i = 0 ;
117117
118118 // FIXME: Should we use transaction here? Du to the I/O created when
@@ -124,7 +124,7 @@ public function deleteAll(): void {
124124 $ lastId = $ preview ->getId ();
125125 }
126126
127- if ($ i !== 1000 ) {
127+ if ($ i !== PreviewMapper:: MAX_CHUNK_SIZE ) {
128128 break ;
129129 }
130130 }
@@ -137,4 +137,38 @@ public function deleteAll(): void {
137137 public function getAvailablePreviews (array $ fileIds ): array {
138138 return $ this ->previewMapper ->getAvailablePreviews ($ fileIds );
139139 }
140+
141+ public function deleteExpiredPreviews (int $ maxAgeDays ): void {
142+
143+ $ lastId = 0 ;
144+ $ startTime = time ();
145+ while (true ) {
146+ try {
147+ $ this ->connection ->beginTransaction ();
148+
149+ $ previews = $ this ->previewMapper ->getPreviews ($ lastId , PreviewMapper::MAX_CHUNK_SIZE , $ maxAgeDays );
150+ $ i = 0 ;
151+ foreach ($ previews as $ preview ) {
152+ $ this ->deletePreview ($ preview );
153+ $ i ++;
154+ $ lastId = $ preview ->getId ();
155+ }
156+
157+ $ this ->connection ->commit ();
158+
159+ if ($ i !== PreviewMapper::MAX_CHUNK_SIZE ) {
160+ break ;
161+ }
162+ } catch (Exception $ e ) {
163+ $ this ->connection ->commit ();
164+
165+ throw $ e ;
166+ }
167+
168+ // Stop if execution time is more than one hour.
169+ if (time () - $ startTime > 3600 ) {
170+ return ;
171+ }
172+ }
173+ }
140174}
0 commit comments