inventory icon indicating copy to clipboard operation
inventory copied to clipboard

Stock Item reindex acts as "update on save" even when using "update on schedule"

Open k4emic opened this issue 5 years ago • 8 comments

Preconditions

  1. Magento 2.3.0
  2. Inventory indexer set to "Update on schedule"

Steps to reproduce

  1. Use import-export feature to import any MSI stock items

Expected result

  1. Stock items are saved
  2. Reindexing is handled by scheduled cron

Actual result

  1. Reindexing is handled immediately after import, then again by scheduled cron

Why this happens

The underlying impleentation for all of the import strategies (replace/append/delete) uses the following interface for handling the save operation, which has a plugin attached to it to ensure that a reindex takes place.

# vendor/magento/module-inventory-indexer/etc/di.xml
<type name="Magento\InventoryApi\Api\SourceItemsSaveInterface">
        <plugin name="reindex_after_source_items_save" type="Magento\InventoryIndexer\Plugin\InventoryApi\ReindexAfterSourceItemsSavePlugin"/>
    </type>

The plugin does not test the current indexing strategy (save/schedule) to decide if the reindex should happen immediately or be scheduled for later.

k4emic avatar Jan 29 '19 11:01 k4emic

@maghamed I'm working with this, can u assign me? thanks

TadeuRodrigues avatar Feb 01 '19 03:02 TadeuRodrigues

While doing some work with MSI today, we found that (always) doing a scheduled reindex might not be the best way of handling this, since the stock deduction triggers a reindex through the plugin mentioned in this issue. This means that if the reindex is done by schedule, some time will pass where merchants are likely to be overselling items, since a positive reservation is placed (putting items back into stock).

# \Magento\InventoryShipping\Observer\SourceDeductionProcessor
$this->sourceDeductionService->execute($sourceDeductionRequest); # deducts stock, runs reindex
$this->placeCompensatingReservation($sourceDeductionRequest); # places compensating reservation to ensure the previous reservation is deleted

k4emic avatar Feb 01 '19 09:02 k4emic

sorry, I missed this thread. @TadeuRodrigues I just granted you write permission for MSI repo, and since now, you can self-assign yourself on open tickets and create Pull Request not from own fork, but just make branching from 2.3-develop MSI branch.

Did the same for you @k4emic as well

maghamed avatar Feb 05 '19 16:02 maghamed

Hi @smoskaluk Was this problem fixed in a recent pull request? I do not see any reference to a merged pull request at this point in time.

k4emic avatar Jun 19 '19 15:06 k4emic

Hello,

I faced with the same issue.

If I comment the plugin which force the reindex, my stock import takes 3 minutes.

If I keep the native code, my stock import takes 24 minutes.

Can you please reference some commit or explain your approach.

Thanks,

franckgarnier21 avatar Jul 10 '19 07:07 franckgarnier21

Anyupdate on this?

Seem like

vendor/magento/module-inventory-configurable-product-admin-ui/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_catalog_product_save_entity_after">
        <observer name="process_source_items_for_configurable" instance="Magento\InventoryConfigurableProductAdminUi\Observer\ProcessSourceItemsObserver"/>
    </event>
</config>

Not sure the Stock/Source reindex strategy could fix the problem or not

image

tuyennn avatar Aug 12 '21 15:08 tuyennn

I see it is still available issue which has consequences for cache. If inventory indexer is set to Update on schedule the cache is invalidated twice. T The first time when the executeList is executed in \Magento\InventoryIndexer\Plugin\InventoryApi\ReindexAfterDecrementSourceItemQty::afterExecute as the executeList has plugins of \Magento\Indexer\Model\Indexer\CacheCleaner::beforeExecuteList and \Magento\Indexer\Model\Indexer\CacheCleaner::afterExecuteList which responsible to clean cache for all dependent entities.

artemii-karkusha avatar Nov 23 '23 09:11 artemii-karkusha

I see the instant indexing through the sourceItemSave plugin is required to prevent over selling scenario. In that case isn't it a better idea to disable the indexing through cron (indexer_update_all_views) for source item change logs to at least get rid of the duplicate effort in inventory/search indexing as well as cache clean?

siju-corra avatar Feb 08 '24 05:02 siju-corra