magento2 – Magento 2 Catalog rule can not applied after reindex automatic


I wrote the code for creating a Catalog rule in Magento 2.3.5 and after saving I have the code to re-index it automatically.

  /** @var MagentoCatalogRuleModelRule $model */
                $ruleRepository = $this->_objectManager->get(
                    MagentoCatalogRuleApiCatalogRuleRepositoryInterface::class
                );
                $catalogRule = $this->_objectManager->create(MagentoCatalogRuleModelRule::class);
                $ruleByBoRefId = $this->getRuleByBoRefId($boRefId);
                if (isset($ruleByBoRefId("rule_id")) && $ruleId = $ruleByBoRefId("rule_id")) {
                    $catalogRule = $ruleRepository->get($ruleId);
                }

                $catalogRule->loadPost($catalogRuleData);
                //$catalogRule->save();
                $ruleRepository->save($catalogRule);
                $this->reindexCatalogRule($catalogRule);

Code Reindex auto:

public function reindexCatalogRule($catalogRule){

    if($catalogRule->isRuleBehaviorChanged()){
        $this->_objectManager
            ->create(MagentoCatalogRuleModelFlag::class)
            ->loadSelf()
            ->setState(0)
            ->save();
    }

    $collection = $this->_indexerCollectionFactory->create()->getAllIds();
    foreach ($collection as $id) {
        $indexer = $this->_indexerFactory->create()->load($id);
        $indexer->getState()
            ->setStatus(MagentoFrameworkIndexerStateInterface::STATUS_INVALID)
            ->save();
        $indexer->reindexAll();
    }
}

But after reindex the Rule still doesn’t apply to the product. However, after I go to the admin and edited the previously saved catalog rule => Click Save => Then I ran the Indexer: reindex command of magento, the rule applies normally. I don’t understand why like that. Please help me
How to apply rules to products automatically ?
Thanks