Skip to content

Commit

Permalink
Added cron to remove old index events for manual indexers (OpenMage#2474
Browse files Browse the repository at this point in the history
)

Co-authored-by: Fabrizio Balliano <[email protected]>
  • Loading branch information
tobihille and fballiano authored May 9, 2023
1 parent d42170e commit 460ce65
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/code/core/Mage/Index/Model/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
/**
Expand Down
63 changes: 63 additions & 0 deletions app/code/core/Mage/Index/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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);
}
}
}
}
12 changes: 12 additions & 0 deletions app/code/core/Mage/Index/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
<version>1.6.0.0</version>
</Mage_Index>
</modules>
<crontab>
<jobs>
<index_clean_events>
<schedule>
<cron_expr>30 */4 * * *</cron_expr>
</schedule>
<run>
<model>index/observer::cleanOutdatedEvents</model>
</run>
</index_clean_events>
</jobs>
</crontab>
<global>
<helpers>
<index>
Expand Down

0 comments on commit 460ce65

Please sign in to comment.