Skip to content

Commit

Permalink
Added LCP indexer to avoid race conditions, upped version to 0.3.0 be…
Browse files Browse the repository at this point in the history
…cause it might be breaking somewhat
  • Loading branch information
peterjaap committed Apr 16, 2019
1 parent 1f30544 commit 393bf9f
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 12 deletions.
93 changes: 93 additions & 0 deletions Model/Indexer/Prewarm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Elgentos\LargeConfigProducts\Model\Indexer;

use Elgentos\LargeConfigProducts\Model\PublisherNotifier;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Indexer\ActionInterface as IndexerActionInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Mview\ActionInterface as MviewActionInterface;
use Magento\Store\Model\StoreManagerInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;

class Prewarm implements IndexerActionInterface, MviewActionInterface
{
/**
* @var ProductCollectionFactory
*/
public $productCollectionFactory;
private $storeManager;
private $messageManager;
private $output;
/**
* @var State
*/
private $state;

/**
* @var PublisherNotifier
*/
protected $publisherNotifier;

/**
* Product constructor.
* @param StoreManagerInterface $storeManager
* @param ManagerInterface $messageManager
* @param ConsoleOutput $output
* @param State $state
* @param PublisherNotifier $publisherNotifier
* @param ProductCollectionFactory $productCollectionFactory
*/
public function __construct(
StoreManagerInterface $storeManager,
ManagerInterface $messageManager,
ConsoleOutput $output,
State $state,
PublisherNotifier $publisherNotifier,
ProductCollectionFactory $productCollectionFactory
) {
$this->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]);
}
}
18 changes: 12 additions & 6 deletions Observer/ProductSaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@

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');
}

/**
* {@inheritdoc}
*/
public function execute(
\Magento\Framework\Event\Observer $observer
) {
$this->publisherNotifier->notify([$observer->getProduct()->getId()]);
)
{
if (!$this->indexer->isScheduled()) {
$this->indexer->reindexRow($observer->getProduct()->getId());
}
}
}
15 changes: 10 additions & 5 deletions Plugin/Product/Action/AfterUpdateAttributesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -30,7 +33,9 @@ public function afterUpdateAttributes(
$attrData,
$storeId
) {
$this->publisherNotifier->notify($productIds);
if (!$this->indexer->isScheduled()) {
$this->indexer->reindexList($productIds);
}

return $action;
}
Expand Down
29 changes: 29 additions & 0 deletions Plugin/StockItemSaveAround.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Elgentos\LargeConfigProducts\Plugin;

use Magento\Framework\Indexer\IndexerRegistry;

class StockItemSaveAround
{
private $indexer;

public function __construct(IndexerRegistry $indexerRegistry)
{
$this->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);
}
}
5 changes: 5 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
</arguments>
</type>

<!-- Not sure if this one is even needed, comment it out for now -->
<!-- <type name="Magento\CatalogInventory\Model\ResourceModel\Stock\Item">-->
<!-- <plugin name="elgentosLCPStockItemPlugin" type="Elgentos\LargeConfigProducts\Plugin\StockItemSaveAround"/>-->
<!-- </type>-->

<type name="Magento\Catalog\Model\Product\Action">
<plugin sortOrder="1" name="elgentosLargeConfigProductsAfterUpdateAttributes"
type="Elgentos\LargeConfigProducts\Plugin\Product\Action\AfterUpdateAttributesPlugin" />
Expand Down
9 changes: 9 additions & 0 deletions etc/indexer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
<indexer id="elgentos_lcp_prewarm" view_id="elgentos_lcp_prewarm" class="Elgentos\LargeConfigProducts\Model\Indexer\Prewarm">
<title translate="true">Large Config Products prewarmer</title>
<description translate="true">
Adds products to the LCP message queue to be prewarmed.
</description>
</indexer>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Elgentos_LargeConfigProducts" setup_version="0.1.11">
<module name="Elgentos_LargeConfigProducts" setup_version="0.3.0">
<sequence>
<module name="Magento_ConfigurableProduct" />
<module name="Magento_Framework" />
Expand Down
21 changes: 21 additions & 0 deletions etc/mview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
<view id="elgentos_lcp_prewarm" class="Elgentos\LargeConfigProducts\Model\Indexer\Prewarm" group="indexer">
<subscriptions>
<table name="catalog_product_entity" entity_column="entity_id" />
<table name="catalog_product_entity_datetime" entity_column="entity_id" />
<table name="catalog_product_entity_decimal" entity_column="entity_id" />
<table name="catalog_product_entity_gallery" entity_column="entity_id" />
<table name="catalog_product_entity_int" entity_column="entity_id" />
<table name="catalog_product_entity_media_gallery" entity_column="value_id" />
<table name="catalog_product_entity_media_gallery_value" entity_column="entity_id" />
<table name="catalog_product_entity_text" entity_column="entity_id" />
<table name="catalog_product_entity_tier_price" entity_column="entity_id" />
<table name="catalog_product_entity_varchar" entity_column="entity_id" />
<table name="catalog_product_website" entity_column="product_id" />
<table name="catalog_category_product" entity_column="product_id" />
<table name="cataloginventory_stock_item" entity_column="product_id" />
<table name="catalog_product_index_price" entity_column="entity_id" />
</subscriptions>
</view>
</config>

0 comments on commit 393bf9f

Please sign in to comment.