diff --git a/VERSIONS_HOTLINE/37385 b/VERSIONS_HOTLINE/37385 new file mode 100644 index 0000000000000000000000000000000000000000..a2c8f94c0f83e2c5c8281392952a47f339c8e860 --- /dev/null +++ b/VERSIONS_HOTLINE/37385 @@ -0,0 +1 @@ + - ticket #37385 : Réduction du temps de la phase de suppression des notices sans exemplaires diff --git a/library/Class/Cosmogramme/Integration/PhaseAbstract.php b/library/Class/Cosmogramme/Integration/PhaseAbstract.php index 844f79c062c5e22f9e436632ccb4ed82fcd54e28..e7d2935c27d167c01d5d8dbc5806c0694f732c6d 100644 --- a/library/Class/Cosmogramme/Integration/PhaseAbstract.php +++ b/library/Class/Cosmogramme/Integration/PhaseAbstract.php @@ -35,6 +35,20 @@ abstract class Class_Cosmogramme_Integration_PhaseAbstract { } + public function setSqlAdapter($adapter) { + $this->_sql_adapter = $adapter; + return $this; + } + + + protected function getSqlAdapter() { + if(!$this->_sql_adapter) + $this->_sql_adapter = Zend_Db_Table::getDefaultAdapter(); + + return $this->_sql_adapter; + } + + protected abstract function _execute(); protected abstract function _init($phase); @@ -134,4 +148,16 @@ abstract class Class_Cosmogramme_Integration_PhaseAbstract { return !$this->_phase->isCron() && $this->_chrono->mainElapsed() > $this->_chrono->timeout(); } + + + protected function _resetDbConnection() { + if (!$this->_db_reset) + return $this; + + Storm_Model_Abstract::unsetLoaders(); + Zend_Db_Table::getDefaultAdapter()->closeConnection(); + setupDatabase(loadConfig()); + gc_collect_cycles(); + return $this; + } } \ No newline at end of file diff --git a/library/Class/Cosmogramme/Integration/PhaseDeleteRecordWithoutItem.php b/library/Class/Cosmogramme/Integration/PhaseDeleteRecordWithoutItem.php index e37cf361e0ca9828554999806edefd55e73758f5..3b3995cf0b619beabe809507c5bbbbe9a30ff9a8 100644 --- a/library/Class/Cosmogramme/Integration/PhaseDeleteRecordWithoutItem.php +++ b/library/Class/Cosmogramme/Integration/PhaseDeleteRecordWithoutItem.php @@ -26,46 +26,41 @@ class Class_Cosmogramme_Integration_PhaseDeleteRecordWithoutItem extends Class_C protected $_label = 'Suppression des notices sans exemplaire'; public function _execute() { - if ($this->isTimeOut()) - return $this->_phase; - $this->_chrono->start(); - $page_size = 1000; - $page = 1; + $page_size = 100; + $page = 0; + + while($records_ids = $this->getSqlAdapter()->fetchAll('SELECT notices.id_notice FROM notices WHERE notices.id_notice NOT IN (SELECT exemplaires.id_notice FROM exemplaires) LIMIT ' . $page . ', ' . $page_size)) { - while ($notices = Class_Notice::findAllBy(['order' => 'id_notice', - 'limitPage' => [$page, $page_size]])) { - foreach ($notices as $notice) { - $this->_runOne($notice); - } + if ($this->isTimeOut()) + return $this->_resetDbConnection() + ->_phase; + foreach ($records_ids as $record_id) + $this->_runOne($record_id['id_notice']); + + $this->_log->ecrire($this->_getData('deleted') . ' notices sans exemplaire supprimées<br/>'); $this->_cleanMemory(); $page++; } - if (0 == $this->_getData('deleted')) + if (0 == $this->_getData('deleted')) { $this->_log->ecrire('Aucune notice sans exemplaire'); + return; + } - if (0 < $this->_getData('deleted')) - $this->_log->ecrire($this->_getData('deleted').' notices sans exemplaire supprimées'); - $this->_log->ecrire('Temps de traitement: '.$this->_chrono->endMain().' ('.$this->_chrono->mainAverage($this->_getData('nombre'), 'notices').')' ); + $this->_log->ecrire('Temps de traitement: '.$this->_chrono->endMain().' ('.$this->_chrono->mainAverage($this->_getData('deleted'), 'notices').')' ); } - protected function _runOne($notice) { - $this->_incrementData('nombre'); - - if ($notice->hasExemplaires()) - return; - - $notice->delete(); + protected function _runOne($record_id) { + Class_Notice::find($record_id)->delete(); $this->_incrementData('deleted'); } protected function _init($phase) { $phase ->resetDatas() - ->setData('nombre', 0) ->setData('deleted', 0); } } diff --git a/library/Class/Cosmogramme/Integration/PhaseItemFacets.php b/library/Class/Cosmogramme/Integration/PhaseItemFacets.php index 9fb699df84fb4c1f4d43ad34b6c1eed24c8d852c..1c40d36af5d451131ee267fcb82902a57dd4932b 100644 --- a/library/Class/Cosmogramme/Integration/PhaseItemFacets.php +++ b/library/Class/Cosmogramme/Integration/PhaseItemFacets.php @@ -128,18 +128,5 @@ class Class_Cosmogramme_Integration_PhaseItemFacets $this->_db_reset = false; return $this; } - - - protected function _resetDbConnection() { - if (!$this->_db_reset) - return $this; - - Storm_Model_Abstract::unsetLoaders(); - Zend_Db_Table::getDefaultAdapter()->closeConnection(); - setupDatabase(loadConfig()); - gc_collect_cycles(); - return $this; - } } - ?> \ No newline at end of file diff --git a/tests/library/Class/Cosmogramme/Integration/PhaseDeleteRecordWithoutItemTest.php b/tests/library/Class/Cosmogramme/Integration/PhaseDeleteRecordWithoutItemTest.php index 28c71ca79d33fb203b69d357671a7ba1771de8ea..e31c6ddc8722e630950fa10b457aab1f04319d04 100644 --- a/tests/library/Class/Cosmogramme/Integration/PhaseDeleteRecordWithoutItemTest.php +++ b/tests/library/Class/Cosmogramme/Integration/PhaseDeleteRecordWithoutItemTest.php @@ -21,13 +21,13 @@ abstract class PhaseDeleteRecordWithoutItemTestCase extends Class_Cosmogramme_Integration_PhaseTestCase { - protected $_phase; + protected $_phase, $_mock_sql; public function setUp() { parent::setUp(); - $this->_phase = $this->_buildPhase('DeleteRecordWithoutItem') ->setMemoryCleaner(function() {}) + ->setSqlAdapter($this->_mock_sql) ->run(); Class_Notice::clearCache(); Class_Exemplaire::clearCache(); @@ -64,13 +64,26 @@ abstract class PhaseDeleteRecordWithoutItemTestCase extends Class_Cosmogramme_In - class PhaseDeleteRecordWithoutItemSimpleTest extends PhaseDeleteRecordWithoutItemTestCase { protected function _prepareFixtures() { parent::_prepareFixtures(); $this->fixture('Class_Notice', ['id' => 34]); $this->fixture('Class_Notice', ['id' => 35]); + + $this->_mock_sql = Storm_Test_ObjectWrapper::mock(); + + $this->_mock_sql + ->whenCalled('fetchAll') + ->with('SELECT notices.id_notice FROM notices WHERE notices.id_notice NOT IN (SELECT exemplaires.id_notice FROM exemplaires) LIMIT 0, 100') + ->answers([0 => ['id_notice' => 34], + 1 => ['id_notice' => 35]]) + + ->whenCalled('fetchAll') + ->with('SELECT notices.id_notice FROM notices WHERE notices.id_notice NOT IN (SELECT exemplaires.id_notice FROM exemplaires) LIMIT 1, 100') + ->answers([]) + + ->beStrict(); } @@ -90,6 +103,18 @@ class PhaseDeleteRecordWithoutItemSimpleTest extends PhaseDeleteRecordWithoutIte class PhaseDeleteRecordWithoutItemNothingToDeleteTest extends PhaseDeleteRecordWithoutItemTestCase { + protected function _prepareFixtures() { + parent::_prepareFixtures(); + $this->_mock_sql = Storm_Test_ObjectWrapper::mock(); + + $this->_mock_sql + ->whenCalled('fetchAll') + ->with('SELECT notices.id_notice FROM notices WHERE notices.id_notice NOT IN (SELECT exemplaires.id_notice FROM exemplaires) LIMIT 0, 100') + ->answers([]) + + ->beStrict(); + } + /** @test */ public function totalNumberOfRecordsShouldBe3() {