|
27 | 27 | */ |
28 | 28 | class FireGento_AdminMonitoring_Model_Clean |
29 | 29 | { |
30 | | - const XML_PATH_ADMINMONITORING_INTERVAL = 'admin/firegento_adminmonitoring/interval'; |
| 30 | + const XML_PATH_ADMINMONITORING_INTERVAL = 'admin/firegento_adminmonitoring/interval'; |
31 | 31 | const XML_PATH_ADMINMONITORING_CLEAN_ENABLED = 'admin/firegento_adminmonitoring/enable_cleaning'; |
32 | 32 |
|
| 33 | + /** |
| 34 | + * Clean in chunks. |
| 35 | + * |
| 36 | + * CHUNK_SIZE determines the items cleared per chunk. |
| 37 | + * |
| 38 | + * CHUNK_RUNS determines the number of chunks cleaned per call to clean() |
| 39 | + * |
| 40 | + * I.e. per call of clean(), at most CHUNK_SIZE * CHUNK_RUNS items are cleaned. |
| 41 | + */ |
| 42 | + const CHUNK_SIZE = 1000; |
| 43 | + const CHUNK_RUNS = 250; |
| 44 | + |
33 | 45 | /** |
34 | 46 | * Cronjob method for cleaning the database table. |
35 | 47 | * |
@@ -63,22 +75,53 @@ public function clean() |
63 | 75 | return $this; |
64 | 76 | } |
65 | 77 |
|
| 78 | + $this->cleanInChunks(); |
| 79 | + |
| 80 | + return $this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Clean the database table for the given interval, usink chunks to avoid memory over-usage. |
| 85 | + * |
| 86 | + * @return $this |
| 87 | + */ |
| 88 | + protected function cleanInChunks() |
| 89 | + { |
| 90 | + $numChunks = 0; |
| 91 | + do { |
| 92 | + $cleanedItems = $this->cleanChunk(); |
| 93 | + } while ($cleanedItems == static::CHUNK_SIZE && $numChunks++ < static::CHUNK_RUNS); |
| 94 | + |
| 95 | + return $this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Clean a chunk of the items in database table for the given interval. |
| 100 | + * |
| 101 | + * @return int Number of items deleted |
| 102 | + */ |
| 103 | + protected function cleanChunk() |
| 104 | + { |
66 | 105 | $interval = Mage::getStoreConfig(self::XML_PATH_ADMINMONITORING_INTERVAL); |
67 | 106 |
|
68 | 107 | /* @var $adminMonitoringCollection FireGento_AdminMonitoring_Model_Resource_History_Collection */ |
69 | 108 | $adminMonitoringCollection = Mage::getModel('firegento_adminmonitoring/history') |
70 | 109 | ->getCollection() |
| 110 | + ->setPageSize(static::CHUNK_SIZE) |
71 | 111 | ->addFieldToFilter( |
72 | 112 | 'created_at', |
73 | 113 | array( |
74 | 114 | 'lt' => new Zend_Db_Expr("DATE_SUB('" . now() . "', INTERVAL " . (int) $interval . " DAY)") |
75 | 115 | ) |
76 | 116 | ); |
77 | 117 |
|
| 118 | + $count = 0; |
| 119 | + |
78 | 120 | foreach ($adminMonitoringCollection as $history) { |
79 | 121 | $history->delete(); |
| 122 | + $count++; |
80 | 123 | } |
81 | 124 |
|
82 | | - return $this; |
| 125 | + return $count; |
83 | 126 | } |
84 | 127 | } |
0 commit comments