Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Plugins to invalidate StockItem indexes when Stock-to-Source assignments have been changed #68

Closed
maghamed opened this issue Sep 1, 2017 · 3 comments
Assignees

Comments

@maghamed
Copy link
Contributor

maghamed commented Sep 1, 2017

Create Plugins to invalidate StockItem indexes when Stock-to-Source assignments have been changed.

For now, we don't mark index as invalid after some changes happened in Sources-to-Stocks assignment.

Precondition:
Source A: assigned to Stock 1
Source B: assigned to Stock 2

after that, we decided to assign:
Source B: assigned to Stock 1

after this operation, we need to invalidate whole Stock 1 index, because all the StockItems for Stock 1 should be re-calculated.

It's a good architectural task because we need to invalidate not the whole indexes , but specific dimensions (Stocks) which were affected

@larsroettig
Copy link
Member

Hi @maghamed,
I started with the implementation on Saturday. I would like disuse about the implementation.

I found the following function in the Interface.
https://github.com/magento-engcom/magento2/blob/a77be455ad9d34bece4f109c60ed2f1a3890b564/lib/internal/Magento/Framework/Indexer/IndexerInterface.php#L107

But I see a performance problem because I can not specifies which data is invalid if is the indexer in the scheduled mode. For big merchant need a full reindex hours only because one Source is not assigned anymore.

My current Implementation:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Inventory\Plugin;

use Magento\Framework\Indexer\IndexerInterfaceFactory;
use Magento\Inventory\Indexer\StockItemIndexerInterface;
use Magento\InventoryApi\Api\UnassignSourceFromStockInterface;

class UnassignSourceFromStock
{

    /**
     * @var \Magento\Framework\Indexer\IndexerInterface
     */
    private $indexer;

    /**
     * @param IndexerInterfaceFactory $indexerFactory
     */
    public function __construct(IndexerInterfaceFactory $indexerFactory)
    {
        /** @var  $indexer */
        $this->indexer = $indexerFactory->create();
        $this->indexer->load(StockItemIndexerInterface::INDEXER_ID);
    }


    public function aroundExecute(
        UnassignSourceFromStockInterface $subject,
        \Closure $proceed,
        int $sourceId
    ) {
        if ($this->indexer->isScheduled()) {
            $this->indexer->invalidate();
        } else {
            $this->indexer->reindexRow($sourceId);
        }
    }
}

@maghamed maghamed assigned maghamed and unassigned maghamed and larsroettig Sep 13, 2017
@maghamed
Copy link
Contributor Author

maghamed commented Sep 15, 2017

Results after the discussion :

  • Pugin (in Inventory module) looks like appropriate solution for Interface belonging to InventoryAPI module (there is no pluginization in the same Module, which is forbidden by Magento guidelines)
  • Plugin should just invalidate cache, not make a re-indexation (like this one - $this->indexer->reindexRow($sourceId))
  • Process of invalidation should be agnostic to the way of re-indexation (Update on Save or by Schedule), thus we no need to have a check like this: $this->indexer->isScheduled()
  • There should not be business logic in constructor

@naydav
Copy link

naydav commented Nov 22, 2017

#141

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants