Skip to content

Commit

Permalink
Rely on indexer when syncing inventory
Browse files Browse the repository at this point in the history
This commit
- subscribes our indexer to changes on various product-related tables
- hooks existing single update logic to indexer interface
- removes observer
- removes the mess of txt file driving the sync logic
- changes our cron job to run once per day, at 4am and invalidating the
index
- makes this time configurable through user interface
- hooks the reindex now button to index invalidation
  • Loading branch information
crutch committed Mar 4, 2021
1 parent d41e5d7 commit 374ffbe
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 161 deletions.
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Request/Reindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
*/
public function execute()
{
$this->_luigisBoxHelper->setIndexInvalidationTimestamp(true);
$this->_luigisBoxHelper->invalidateIndex();

$result = $this->_jsonFactory->create();

Expand Down
15 changes: 9 additions & 6 deletions Cron/Checker.php → Cron/IndexInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,39 @@
namespace LuigisBox\SearchSuite\Cron;

use LuigisBox\SearchSuite\Helper\Helper;
use Psr\Log\LoggerInterface;

class Checker
class IndexInvalidator
{
protected $_indexerFactory;
protected $_helper;
protected $_logger;

public function __construct(
\Magento\Indexer\Model\IndexerFactory $indexerFactory,
LoggerInterface $logger,
Helper $helper
) {
$this->_indexerFactory = $indexerFactory;
$this->_helper = $helper;
$this->_logger = $logger;
}

/**
* Check if index needs to be invalidated
* Invalidates Luigi's Box indexer
*
* @return void
*/

public function execute()
{
if (!$this->_helper->isConfigured() || $this->_helper->isIndexValid()) {
$this->_logger->info("Luigi's Box index invalidator triggered");
if (!$this->_helper->isConfigured()) {
return;
}

$indexer = $this->_indexerFactory->create();
$indexer->load('luigisbox_index_product');
$indexer->load('luigisbox_reindex');
$indexer->invalidate();

$this->_helper->setIndexInvalidationTimestamp();
}
}
103 changes: 9 additions & 94 deletions Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class Helper extends AbstractHelper

protected $_fixImageLinksOmittingPubFolder; // @see https://github.com/magento/magento2/issues/9111

protected $_indexerFactory;

/**
* @param Context $context
* @param ScopeConfigInterface $scopeConfig
Expand All @@ -86,7 +88,8 @@ public function __construct(
\Magento\Framework\Filesystem $filesystem,
\Magento\Store\Model\App\Emulation $emulation,
\Magento\Framework\App\DeploymentConfig $deploymentConfig,
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
\Magento\Indexer\Model\IndexerFactory $indexerFactory
) {
$this->_scopeConfig = $scopeConfig;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
Expand All @@ -101,6 +104,7 @@ public function __construct(
$this->_emulation = $emulation;
$this->_deploymentConfig = $deploymentConfig;
$this->_stockRegistry = $stockRegistry;
$this->_indexerFactory = $indexerFactory;
parent::__construct($context);
}

Expand Down Expand Up @@ -842,100 +846,11 @@ public function urlExists($url)
return ($response->getStatusCode() === 200);
}

public function getLogfile()
{
$tmp = $this->_filesystem->getDirectoryRead(DirectoryList::TMP);

if ($tmp->isExist(self::LOGFILE)) {
try {
$log = json_decode($tmp->readFile(self::LOGFILE));
} catch (\Magento\Framework\Exception\FileSystemException $ex) {
$this->_logger->debug('Luigi\'s Box: file update.txt not readable');
return false;
}

return $log;
}

return false;
}

public function setLogfile($log)
{
try {
$tmp = $this->_filesystem->getDirectoryWrite(DirectoryList::TMP);
$tmp->writeFile(self::LOGFILE, json_encode($log));
} catch (\Magento\Framework\Exception\FileSystemException $ex) {
$this->_logger->debug('Luigi\'s Box: log file is not writable');
}
}

public function isIndexValid()
public function invalidateIndex()
{
$now = date('U');

$last = 0;

if ($log = $this->getLogfile()) {
$last = $log->invalidated;
}

$diff = $now - $last;

return ($diff < self::INTERVAL);
}

public function isIndexFinished()
{
if ($log = $this->getLogfile()) {
return $log->finished;
}

return true;
}

public function isIndexRunning()
{
if ($log = $this->getLogfile()) {
return $log->running;
}

return false;
}

public function markIndexFinished()
{
if ($log = $this->getLogfile()) {
$log->finished = true;
$log->running = false;

$this->setLogfile($log);
}
}

public function markIndexRunning()
{
if ($log = $this->getLogfile()) {
$log->finished = false;
$log->running = true;

$this->setLogfile($log);
}
}

public function setIndexInvalidationTimestamp($reindexImmediately = false)
{
$now = date('U');

if ($reindexImmediately) {
$now = 0;
}

$log = ['invalidated' => $now, 'running' => false, 'finished' => false];

$this->setLogfile($log);

$this->_logger->debug('Luigi\'s Box: index invalidated');
$indexer = $this->_indexerFactory->create();
$indexer->load('luigisbox_reindex');
$indexer->invalidate();
}

public function sanitizeUrl($url, $store)
Expand Down
36 changes: 27 additions & 9 deletions Model/Indexer/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,51 @@ public function __construct(

public function execute($ids)
{
}
$this->_logger->debug('Luigi\'s Box Indexer triggered');

public function executeFull()
{
if (!$this->_helper->isConfigured()) {
$this->_logger->debug('Luigi\'s Box not configured, can not index products');
return;
}

// Run reindex if index timestamp is invalidated and
if (!$this->_helper->isIndexValid() || $this->_helper->isIndexRunning() || $this->_helper->isIndexFinished()) {
$this->_logger->debug('Luigi\'s Box reindex preventing run');
return;
foreach ($ids as $id) {
$this->_helper->singleContentUpdate($id);
}
$this->_logger->debug('Luigi\'s Box Indexer finished');
}

$this->_helper->markIndexRunning();
public function executeFull()
{
if (!$this->_helper->isConfigured()) {
$this->_logger->debug('Luigi\'s Box not configured, can not index products');
return;
}

$this->_helper->allContentUpdate();

$this->_helper->markIndexFinished();
}

public function executeList(array $ids)
{
$this->_logger->debug('Luigi\'s Box Indexer triggered');

if (!$this->_helper->isConfigured()) {
$this->_logger->debug('Luigi\'s Box not configured, can not index products');
return;
}

foreach ($ids as $id) {
$this->_helper->singleContentUpdate($id);
}
$this->_logger->debug('Luigi\'s Box Indexer finished');
}

public function executeRow($id)
{
if (!$this->_helper->isConfigured()) {
$this->_logger->debug('Luigi\'s Box not configured, can not index products');
return;
}
$this->_helper->singleContentUpdate($id);
}
}
32 changes: 0 additions & 32 deletions Observer/ProductAfterSave.php

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Installation

We strongly recommend to use [Composer](https://getcomposer.org/).

Please run the following commands in root of your M2 installation:
Please run **all** of the following commands in root of your M2 installation:

- ```$ composer require luigisbox/search-suite-magento2```
- ```$ bin/magento module:enable LuigisBox_SearchSuite```
- ```$ bin/magento setup:upgrade```
- ```$ bin/magento setup:di:compile```
- ```$ bin/magento indexer:set-mode schedule luigisbox_index_product```
- ```$ bin/magento indexer:set-mode schedule luigisbox_reindex```

Once you completed these steps, you are done with the installation. Now please go to `Administration > Stores > Configuration > Luigi's Box` and configure the extension there.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "magento2-module",
"minimum-stability": "stable",
"license": ["MIT"],
"version": "1.3.4",
"version": "2.0.0",
"require": {
"php": "~7.0|~7.1|~7.2",
"magento/framework": "~100.1|~101.0|~102.0",
Expand Down
6 changes: 0 additions & 6 deletions etc/adminhtml/events.xml

This file was deleted.

13 changes: 12 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@
</depends>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="force_reindex" translate="label comment" type="button" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="cron_schedule" translate="label comment" type="text" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Nightly reindex cron schedule</label>
<depends>
<field id="enabled">1</field>
</depends>
<comment>
<![CDATA[
Enter the cron schedule expression. Use <a href="https://crontab.guru/" target="_blank">this website</a> to help you make one.
]]>
</comment>
</field>
<field id="force_reindex" translate="label comment" type="button" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="1">
<frontend_model>LuigisBox\SearchSuite\Block\Reindex</frontend_model>
<label>Force reindex</label>
<depends>
Expand Down
3 changes: 2 additions & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<attributes_visible>1</attributes_visible>
<attributes_searchable>1</attributes_searchable>
<attributes_filterable>1</attributes_filterable>
<cron_schedule>0 4 * * *</cron_schedule>
</settings>
</luigisboxsearch_settings>
</default>
</config>
</config>
6 changes: 3 additions & 3 deletions etc/crontab.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="luigisbox_cron_checker" instance="LuigisBox\SearchSuite\Cron\Checker" method="execute">
<schedule>* * * * *</schedule>
<job name="luigisbox_idx_invalidator" instance="LuigisBox\SearchSuite\Cron\IndexInvalidator" method="execute">
<config_path>luigisboxsearch_settings/settings/cron_schedule</config_path>
</job>
</group>
</config>
</config>
6 changes: 3 additions & 3 deletions etc/indexer.xml
Original file line number Diff line number Diff line change
@@ -1,9 +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="luigisbox_index_product" view_id="luigisbox_index_product" class="LuigisBox\SearchSuite\Model\Indexer\Product">
<title translate="true">Luigi's Box Search Results</title>
<indexer id="luigisbox_reindex" view_id="luigisbox_reindex" class="LuigisBox\SearchSuite\Model\Indexer\Product">
<title translate="true">Luigi's Box Reindexer</title>
<description translate="true">
Rebuild Luigi's Box catalog index.
Reindex products in Luigi's Box catalog index.
</description>
</indexer>
</config>
15 changes: 13 additions & 2 deletions etc/mview.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?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="luigisbox_index_product" class="LuigisBox\SearchSuite\Model\Indexer\Product" group="indexer">
<view id="luigisbox_reindex" class="LuigisBox\SearchSuite\Model\Indexer\Product" 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_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" />
</subscriptions>
</view>
</config>
</config>

0 comments on commit 374ffbe

Please sign in to comment.