0

I need to run separately with its own schedule the reindexing of:

catalog_category_product
catalog_product_category
catalog_category_flat

I dont find the in any crontab.xml

How can we do this? Thanks

  • do you mean, you want to run these 3 repetedly after some amount of time? – Dhiren Vasoya Aug 2 at 10:08
  • yes, and not anymore as part of the current, default, reindex processes.. – user968865 Aug 2 at 10:13
  • create separate cron job with magento command as per your requirement – Hardik Visa Aug 2 at 11:54
  • @HardikVisa But then i am adding a new cron for that index, not changing the schedule of the index in question right? – user968865 Aug 2 at 12:11
1

You can also create your own cron file and schedule with your specific time.

Create crontab.xml file here

app/code/YourModule/YourNamespace/etc/crontab.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <job instance="YourModule\YourNamespace\Cron\CronIndexing" method="execute" name="yourmodulename_cron_indexing_cronjob">
            <schedule>10 * * * *</schedule>
        </job>
    </group>
</config>

Above cron is run every 10 mins.

Now create a CRON class file

Create CronIndexing.php file here

app/code/YourModule/YourNamespace/Cron/CronIndexing.php

<?php

namespace YourModule\YourNamespace\Cron;

use \Psr\Log\LoggerInterface;

class CronIndexing
{
    protected $logger;
    /**
     * @var \Magento\Indexer\Model\IndexerFactory
     */
    protected $_indexerFactory;
    /**
     * @var \Magento\Indexer\Model\Indexer\CollectionFactory
     */
    protected $_indexerCollectionFactory;


    public function __construct(
        LoggerInterface $logger,
        \Magento\Indexer\Model\IndexerFactory $indexerFactory,
        \Magento\Indexer\Model\Indexer\CollectionFactory $indexerCollectionFactory
    )
    {
        $this->logger = $logger;
        $this->_indexerFactory = $indexerFactory;
        $this->_indexerCollectionFactory = $indexerCollectionFactory;
    }

    public function execute()
    {
        $indexerCollection = $this->_indexerCollectionFactory->create();
        $ids = $indexerCollection->getAllIds();
        foreach ($ids as $id) {
            $idx = $this->_indexerFactory->create()->load($id);
            $idx->reindexAll($id);
        }
        $this->logger->info('Successfully Reindexing');
    }
}

If not affecting anything after adding above files so please run below commands and try again:

  1. php bin/magento cron:install

  2. php bin/magento cron:run

  • well, with this i would be running them at specific times, but not avoiding them to run in the default reindexing, right? It is about reconfiguring the indexes in question, not adding more of them. – user968865 Aug 2 at 10:31
  • Yes you're right @user968865 – Praveen Chelumalla Aug 2 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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