From 393bf9f5d1f2915b3fc82a8a4092976c6cc618fe Mon Sep 17 00:00:00 2001 From: peterjaap Date: Tue, 16 Apr 2019 14:48:17 +0200 Subject: [PATCH] Added LCP indexer to avoid race conditions, upped version to 0.3.0 because it might be breaking somewhat --- Model/Indexer/Prewarm.php | 93 +++++++++++++++++++ Observer/ProductSaveAfter.php | 18 ++-- .../Action/AfterUpdateAttributesPlugin.php | 15 ++- Plugin/StockItemSaveAround.php | 29 ++++++ etc/di.xml | 5 + etc/indexer.xml | 9 ++ etc/module.xml | 2 +- etc/mview.xml | 21 +++++ 8 files changed, 180 insertions(+), 12 deletions(-) create mode 100644 Model/Indexer/Prewarm.php create mode 100644 Plugin/StockItemSaveAround.php create mode 100644 etc/indexer.xml create mode 100644 etc/mview.xml diff --git a/Model/Indexer/Prewarm.php b/Model/Indexer/Prewarm.php new file mode 100644 index 0000000..7f8bc66 --- /dev/null +++ b/Model/Indexer/Prewarm.php @@ -0,0 +1,93 @@ +publisherNotifier = $publisherNotifier; + $this->storeManager = $storeManager; + $this->messageManager = $messageManager; + $this->output = $output; + $this->state = $state; + $this->productCollectionFactory = $productCollectionFactory; + } + + public function execute($productIds) + { + try { + $this->state->setAreaCode(Area::AREA_GLOBAL); + } catch (\Exception $e) { + + } + + if (!is_array($productIds)) { + /** @var ProductCollection $collection */ + $collection = $this->productCollectionFactory->create(); + $productIds = $collection->addAttributeToFilter('type_id', 'configurable')->getAllIds(); + } + + foreach ($productIds as $productId) { + $this->publisherNotifier->notify([$productId]); + } + } + + public function executeFull() + { + $this->execute(null); + } + + public function executeList(array $ids) + { + $this->execute($ids); + } + + public function executeRow($id) + { + $this->execute([$id]); + } +} diff --git a/Observer/ProductSaveAfter.php b/Observer/ProductSaveAfter.php index f1d4e6f..7c32695 100644 --- a/Observer/ProductSaveAfter.php +++ b/Observer/ProductSaveAfter.php @@ -2,17 +2,20 @@ namespace Elgentos\LargeConfigProducts\Observer; -use Elgentos\LargeConfigProducts\Model\PublisherNotifier; use Magento\Framework\Event\ObserverInterface; +use Magento\Framework\Indexer\IndexerRegistry; class ProductSaveAfter implements ObserverInterface { + private $indexer; + /** * ProductSaveAfter constructor. - * @param PublisherNotifier $publisherNotifier + * @param IndexerRegistry $indexerRegistry */ - public function __construct(PublisherNotifier $publisherNotifier) { - $this->publisherNotifier = $publisherNotifier; + public function __construct(IndexerRegistry $indexerRegistry) + { + $this->indexer = $indexerRegistry->get('elgentos_lcp_prewarm'); } /** @@ -20,7 +23,10 @@ public function __construct(PublisherNotifier $publisherNotifier) { */ public function execute( \Magento\Framework\Event\Observer $observer - ) { - $this->publisherNotifier->notify([$observer->getProduct()->getId()]); + ) + { + if (!$this->indexer->isScheduled()) { + $this->indexer->reindexRow($observer->getProduct()->getId()); + } } } \ No newline at end of file diff --git a/Plugin/Product/Action/AfterUpdateAttributesPlugin.php b/Plugin/Product/Action/AfterUpdateAttributesPlugin.php index d95c591..608eb78 100644 --- a/Plugin/Product/Action/AfterUpdateAttributesPlugin.php +++ b/Plugin/Product/Action/AfterUpdateAttributesPlugin.php @@ -2,17 +2,20 @@ namespace Elgentos\LargeConfigProducts\Plugin\Product\Action; -use Elgentos\LargeConfigProducts\Model\PublisherNotifier; use Magento\Catalog\Model\Product\Action as ProductAction; +use Magento\Framework\Indexer\IndexerRegistry; class AfterUpdateAttributesPlugin { + private $indexer; + /** * AfterUpdateAttributesPlugin constructor. - * @param PublisherNotifier $publisherNotifier + * @param IndexerRegistry $indexerRegistry */ - public function __construct(PublisherNotifier $publisherNotifier) { - $this->publisherNotifier = $publisherNotifier; + public function __construct(IndexerRegistry $indexerRegistry) + { + $this->indexer = $indexerRegistry->get('elgentos_lcp_prewarm'); } /** @@ -30,7 +33,9 @@ public function afterUpdateAttributes( $attrData, $storeId ) { - $this->publisherNotifier->notify($productIds); + if (!$this->indexer->isScheduled()) { + $this->indexer->reindexList($productIds); + } return $action; } diff --git a/Plugin/StockItemSaveAround.php b/Plugin/StockItemSaveAround.php new file mode 100644 index 0000000..028b551 --- /dev/null +++ b/Plugin/StockItemSaveAround.php @@ -0,0 +1,29 @@ +indexer = $indexerRegistry->get('elgentos_lcp_prewarm'); + } + + public function aroundSave( + \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItemModel, + \Closure $proceed, + \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem + ) { + $stockItemModel->addCommitCallback(function () use ($stockItem) { + if (!$this->indexer->isScheduled()) { + $this->indexer->reindexRow($stockItem->getProductId()); + } + }); + + return $proceed($stockItem); + } +} diff --git a/etc/di.xml b/etc/di.xml index dc2ff3e..0a9f591 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -15,6 +15,11 @@ + + + + + diff --git a/etc/indexer.xml b/etc/indexer.xml new file mode 100644 index 0000000..de0ef07 --- /dev/null +++ b/etc/indexer.xml @@ -0,0 +1,9 @@ + + + + Large Config Products prewarmer + + Adds products to the LCP message queue to be prewarmed. + + + diff --git a/etc/module.xml b/etc/module.xml index 90b5144..c80a869 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,7 +1,7 @@ - + diff --git a/etc/mview.xml b/etc/mview.xml new file mode 100644 index 0000000..08d6e68 --- /dev/null +++ b/etc/mview.xml @@ -0,0 +1,21 @@ + + + + + +
+
+
+
+
+
+
+
+
+
+
+
+
+ + + \ No newline at end of file