Skip to content
Closed
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
57 changes: 39 additions & 18 deletions lib/internal/Magento/Framework/Mview/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,42 +274,63 @@ public function update()
$lastVersionId = (int) $this->getState()->getVersionId();
$action = $this->actionFactory->get($this->getActionClass());

$this->executeAction($action, $lastVersionId, $currentVersionId);

try {
$this->getState()->setStatus(View\StateInterface::STATUS_WORKING)->save();

$versionBatchSize = self::$maxVersionQueryBatch;
$batchSize = isset($this->changelogBatchSize[$this->getChangelog()->getViewId()])
? $this->changelogBatchSize[$this->getChangelog()->getViewId()]
: self::DEFAULT_BATCH_SIZE;

for ($versionFrom = $lastVersionId; $versionFrom < $currentVersionId; $versionFrom += $versionBatchSize) {
// Don't go past the current version for atomicy.
$versionTo = min($currentVersionId, $versionFrom + $versionBatchSize);
$ids = array_map('intval', $this->getChangelog()->getList($versionFrom, $versionTo));

// We run the actual indexer in batches. Chunked AFTER loading to avoid duplicates in separate chunks.
$chunks = array_chunk($ids, $batchSize);
foreach ($chunks as $ids) {
$action->execute($ids);
}
}

$this->getState()->loadByView($this->getId());
$statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
? View\StateInterface::STATUS_SUSPENDED
: View\StateInterface::STATUS_IDLE;
$this->getState()->setVersionId($currentVersionId)->setStatus($statusToRestore)->save();
} catch (\Exception $exception) {
} catch (\Throwable $exception) {
$this->getState()->loadByView($this->getId());
$statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
? View\StateInterface::STATUS_SUSPENDED
: View\StateInterface::STATUS_IDLE;
$this->getState()->setStatus($statusToRestore)->save();
if (!$exception instanceof \Exception) {
$exception = new \RuntimeException(
'Error when updating an mview',
0,
$exception
);
}
throw $exception;
}
}
}

/**
* Execute action from last version to current version, by batches
*
* @param ActionInterface $action
* @param int $lastVersionId
* @param int $currentVersionId
* @return void
* @throws \Exception
*/
private function executeAction(ActionInterface $action, int $lastVersionId, int $currentVersionId)
{
$versionBatchSize = self::$maxVersionQueryBatch;
$batchSize = isset($this->changelogBatchSize[$this->getChangelog()->getViewId()])
? $this->changelogBatchSize[$this->getChangelog()->getViewId()]
: self::DEFAULT_BATCH_SIZE;

for ($versionFrom = $lastVersionId; $versionFrom < $currentVersionId; $versionFrom += $versionBatchSize) {
// Don't go past the current version for atomicy.
$versionTo = min($currentVersionId, $versionFrom + $versionBatchSize);
$ids = array_map('intval', $this->getChangelog()->getList($versionFrom, $versionTo));

// We run the actual indexer in batches. Chunked AFTER loading to avoid duplicates in separate chunks.
$chunks = array_chunk($ids, $batchSize);
foreach ($chunks as $ids) {
$action->execute($ids);
}
}
}

/**
* Suspend view updates and set version ID to changelog's end
*
Expand Down