Don't load all calendar objects into memory

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-08-20 07:56:25 +02:00
parent 6d21e0f6ff
commit 553cda2a6d
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
1 changed files with 15 additions and 14 deletions

View File

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