diff --git a/app/code/core/Mage/Index/Model/Indexer.php b/app/code/core/Mage/Index/Model/Indexer.php index 781dd480680..49478cf2bc1 100644 --- a/app/code/core/Mage/Index/Model/Indexer.php +++ b/app/code/core/Mage/Index/Model/Indexer.php @@ -282,10 +282,10 @@ public function logEvent(Varien_Object $entity, $entityType, $eventType, $doSave * @param Varien_Object $entity * @param string $entityType * @param string $eventType - * @throws Exception * @return Mage_Index_Model_Indexer + * @throws Exception|Throwable */ - public function processEntityAction(Varien_Object $entity, $entityType, $eventType) + public function processEntityAction(Varien_Object $entity, $entityType, $eventType): Mage_Index_Model_Indexer { $event = $this->logEvent($entity, $entityType, $eventType, false); /** diff --git a/app/code/core/Mage/Index/Model/Observer.php b/app/code/core/Mage/Index/Model/Observer.php index 93a15966456..84560f54fc3 100644 --- a/app/code/core/Mage/Index/Model/Observer.php +++ b/app/code/core/Mage/Index/Model/Observer.php @@ -19,6 +19,9 @@ */ class Mage_Index_Model_Observer { + public const OLD_INDEX_EVENT_THRESHOLD_SECONDS = 24 * 60 * 60; + public const OLD_INDEX_EVENT_DELETE_COUNT = 1000; + /** * Indexer model * @@ -35,6 +38,7 @@ public function __construct() * Store after commit observer. Process store related indexes * * @param Varien_Event_Observer $observer + * @throws Throwable */ public function processStoreSave(Varien_Event_Observer $observer) { @@ -50,6 +54,7 @@ public function processStoreSave(Varien_Event_Observer $observer) * Store group after commit observer. Process store group related indexes * * @param Varien_Event_Observer $observer + * @throws Throwable */ public function processStoreGroupSave(Varien_Event_Observer $observer) { @@ -65,6 +70,7 @@ public function processStoreGroupSave(Varien_Event_Observer $observer) * Website save after commit observer. Process website related indexes * * @param Varien_Event_Observer $observer + * @throws Throwable */ public function processWebsiteSave(Varien_Event_Observer $observer) { @@ -80,6 +86,7 @@ public function processWebsiteSave(Varien_Event_Observer $observer) * Store after commit observer. Process store related indexes * * @param Varien_Event_Observer $observer + * @throws Throwable */ public function processStoreDelete(Varien_Event_Observer $observer) { @@ -95,6 +102,7 @@ public function processStoreDelete(Varien_Event_Observer $observer) * Store group after commit observer. Process store group related indexes * * @param Varien_Event_Observer $observer + * @throws Throwable */ public function processStoreGroupDelete(Varien_Event_Observer $observer) { @@ -110,6 +118,7 @@ public function processStoreGroupDelete(Varien_Event_Observer $observer) * Website save after commit observer. Process website related indexes * * @param Varien_Event_Observer $observer + * @throws Throwable */ public function processWebsiteDelete(Varien_Event_Observer $observer) { @@ -125,6 +134,7 @@ public function processWebsiteDelete(Varien_Event_Observer $observer) * Config data after commit observer. * * @param Varien_Event_Observer $observer + * @throws Throwable */ public function processConfigDataSave(Varien_Event_Observer $observer) { @@ -135,4 +145,57 @@ public function processConfigDataSave(Varien_Event_Observer $observer) Mage_Index_Model_Event::TYPE_SAVE ); } + + /** + * Clean old index events for indexers in manual mode + * + * @return void + * @throws Exception + */ + public function cleanOutdatedEvents() + { + $manualIndexProcessCollection = Mage::getSingleton('index/indexer') + ->getProcessesCollection() + ->addFieldToFilter('mode', Mage_Index_Model_Process::MODE_MANUAL); + + $now = new DateTime(); + /** @noinspection PhpUnhandledExceptionInspection */ + $dateInterval = new DateInterval('PT' . self::OLD_INDEX_EVENT_THRESHOLD_SECONDS . 'S'); + $oldEventsThreshold = $now + ->sub($dateInterval) + ->format(Varien_Db_Adapter_Pdo_Mysql::TIMESTAMP_FORMAT); + + $coreResource = Mage::getSingleton('core/resource'); + $writeConnection = $coreResource->getConnection('core_write'); + $indexEventTableName = $coreResource->getTableName('index/event'); + + /** @var Mage_Index_Model_Process $process */ + foreach ($manualIndexProcessCollection as $process) { + $unprocessedEventsCollection = $process + ->getUnprocessedEventsCollection() + ->addFieldToFilter('created_at', ['lt' => $oldEventsThreshold]) + ->load(); + + $i = 0; + $eventList = []; + /** @var Mage_Index_Model_Event $unprocessedEvent */ + foreach ($unprocessedEventsCollection as $unprocessedEvent) { + $i++; + $eventList[] = $unprocessedEvent->getId(); + if ($i === self::OLD_INDEX_EVENT_DELETE_COUNT) { + break; + } + } + + if (!empty($eventList)) { + $where = new Zend_Db_Expr( + sprintf( + 'event_id in (%s)', + implode(',', $eventList) + ) + ); + $writeConnection->delete($indexEventTableName, $where); + } + } + } } diff --git a/app/code/core/Mage/Index/etc/config.xml b/app/code/core/Mage/Index/etc/config.xml index 9a46c2920b0..94e1aa18f54 100644 --- a/app/code/core/Mage/Index/etc/config.xml +++ b/app/code/core/Mage/Index/etc/config.xml @@ -20,6 +20,18 @@ 1.6.0.0 + + + + + 30 */4 * * * + + + index/observer::cleanOutdatedEvents + + + +