Skip to content

Commit d90cbb5

Browse files
committed
Merge pull request #17 from gruenspar/delete-in-chunks
Reduce memory usage while cleaning up
2 parents 995164f + 334222f commit d90cbb5

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ DROP TABLE 'firegento_adminmonitoring_history';
122122

123123
Support
124124
-------
125-
If you have any issues with this extension, open an issue on [GitHub](https://github.com/firegento/firegento-customer/issues).
125+
If you have any issues with this extension, open an issue on [GitHub](https://github.com/firegento/firegento-adminmonitoring/issues).
126126

127127
Contribution
128128
------------

src/app/code/community/FireGento/AdminMonitoring/Model/Clean.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,21 @@
2828
*/
2929
class FireGento_AdminMonitoring_Model_Clean
3030
{
31-
const XML_PATH_ADMINMONITORING_INTERVAL = 'admin/firegento_adminmonitoring/interval';
31+
const XML_PATH_ADMINMONITORING_INTERVAL = 'admin/firegento_adminmonitoring/interval';
3232
const XML_PATH_ADMINMONITORING_CLEAN_ENABLED = 'admin/firegento_adminmonitoring/enable_cleaning';
3333

34+
/**
35+
* Clean in chunks.
36+
*
37+
* CHUNK_SIZE determines the items cleared per chunk.
38+
*
39+
* CHUNK_RUNS determines the number of chunks cleaned per call to clean()
40+
*
41+
* I.e. per call of clean(), at most CHUNK_SIZE * CHUNK_RUNS items are cleaned.
42+
*/
43+
const CHUNK_SIZE = 1000;
44+
const CHUNK_RUNS = 250;
45+
3446
/**
3547
* Cronjob method for cleaning the database table.
3648
*
@@ -64,22 +76,53 @@ public function clean()
6476
return $this;
6577
}
6678

79+
$this->cleanInChunks();
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* Clean the database table for the given interval, usink chunks to avoid memory over-usage.
86+
*
87+
* @return $this
88+
*/
89+
protected function cleanInChunks()
90+
{
91+
$numChunks = 0;
92+
do {
93+
$cleanedItems = $this->cleanChunk();
94+
} while ($cleanedItems == static::CHUNK_SIZE && $numChunks++ < static::CHUNK_RUNS);
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* Clean a chunk of the items in database table for the given interval.
101+
*
102+
* @return int Number of items deleted
103+
*/
104+
protected function cleanChunk()
105+
{
67106
$interval = Mage::getStoreConfig(self::XML_PATH_ADMINMONITORING_INTERVAL);
68107

69108
/* @var $adminMonitoringCollection FireGento_AdminMonitoring_Model_Resource_History_Collection */
70109
$adminMonitoringCollection = Mage::getModel('firegento_adminmonitoring/history')
71110
->getCollection()
111+
->setPageSize(static::CHUNK_SIZE)
72112
->addFieldToFilter(
73113
'created_at',
74114
array(
75115
'lt' => new Zend_Db_Expr("DATE_SUB('" . now() . "', INTERVAL " . (int)$interval . " DAY)")
76116
)
77117
);
78118

119+
$count = 0;
120+
79121
foreach ($adminMonitoringCollection as $history) {
80122
$history->delete();
123+
$count++;
81124
}
82125

83-
return $this;
126+
return $count;
84127
}
85128
}

0 commit comments

Comments
 (0)