diff --git a/core/ArchiveProcessor/FixedSiteIds.php b/core/ArchiveProcessor/FixedSiteIds.php index 5bf54ccc3f9..8e70f9b18ae 100644 --- a/core/ArchiveProcessor/FixedSiteIds.php +++ b/core/ArchiveProcessor/FixedSiteIds.php @@ -20,7 +20,9 @@ class FixedSiteIds public function __construct($websiteIds) { - $this->siteIds = $websiteIds; + if (!empty($websiteIds)) { + $this->siteIds = $websiteIds; + } } /** @@ -40,7 +42,13 @@ public function getNumSites() */ public function getNumProcessedWebsites() { - return $this->index + 1; + $numProcessed = $this->index + 1; + + if ($numProcessed > $this->getNumSites()) { + return $this->getNumSites(); + } + + return $numProcessed; } public function getNextSiteId() diff --git a/core/ArchiveProcessor/SharedSiteIds.php b/core/ArchiveProcessor/SharedSiteIds.php index 84d6b6dcff5..0deeda80bb0 100644 --- a/core/ArchiveProcessor/SharedSiteIds.php +++ b/core/ArchiveProcessor/SharedSiteIds.php @@ -20,9 +20,14 @@ class SharedSiteIds { private $siteIds = array(); private $currentSiteId; + private $done = false; public function __construct($websiteIds) { + if (empty($websiteIds)) { + $websiteIds = array(); + } + $self = $this; $this->siteIds = $this->runExclusive(function () use ($self, $websiteIds) { // if there are already sites to be archived registered, prefer the list of existing archive, meaning help @@ -56,6 +61,10 @@ public function getNumSites() */ public function getNumProcessedWebsites() { + if ($this->done) { + return $this->getNumSites(); + } + if (empty($this->currentSiteId)) { return 0; } @@ -103,6 +112,7 @@ public function getAllSiteIdsToArchive() private function runExclusive($closure) { $process = new Process('archive.sharedsiteids'); + while ($process->isRunning() && $process->getSecondsSinceCreation() < 5) { // wait max 5 seconds, such an operation should not take longer usleep(25); @@ -145,6 +155,10 @@ public function getNextSiteId() return $nextSiteId; }); + if (is_null($this->currentSiteId)) { + $this->done = true; + } + return $this->currentSiteId; } diff --git a/core/CronArchive.php b/core/CronArchive.php index 2c5cc4fd964..f14545ee01a 100644 --- a/core/CronArchive.php +++ b/core/CronArchive.php @@ -284,7 +284,8 @@ public function run() // Also reprocess when day has ended since last run if ($dayHasEndedMustReprocess - && !$this->hasBeenProcessedSinceMidnight($idsite, $lastTimestampWebsiteProcessedDay) // it might have reprocessed for that day by another cron + // it might have reprocessed for that day by another cron + && !$this->hasBeenProcessedSinceMidnight($idsite, $lastTimestampWebsiteProcessedDay) && !$existingArchiveIsValid) { $skipDayArchive = false; } diff --git a/tests/PHPUnit/Core/ArchiveProcessor/FixedSiteIdsTest.php b/tests/PHPUnit/Core/ArchiveProcessor/FixedSiteIdsTest.php new file mode 100644 index 00000000000..6d0c6cf78eb --- /dev/null +++ b/tests/PHPUnit/Core/ArchiveProcessor/FixedSiteIdsTest.php @@ -0,0 +1,55 @@ +fixedSiteIds = new FixedSiteIds(array(1,2,5,9)); + } + + public function test_construct_withEmptyValue() + { + $siteIds = new FixedSiteIds(null); + $this->assertEquals(0, $siteIds->getNumSites()); + $this->assertNull($siteIds->getNextSiteId()); + } + + public function test_getNumSites() + { + $this->assertEquals(4, $this->fixedSiteIds->getNumSites()); + } + + public function test_getNumProcessedWebsites_getNextSiteId() + { + $this->assertEquals(0, $this->fixedSiteIds->getNumProcessedWebsites()); + $this->assertEquals(1, $this->fixedSiteIds->getNextSiteId()); + $this->assertEquals(1, $this->fixedSiteIds->getNumProcessedWebsites()); + $this->assertEquals(2, $this->fixedSiteIds->getNextSiteId()); + $this->assertEquals(2, $this->fixedSiteIds->getNumProcessedWebsites()); + $this->assertEquals(5, $this->fixedSiteIds->getNextSiteId()); + $this->assertEquals(3, $this->fixedSiteIds->getNumProcessedWebsites()); + $this->assertEquals(9, $this->fixedSiteIds->getNextSiteId()); + $this->assertEquals(4, $this->fixedSiteIds->getNumProcessedWebsites()); + + $this->assertNull($this->fixedSiteIds->getNextSiteId()); + $this->assertEquals(4, $this->fixedSiteIds->getNumProcessedWebsites()); + } + + +} \ No newline at end of file diff --git a/tests/PHPUnit/Core/ArchiveProcessor/SharedSiteIdsTest.php b/tests/PHPUnit/Core/ArchiveProcessor/SharedSiteIdsTest.php new file mode 100644 index 00000000000..e8df2c9d233 --- /dev/null +++ b/tests/PHPUnit/Core/ArchiveProcessor/SharedSiteIdsTest.php @@ -0,0 +1,108 @@ +sharedSiteIds = new SharedSiteIds(array(1,2,5,9)); + } + + public function tearDown() + { + $this->sharedSiteIds->setSiteIdsToArchive(array()); + } + + public function test_construct_withEmptyValue() + { + $this->sharedSiteIds->setSiteIdsToArchive(array()); + + $siteIds = new SharedSiteIds(null); + $this->assertEquals(0, $siteIds->getNumSites()); + $this->assertNull($siteIds->getNextSiteId()); + } + + public function test_isSupported() + { + $this->assertTrue(SharedSiteIds::isSupported()); + } + + public function test_getNumSites() + { + $this->assertEquals(4, $this->sharedSiteIds->getNumSites()); + } + + public function test_getAllSiteIdsToArchive() + { + $this->assertEquals(array(1,2,5,9), $this->sharedSiteIds->getAllSiteIdsToArchive()); + } + + public function test_getNumProcessedWebsites_getNextSiteId() + { + $this->assertEquals(0, $this->sharedSiteIds->getNumProcessedWebsites()); + + $this->assertEquals(1, $this->sharedSiteIds->getNextSiteId()); + $this->assertEquals(1, $this->sharedSiteIds->getNumProcessedWebsites()); + + $this->assertEquals(2, $this->sharedSiteIds->getNextSiteId()); + $this->assertEquals(2, $this->sharedSiteIds->getNumProcessedWebsites()); + + $this->assertEquals(5, $this->sharedSiteIds->getNextSiteId()); + $this->assertEquals(3, $this->sharedSiteIds->getNumProcessedWebsites()); + + $this->assertEquals(9, $this->sharedSiteIds->getNextSiteId()); + $this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites()); + + $this->assertNull($this->sharedSiteIds->getNextSiteId()); + $this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites()); + } + + public function test_usingMultipleSharedSiteIds() + { + $second = new SharedSiteIds(array(7,9,11,6,1,2)); + + // should ignore his queue and help processing the existing queue + $this->assertEquals(4, $second->getNumSites()); + $this->assertEquals(4, $this->sharedSiteIds->getNumSites()); + + $this->assertEquals(array(1,2,5,9), $second->getAllSiteIdsToArchive()); + $this->assertEquals(1, $second->getNextSiteId()); + $this->assertEquals(1, $second->getNumProcessedWebsites()); + + $this->assertEquals(array(2,5,9), $this->sharedSiteIds->getAllSiteIdsToArchive()); + $this->assertEquals(2, $this->sharedSiteIds->getNextSiteId()); + $this->assertEquals(2, $this->sharedSiteIds->getNumProcessedWebsites()); + + $this->assertEquals(array(5,9), $second->getAllSiteIdsToArchive()); + $this->assertEquals(5, $second->getNextSiteId()); + $this->assertEquals(3, $second->getNumProcessedWebsites()); + + $this->assertEquals(array(9), $this->sharedSiteIds->getAllSiteIdsToArchive()); + $this->assertEquals(9, $this->sharedSiteIds->getNextSiteId()); + $this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites()); + + $this->assertNull($second->getNextSiteId()); + $this->assertEquals(4, $second->getNumProcessedWebsites()); + $this->assertEquals(array(), $second->getAllSiteIdsToArchive()); + + $this->assertNull($this->sharedSiteIds->getNextSiteId()); + $this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites()); + $this->assertEquals(array(), $this->sharedSiteIds->getAllSiteIdsToArchive()); + } + +} \ No newline at end of file