Skip to content

Commit e7749a8

Browse files
authored
ENGCOM-5210: Catch throwables in mview updating #23125
2 parents e146a7e + 5d1e830 commit e7749a8

File tree

1 file changed

+41
-18
lines changed
  • lib/internal/Magento/Framework/Mview

1 file changed

+41
-18
lines changed

lib/internal/Magento/Framework/Mview/View.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\Framework\Mview;
810

911
use InvalidArgumentException;
@@ -254,39 +256,60 @@ public function update()
254256
try {
255257
$this->getState()->setStatus(View\StateInterface::STATUS_WORKING)->save();
256258

257-
$versionBatchSize = self::$maxVersionQueryBatch;
258-
$batchSize = isset($this->changelogBatchSize[$this->getChangelog()->getViewId()])
259-
? $this->changelogBatchSize[$this->getChangelog()->getViewId()]
260-
: self::DEFAULT_BATCH_SIZE;
261-
262-
for ($vsFrom = $lastVersionId; $vsFrom < $currentVersionId; $vsFrom += $versionBatchSize) {
263-
// Don't go past the current version for atomicy.
264-
$versionTo = min($currentVersionId, $vsFrom + $versionBatchSize);
265-
$ids = $this->getChangelog()->getList($vsFrom, $versionTo);
266-
267-
// We run the actual indexer in batches.
268-
// Chunked AFTER loading to avoid duplicates in separate chunks.
269-
$chunks = array_chunk($ids, $batchSize);
270-
foreach ($chunks as $ids) {
271-
$action->execute($ids);
272-
}
273-
}
259+
$this->executeAction($action, $lastVersionId, $currentVersionId);
274260

275261
$this->getState()->loadByView($this->getId());
276262
$statusToRestore = $this->getState()->getStatus() === View\StateInterface::STATUS_SUSPENDED
277263
? View\StateInterface::STATUS_SUSPENDED
278264
: View\StateInterface::STATUS_IDLE;
279265
$this->getState()->setVersionId($currentVersionId)->setStatus($statusToRestore)->save();
280-
} catch (Exception $exception) {
266+
} catch (\Throwable $exception) {
281267
$this->getState()->loadByView($this->getId());
282268
$statusToRestore = $this->getState()->getStatus() === View\StateInterface::STATUS_SUSPENDED
283269
? View\StateInterface::STATUS_SUSPENDED
284270
: View\StateInterface::STATUS_IDLE;
285271
$this->getState()->setStatus($statusToRestore)->save();
272+
if (!$exception instanceof \Exception) {
273+
$exception = new \RuntimeException(
274+
'Error when updating an mview',
275+
0,
276+
$exception
277+
);
278+
}
286279
throw $exception;
287280
}
288281
}
289282

283+
/**
284+
* Execute action from last version to current version, by batches
285+
*
286+
* @param ActionInterface $action
287+
* @param int $lastVersionId
288+
* @param int $currentVersionId
289+
* @return void
290+
* @throws \Exception
291+
*/
292+
private function executeAction(ActionInterface $action, int $lastVersionId, int $currentVersionId)
293+
{
294+
$versionBatchSize = self::$maxVersionQueryBatch;
295+
$batchSize = isset($this->changelogBatchSize[$this->getChangelog()->getViewId()])
296+
? $this->changelogBatchSize[$this->getChangelog()->getViewId()]
297+
: self::DEFAULT_BATCH_SIZE;
298+
299+
for ($vsFrom = $lastVersionId; $vsFrom < $currentVersionId; $vsFrom += $versionBatchSize) {
300+
// Don't go past the current version for atomicity.
301+
$versionTo = min($currentVersionId, $vsFrom + $versionBatchSize);
302+
$ids = $this->getChangelog()->getList($vsFrom, $versionTo);
303+
304+
// We run the actual indexer in batches.
305+
// Chunked AFTER loading to avoid duplicates in separate chunks.
306+
$chunks = array_chunk($ids, $batchSize);
307+
foreach ($chunks as $ids) {
308+
$action->execute($ids);
309+
}
310+
}
311+
}
312+
290313
/**
291314
* Suspend view updates and set version ID to changelog's end
292315
*

0 commit comments

Comments
 (0)