]> source.dussan.org Git - nextcloud-server.git/commitdiff
Don't load all calendar objects into memory 22311/head
authorJoas Schilling <coding@schilljs.com>
Thu, 20 Aug 2020 05:56:25 +0000 (07:56 +0200)
committerJoas Schilling <coding@schilljs.com>
Thu, 20 Aug 2020 05:56:25 +0000 (07:56 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
apps/dav/lib/Migration/BuildCalendarSearchIndexBackgroundJob.php

index ada3c47017162c66187f25b70e8d45972d7ad4b3..5f6ec2e77f2e4c57f57bc4a1e7f4de6a2802d8b1 100644 (file)
@@ -69,12 +69,18 @@ class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
        }
 
        public function run($arguments) {
-               $offset = $arguments['offset'];
-               $stopAt = $arguments['stopAt'];
+               $offset = (int) $arguments['offset'];
+               $stopAt = (int) $arguments['stopAt'];
 
                $this->logger->info('Building calendar index (' . $offset .'/' . $stopAt . ')');
 
-               $offset = $this->buildIndex($offset, $stopAt);
+               $startTime = $this->timeFactory->getTime();
+               while (($this->timeFactory->getTime() - $startTime) < 15) {
+                       $offset = $this->buildIndex($offset, $stopAt);
+                       if ($offset >= $stopAt) {
+                               break;
+                       }
+               }
 
                if ($offset >= $stopAt) {
                        $this->logger->info('Building calendar index done');
@@ -92,18 +98,17 @@ class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
         * @param int $stopAt
         * @return int
         */
-       private function buildIndex($offset, $stopAt) {
-               $startTime = $this->timeFactory->getTime();
-
+       private function buildIndex(int $offset, int $stopAt): int {
                $query = $this->db->getQueryBuilder();
                $query->select(['id', 'calendarid', 'uri', 'calendardata'])
                        ->from('calendarobjects')
                        ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
                        ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
-                       ->orderBy('id', 'ASC');
+                       ->orderBy('id', 'ASC')
+                       ->setMaxResults(500);
 
-               $stmt = $query->execute();
-               while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+               $result = $query->execute();
+               while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
                        $offset = $row['id'];
 
                        $calendarData = $row['calendardata'];
@@ -112,12 +117,8 @@ class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
                        }
 
                        $this->calDavBackend->updateProperties($row['calendarid'], $row['uri'], $calendarData);
-
-                       if (($this->timeFactory->getTime() - $startTime) > 15) {
-                               return $offset;
-                       }
                }
 
-               return $stopAt;
+               return $offset;
        }
 }