|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +declare(strict_types=1); |
| 8 | + |
7 | 9 | namespace Magento\Framework\Mview;
|
8 | 10 |
|
9 | 11 | use InvalidArgumentException;
|
@@ -254,39 +256,60 @@ public function update()
|
254 | 256 | try {
|
255 | 257 | $this->getState()->setStatus(View\StateInterface::STATUS_WORKING)->save();
|
256 | 258 |
|
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); |
274 | 260 |
|
275 | 261 | $this->getState()->loadByView($this->getId());
|
276 | 262 | $statusToRestore = $this->getState()->getStatus() === View\StateInterface::STATUS_SUSPENDED
|
277 | 263 | ? View\StateInterface::STATUS_SUSPENDED
|
278 | 264 | : View\StateInterface::STATUS_IDLE;
|
279 | 265 | $this->getState()->setVersionId($currentVersionId)->setStatus($statusToRestore)->save();
|
280 |
| - } catch (Exception $exception) { |
| 266 | + } catch (\Throwable $exception) { |
281 | 267 | $this->getState()->loadByView($this->getId());
|
282 | 268 | $statusToRestore = $this->getState()->getStatus() === View\StateInterface::STATUS_SUSPENDED
|
283 | 269 | ? View\StateInterface::STATUS_SUSPENDED
|
284 | 270 | : View\StateInterface::STATUS_IDLE;
|
285 | 271 | $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 | + } |
286 | 279 | throw $exception;
|
287 | 280 | }
|
288 | 281 | }
|
289 | 282 |
|
| 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 | + |
290 | 313 | /**
|
291 | 314 | * Suspend view updates and set version ID to changelog's end
|
292 | 315 | *
|
|
0 commit comments