Magento 2 How To Check Indexer Mode Is Running By Schedule Or Not Programmatically

If you want to check whether the indexer mode is running by schedule or not, we can following code snippet.

Magento has two types of Indexer mode: Update by schedule and Update on Save.

If we want to know about the specifically given type of indexer mode is by scheduled or not, we need to collect specific indexer id like catalog_category_product we are checking the type of mode.

<?php
namespace Magemeta\Indexer\Model;
 
use Magento\Framework\Indexer\IndexerRegistry;
 
class Mode
{
    /**
     * @var IndexerRegistry
     */
    protected $indexerRegistry;
 
    public function __construct(
        IndexerRegistry $indexerRegistry
    ) {
        $this->indexerRegistry = $indexerRegistry;
    }
 
    /**
     * @return bool
     */
    public function isIndexerScheduled()
    {
     $isScheduled = false;
     $indexerId = 'catalog_category_product';// you need to make it dynamic by passing this indexer id in method params
     try {
            $indexer = $this->indexerRegistry->get($indexerId);
 
            $isScheduled = $indexer->isScheduled()
        } catch (\InvalidArgumentException $e) {
        }
        return $isScheduled;
    }
}

To check the result, we can call the function as:

$result = $this->isIndexerScheduled();

We can check any indexer value using the same approach just by passing the actual indexer id.

Leave a Reply