1

I have line of code which schedule full or partial reindex cron job.

using Trigger::REINDEX_SCOPE_PARTIAL and Trigger::REINDEX_SCOPE_FULL parameter.

public function restoreIndexers($reindex = false, $indexers = null, $threshold = null)
{
    foreach ($this->prepareCodesForReindex($indexers) as $code) {
        $previousMode = $this->indexerModes[$code] ?? null;

        if (\is_null($previousMode)) {
            continue;
        }

        try {
            /** @var \Magento\Framework\Indexer\IndexerInterface $indexer */
            $indexer = $this->getIndexerByCode($code);
            $currentMode = $indexer->isScheduled();
            $indexer->setScheduled($previousMode);
            $this->indexerModes[$code] = null;

            if (\is_array($reindex)) {
                $this->scheduleReindex($code, Trigger::REINDEX_SCOPE_PARTIAL, $reindex, $threshold);
            } elseif ($reindex) {
                $this->scheduleReindex($code, Trigger::REINDEX_SCOPE_FULL);
            }
        } catch (\Exception $e) {
            $this->logger->critical($e, [
                'indexer' => $code,
                'previous_mode' => $previousMode ? 'on schedule' : 'on save',
                'current_mode' => isset($currentMode) ? ($currentMode ? 'on schedule' : 'on save') : 'unset',
            ]);
        }
    }
}

I need to call mview reindexer using this function. I don't know how can I achieve this. Trigger::REINDEX_SCOPE_MVIEW, and need to add option code under Trigger class. but I am not aware how I can call MVIEW reindexer under scheduleReindex function.

I know very little about this MVIEW reindexer. Can anyone please provide a suggestion?

3
  • It's a very old question but if you still need the answer then please reply and I will answer it
    – Arif Ahmad
    Nov 17, 2020 at 18:38
  • Yes, required answer
    – Charlie
    Nov 20, 2020 at 3:39
  • added. let me know if you need more insight and I will be happy to help on that
    – Arif Ahmad
    Nov 23, 2020 at 10:01

1 Answer 1

0

MVIEW index works on changelog data and only for mode "On Schedule". If reindex mode is on save then there is no use of MVIEW.

Now, coming back to question.

The scheduled MVIEW jobs are executed by default Magento cron. So, it will check the backlog data and will do reindex only on the changelog.

Now, if you read the above statement then it means you do not have to schedule it but it will work when there is a backlog.

Let's say it is having backlog and you would like to execute by a custom command and not by using the default Magento cron for some reason.

here is a code snippet that will do the reindex for all views. Now, you can modify it and use in your class where applicable.

$di = $bootstrap->getObjectManager();

$state = $di->get('Magento\Framework\App\State');
$state->setAreaCode('crontab');

$viewsFactory = $di->get('\Magento\Framework\Mview\View\CollectionFactory');
$collection = $viewsFactory->create();

foreach ($collection->getItems() as $view) {
    try {
        $view->update(); # Updates the index data based on changelog
    } catch (Exception $e) {
        // do something here like log $e->getMessage();
    }
}

Again, if anything more is needed then please let me know I will try to explain little more.

The foreach loop is the most important area of interest for you.

Be very careful when playing with Magento Indexers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.