From c54f68f5c4f28646b5b36901a4eaf68e458c28ee Mon Sep 17 00:00:00 2001 From: Ghislain Loas <ghislo@sandbox.pergame.net> Date: Wed, 10 Feb 2016 13:39:28 +0100 Subject: [PATCH] hotline #38320 add try catch to PhaseAbstract run + add cleanMemory in phase _execute --- VERSIONS_HOTLINE/38320 | 1 + .../php/integration/integration_phase.php | 2 -- .../Cosmogramme/Integration/PhaseAbstract.php | 25 ++++++++++++++--- .../Cosmogramme/Integration/PhaseBatchs.php | 2 ++ .../Integration/PhaseItemFacets.php | 11 +++----- .../Integration/PhaseOnDataSource.php | 1 + .../Integration/PhasePseudoRecord.php | 1 + .../Cosmogramme/Integration/PhaseReviews.php | 27 +++---------------- .../Integration/PhaseItemFacetsTest.php | 26 +++++++++++++++++- .../Integration/PhaseNoticeTestCase.php | 4 ++- .../Integration/PhasePanierTest.php | 5 ++-- .../Integration/PhasePatronsTest.php | 1 + .../Integration/PhaseReservationTest.php | 3 ++- .../Integration/PhaseReviewsTest.php | 1 + 14 files changed, 69 insertions(+), 41 deletions(-) create mode 100644 VERSIONS_HOTLINE/38320 diff --git a/VERSIONS_HOTLINE/38320 b/VERSIONS_HOTLINE/38320 new file mode 100644 index 00000000000..2f3db3edf78 --- /dev/null +++ b/VERSIONS_HOTLINE/38320 @@ -0,0 +1 @@ + - ticket #38320 : Cosmogramme : Ajout d'une gestion d'erreur sur l'execution des différentes phases diff --git a/cosmogramme/php/integration/integration_phase.php b/cosmogramme/php/integration/integration_phase.php index 3927cb03261..db3877a85af 100644 --- a/cosmogramme/php/integration/integration_phase.php +++ b/cosmogramme/php/integration/integration_phase.php @@ -46,9 +46,7 @@ function startIntegrationPhase($name) { $current_chrono->backToLegacyState($chrono, $chrono_fichier, $chrono100notices); $new_phase->backToLegacyState($phase, $phase_data, $mode_cron, $reprise, $compteur); - if (!$mode_cron && $requested_phase->isTimeOut()) sauveContexte(); } - ?> \ No newline at end of file diff --git a/library/Class/Cosmogramme/Integration/PhaseAbstract.php b/library/Class/Cosmogramme/Integration/PhaseAbstract.php index e7d2935c27d..7f74223fd1f 100644 --- a/library/Class/Cosmogramme/Integration/PhaseAbstract.php +++ b/library/Class/Cosmogramme/Integration/PhaseAbstract.php @@ -23,8 +23,13 @@ abstract class Class_Cosmogramme_Integration_PhaseAbstract { use Trait_TimeSource, Trait_StaticFileSystem, Trait_Translator, Trait_MemoryCleaner; - protected $_label = ''; - protected $_phase, $_log, $_printer, $_chrono, $_is_time_out; + protected $_label = '', + $_phase, + $_log, + $_printer, + $_chrono, + $_is_time_out, + $_db_reset = true; public function __construct($phase, $log, $chrono) { @@ -83,7 +88,14 @@ abstract class Class_Cosmogramme_Integration_PhaseAbstract { $this->_printLabel(); - $this->_execute(); + try { + $this->_execute(); + } catch (Exception $e) { + $this->_log->ecrire('<p class="rouge">' . sprintf('Erreur lors de l\'execution de la phase %s : </br>%s', + $this->_label, + $e->getMessage()) . '</p>'); + $this->_cleanMemory(); + } return $this->_phase; } @@ -160,4 +172,11 @@ abstract class Class_Cosmogramme_Integration_PhaseAbstract { gc_collect_cycles(); return $this; } + + + /** @category testing */ + public function noDbReset() { + $this->_db_reset = false; + return $this; + } } \ No newline at end of file diff --git a/library/Class/Cosmogramme/Integration/PhaseBatchs.php b/library/Class/Cosmogramme/Integration/PhaseBatchs.php index 8bb8714e42e..56106347c28 100644 --- a/library/Class/Cosmogramme/Integration/PhaseBatchs.php +++ b/library/Class/Cosmogramme/Integration/PhaseBatchs.php @@ -44,6 +44,8 @@ class Class_Cosmogramme_Integration_PhaseBatchs $this->_resetHttpClient() ->_runOne($batch); + + $this->_cleanMemory(); } } diff --git a/library/Class/Cosmogramme/Integration/PhaseItemFacets.php b/library/Class/Cosmogramme/Integration/PhaseItemFacets.php index 1c40d36af5d..829bef1673f 100644 --- a/library/Class/Cosmogramme/Integration/PhaseItemFacets.php +++ b/library/Class/Cosmogramme/Integration/PhaseItemFacets.php @@ -39,6 +39,8 @@ class Class_Cosmogramme_Integration_PhaseItemFacets public function _execute() { while ($records = Class_Notice::findAllAfter($this->_getData('pointeur_notice'), $this->_getData('pointeur'))) { + if ($this->isTimeOut()) + return $this->_phase; if (0 == ($this->_getData('nombre') % 100)) $this->_log->ecrire('<span class="vert"> ' . $this->_getData('nombre') . ' records updated </span>'); @@ -52,6 +54,7 @@ class Class_Cosmogramme_Integration_PhaseItemFacets $this->runUpdateForRecords($records); $this->_resetDbConnection(); + $this->_cleanMemory(); } Class_CosmoVar::setValueOf('date_maj_facettes', $this->_last_update_date); @@ -121,12 +124,4 @@ class Class_Cosmogramme_Integration_PhaseItemFacets protected function _wasRunning() { return $this->_getData('pointeur_notice') > 0; } - - - /** @category testing */ - public function noDbReset() { - $this->_db_reset = false; - return $this; - } } -?> \ No newline at end of file diff --git a/library/Class/Cosmogramme/Integration/PhaseOnDataSource.php b/library/Class/Cosmogramme/Integration/PhaseOnDataSource.php index c95a715ac56..ff2634035af 100644 --- a/library/Class/Cosmogramme/Integration/PhaseOnDataSource.php +++ b/library/Class/Cosmogramme/Integration/PhaseOnDataSource.php @@ -30,6 +30,7 @@ abstract class Class_Cosmogramme_Integration_PhaseOnDataSource extends Class_Cos return $this->_phase; $this->_runOne($integration); + $this->_cleanMemory(); } $this->_setData('pointeur', 0); diff --git a/library/Class/Cosmogramme/Integration/PhasePseudoRecord.php b/library/Class/Cosmogramme/Integration/PhasePseudoRecord.php index 8e91dc3e70a..3ead13c40dc 100644 --- a/library/Class/Cosmogramme/Integration/PhasePseudoRecord.php +++ b/library/Class/Cosmogramme/Integration/PhasePseudoRecord.php @@ -54,6 +54,7 @@ abstract class Class_Cosmogramme_Integration_PhasePseudoRecord return; $this->_runPage($models); + $this->_cleanMemory(); } $this->_summarize(); diff --git a/library/Class/Cosmogramme/Integration/PhaseReviews.php b/library/Class/Cosmogramme/Integration/PhaseReviews.php index 0492474e466..6a465b155e7 100644 --- a/library/Class/Cosmogramme/Integration/PhaseReviews.php +++ b/library/Class/Cosmogramme/Integration/PhaseReviews.php @@ -23,7 +23,6 @@ class Class_Cosmogramme_Integration_PhaseReviews extends Class_Cosmogramme_Integ const MY_ID=7.5; protected $_label = 'Reviews attachments'; - protected $_db_reset=true; public function __construct($phase, $log, $chrono) { parent::__construct($phase, $log, $chrono); @@ -52,7 +51,9 @@ class Class_Cosmogramme_Integration_PhaseReviews extends Class_Cosmogramme_Integ public function _execute() { $page = 1; while ($records = Class_AvisNotice::findAllBy(['order' => 'id', - 'limitPage' => [$page, 500]])) { + 'limitPage' => [$page, 100]])) { + if ($this->isTimeOut()) + return $this->_phase; if ($this->_log && (0 == ($this->_getData('nombre') % 100))) $this->_log->ecrire('<span class="vert"> ' . $this->_getData('nombre') . ' avis mis à jour </span>'); @@ -60,7 +61,7 @@ class Class_Cosmogramme_Integration_PhaseReviews extends Class_Cosmogramme_Integ $this->runUpdateForRecords($records); $this->_resetDbConnection(); - + $this->_cleanMemory(); $page++; } @@ -76,7 +77,6 @@ class Class_Cosmogramme_Integration_PhaseReviews extends Class_Cosmogramme_Integ $this->_log->ecrire($msg); } - } @@ -97,23 +97,4 @@ class Class_Cosmogramme_Integration_PhaseReviews extends Class_Cosmogramme_Integ $this->_incrementData('nombre'); } - - /** @category testing */ - public function noDbReset() { - $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/PhaseItemFacetsTest.php b/tests/library/Class/Cosmogramme/Integration/PhaseItemFacetsTest.php index e9557875b50..d7b9cc1f404 100644 --- a/tests/library/Class/Cosmogramme/Integration/PhaseItemFacetsTest.php +++ b/tests/library/Class/Cosmogramme/Integration/PhaseItemFacetsTest.php @@ -25,6 +25,7 @@ abstract class PhaseItemFacetsTestCase extends Class_Cosmogramme_Integration_Pha parent::setUp(); $this->_phase = $this->_buildPhase('ItemFacets') + ->setMemoryCleaner(function() {}) ->noDbReset() ->run(); } @@ -87,7 +88,7 @@ class PhaseItemFacetsExpectedPreviousPhaseTest extends PhaseItemFacetsTestCase { /** @test */ public function lastFacetsUpdateDateShouldBeSet() { - $this->assertNotEquals('', Class_CosmoVar::getValueOf('date_maj_facettes')); + $this->assertNotEmpty(Class_CosmoVar::getValueOf('date_maj_facettes')); } } @@ -111,4 +112,27 @@ abstract class PhaseItemFacetsCallbackTest extends PhaseItemFacetsTestCase { ['id' => 1, 'date_maj' => '2015-04-05 15:08:34'])]); } +} + + + +class PhaseItemFacetsExpectedExceptionPhaseTest extends PhaseItemFacetsTestCase { + protected function _getPreviousPhase() { + return (new Class_Cosmogramme_Integration_Phase(4)) + ->beCron(); + } + + + protected function _prepareFixtures() { + $this + ->onLoaderOfModel('Class_Notice') + ->whenCalled('findAllAfter') + ->answers('error'); + } + + + /** @test */ + public function logShouldContainsPhaseLabel() { + $this->assertLogContains('<h4>Mise à jour des facettes exemplaires</h4><span class="vert"> 0 records updated </span><p class="rouge">Erreur lors de l\'execution de la phase Mise à jour des facettes exemplaires : </br>Invalid argument supplied for foreach()</p>'); + } } \ No newline at end of file diff --git a/tests/library/Class/Cosmogramme/Integration/PhaseNoticeTestCase.php b/tests/library/Class/Cosmogramme/Integration/PhaseNoticeTestCase.php index 7095a3c7a52..0c8fd99ce43 100644 --- a/tests/library/Class/Cosmogramme/Integration/PhaseNoticeTestCase.php +++ b/tests/library/Class/Cosmogramme/Integration/PhaseNoticeTestCase.php @@ -28,7 +28,9 @@ class PhaseNoticeTestCase extends Class_Cosmogramme_Integration_PhaseTestCase { $this->fixture('Class_CosmoVar', ['id' => 'unimarc_zone_matiere', 'valeur' => '610a']); - $this->_phase = $this->_buildPhase('Notice')->run(); + $this->_phase = $this->_buildPhase('Notice') + ->setMemoryCleaner(function() {}) + ->run(); Class_Notice::clearCache(); Class_Exemplaire::clearCache(); } diff --git a/tests/library/Class/Cosmogramme/Integration/PhasePanierTest.php b/tests/library/Class/Cosmogramme/Integration/PhasePanierTest.php index e60477f5be7..443763cea4e 100644 --- a/tests/library/Class/Cosmogramme/Integration/PhasePanierTest.php +++ b/tests/library/Class/Cosmogramme/Integration/PhasePanierTest.php @@ -61,8 +61,9 @@ abstract class PhasePanierTestCase extends Class_Cosmogramme_Integration_PhaseTe parent::setUp(); $this->_phase = $this->_buildPhase('Panier') - ->setPrinter($this->_printer) - ->run(); + ->setMemoryCleaner(function() {}) + ->setPrinter($this->_printer) + ->run(); } } diff --git a/tests/library/Class/Cosmogramme/Integration/PhasePatronsTest.php b/tests/library/Class/Cosmogramme/Integration/PhasePatronsTest.php index cd123f52551..cdcd4dcbe7d 100644 --- a/tests/library/Class/Cosmogramme/Integration/PhasePatronsTest.php +++ b/tests/library/Class/Cosmogramme/Integration/PhasePatronsTest.php @@ -68,6 +68,7 @@ abstract class PhasePatronsTestCase extends Class_Cosmogramme_Integration_PhaseT public function setUp() { parent::setUp(); $this->_phase = $this->_buildPhase('Patrons') + ->setMemoryCleaner(function() {}) ->setPrinter($this->_printer); } } diff --git a/tests/library/Class/Cosmogramme/Integration/PhaseReservationTest.php b/tests/library/Class/Cosmogramme/Integration/PhaseReservationTest.php index 87e43bc6d98..083363609a6 100644 --- a/tests/library/Class/Cosmogramme/Integration/PhaseReservationTest.php +++ b/tests/library/Class/Cosmogramme/Integration/PhaseReservationTest.php @@ -27,7 +27,8 @@ abstract class PhaseReservationTestCase extends Class_Cosmogramme_Integration_Ph parent::setUp(); $this->_phase = $this->_buildPhase('Reservation') - ->run(); + ->setMemoryCleaner(function() {}) + ->run(); } diff --git a/tests/library/Class/Cosmogramme/Integration/PhaseReviewsTest.php b/tests/library/Class/Cosmogramme/Integration/PhaseReviewsTest.php index a5f97cd83d4..3e38c9ecb4e 100644 --- a/tests/library/Class/Cosmogramme/Integration/PhaseReviewsTest.php +++ b/tests/library/Class/Cosmogramme/Integration/PhaseReviewsTest.php @@ -25,6 +25,7 @@ abstract class PhaseReviewsTestCase extends Class_Cosmogramme_Integration_PhaseT parent::setUp(); $this->_phase = $this->_buildPhase('Reviews') + ->setMemoryCleaner(function() {}) ->noDbReset() ->run(); } -- GitLab