1$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
2 $indexerCollectionFactory = $objectManager->get("\Magento\Indexer\Model\Indexer\CollectionFactory");
3 $indexerFactory = $objectManager->get("\Magento\Indexer\Model\IndexerFactory");
4
5 $indexerCollection = $indexerCollectionFactory->create();
6 $allIds = $indexerCollection->getAllIds();
7 foreach ($allIds as $id) {
8 $indexer = $indexerFactory->create()->load($id);
9 $indexer->reindexAll();
10 }
1<?php
2
3namespace Sark\CustomApi\Model;
4
5class CustomRepository implements Sark\CustomApi\Api\CustomRepositoryInterface
6{
7 /**
8 * @var \Magento\Framework\Indexer\IndexerRegistry
9 */
10 private $indexerRegistry;
11 /**
12 * @var \Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory
13 */
14 private $stateCollectionFactory;
15
16 public function __construct(
17 \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
18 \Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory $stateCollectionFactory
19 ) {
20 $this->indexerRegistry = $indexerRegistry;
21 $this->stateCollectionFactory = $stateCollectionFactory;
22 }
23
24 public function create()
25 {
26 $indexerStates = $this->stateCollectionFactory->create();
27 foreach ($indexerStates->getItems() as $indexerState) {
28 $indexerState->getIndexerId();
29 $indexer = $this->indexerRegistry->get($indexerState->getIndexerId());
30 if ($indexer->getId() && !$indexer->isWorking()) {
31 $indexer->setScheduled(true);
32 }
33 }
34 }
35}