Skip to content

Commit

Permalink
refs matomo-org#4903 added some comments and better naming
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Mar 25, 2014
1 parent 5fcbc09 commit 3827424
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
10 changes: 10 additions & 0 deletions core/ArchiveProcessor/FixedSiteIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ public function __construct($websiteIds)
$this->siteIds = $websiteIds;
}

/**
* Get the number of total websites that needs to be processed.
*
* @return int
*/
public function getNumSites()
{
return count($this->siteIds);
}

/**
* Get the number of already processed websites. All websites were processed by the current archiver.
*
* @return int
*/
public function getNumProcessedWebsites()
{
return $this->index + 1;
Expand Down
31 changes: 26 additions & 5 deletions core/ArchiveProcessor/SharedSiteIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
use Piwik\Option;
use Piwik\CliMulti\Process;

/**
* This class saves all to be processed siteIds in an Option named 'SharedSiteIdsToArchive' and processes all sites
* within that list. If a user starts multiple archiver those archiver will help to finish processing that list.
*/
class SharedSiteIds
{
private $siteIds = array();
Expand All @@ -21,6 +25,8 @@ public function __construct($websiteIds)
{
$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
// to finish this queue of sites instead of starting a new queue
$existingWebsiteIds = $self->getAllSiteIdsToArchive();

if (!empty($existingWebsiteIds)) {
Expand All @@ -33,11 +39,21 @@ public function __construct($websiteIds)
});
}

/**
* Get the number of total websites that needs to be processed.
*
* @return int
*/
public function getNumSites()
{
return count($this->siteIds);
}

/**
* Get the number of already processed websites (not necessarily all of those where processed by this archiver).
*
* @return int
*/
public function getNumProcessedWebsites()
{
if (empty($this->currentSiteId)) {
Expand All @@ -56,16 +72,16 @@ public function getNumProcessedWebsites()
public function setSiteIdsToArchive($siteIds)
{
if (!empty($siteIds)) {
Option::set('SiteIdsToArchive', implode(',', $siteIds));
Option::set('SharedSiteIdsToArchive', implode(',', $siteIds));
} else {
Option::delete('SiteIdsToArchive');
Option::delete('SharedSiteIdsToArchive');
}
}

public function getAllSiteIdsToArchive()
{
Option::clearCachedOption('SiteIdsToArchive');
$siteIdsToArchive = Option::get('SiteIdsToArchive');
Option::clearCachedOption('SharedSiteIdsToArchive');
$siteIdsToArchive = Option::get('SharedSiteIdsToArchive');

if (empty($siteIdsToArchive)) {
return array();
Expand All @@ -86,7 +102,7 @@ public function getAllSiteIdsToArchive()
*/
private function runExclusive($closure)
{
$process = new Process('archive.lock');
$process = new Process('archive.sharedsiteids');
while ($process->isRunning() && $process->getSecondsSinceCreation() < 5) {
// wait max 5 seconds, such an operation should not take longer
usleep(25);
Expand All @@ -106,6 +122,11 @@ private function runExclusive($closure)
return $result;
}

/**
* Get the next site id that needs to be processed or null if all site ids where processed.
*
* @return int|null
*/
public function getNextSiteId()
{
$self = $this;
Expand Down
2 changes: 1 addition & 1 deletion core/CronArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function init()
$this->websites = new SharedSiteIds($websitesIds);
}

if($this->shouldStartProfiler) {
if ($this->shouldStartProfiler) {
\Piwik\Profiler::setupProfilerXHProf($mainRun = true);
$this->log("XHProf profiling is enabled.");
}
Expand Down

0 comments on commit 3827424

Please sign in to comment.