|
28 | 28 | */ |
29 | 29 | class FireGento_AdminMonitoring_Model_Clean |
30 | 30 | { |
31 | | - const XML_PATH_ADMINMONITORING_INTERVAL = 'admin/firegento_adminmonitoring/interval'; |
| 31 | + const XML_PATH_ADMINMONITORING_INTERVAL = 'admin/firegento_adminmonitoring/interval'; |
32 | 32 | const XML_PATH_ADMINMONITORING_CLEAN_ENABLED = 'admin/firegento_adminmonitoring/enable_cleaning'; |
33 | 33 |
|
| 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 | + |
34 | 46 | /** |
35 | 47 | * Cronjob method for cleaning the database table. |
36 | 48 | * |
@@ -64,22 +76,53 @@ public function clean() |
64 | 76 | return $this; |
65 | 77 | } |
66 | 78 |
|
| 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 | + { |
67 | 106 | $interval = Mage::getStoreConfig(self::XML_PATH_ADMINMONITORING_INTERVAL); |
68 | 107 |
|
69 | 108 | /* @var $adminMonitoringCollection FireGento_AdminMonitoring_Model_Resource_History_Collection */ |
70 | 109 | $adminMonitoringCollection = Mage::getModel('firegento_adminmonitoring/history') |
71 | 110 | ->getCollection() |
| 111 | + ->setPageSize(static::CHUNK_SIZE) |
72 | 112 | ->addFieldToFilter( |
73 | 113 | 'created_at', |
74 | 114 | array( |
75 | 115 | 'lt' => new Zend_Db_Expr("DATE_SUB('" . now() . "', INTERVAL " . (int)$interval . " DAY)") |
76 | 116 | ) |
77 | 117 | ); |
78 | 118 |
|
| 119 | + $count = 0; |
| 120 | + |
79 | 121 | foreach ($adminMonitoringCollection as $history) { |
80 | 122 | $history->delete(); |
| 123 | + $count++; |
81 | 124 | } |
82 | 125 |
|
83 | | - return $this; |
| 126 | + return $count; |
84 | 127 | } |
85 | 128 | } |
0 commit comments