diff options
Diffstat (limited to 'plugins/CoreAdminHome/Tasks.php')
-rw-r--r-- | plugins/CoreAdminHome/Tasks.php | 63 |
1 files changed, 59 insertions, 4 deletions
diff --git a/plugins/CoreAdminHome/Tasks.php b/plugins/CoreAdminHome/Tasks.php index d633b9fd5d..700c7d69c2 100644 --- a/plugins/CoreAdminHome/Tasks.php +++ b/plugins/CoreAdminHome/Tasks.php @@ -8,13 +8,28 @@ */ namespace Piwik\Plugins\CoreAdminHome; -use Piwik\DataAccess\ArchivePurger; +use Piwik\ArchiveProcessor\Rules; +use Piwik\Archive\ArchivePurger; +use Piwik\Container\StaticContainer; use Piwik\DataAccess\ArchiveTableCreator; use Piwik\Date; use Piwik\Db; +use Piwik\Log; +use Piwik\Plugins\CoreAdminHome\Tasks\ArchivesToPurgeDistributedList; +use Piwik\SettingsServer; class Tasks extends \Piwik\Plugin\Tasks { + /** + * @var ArchivePurger + */ + private $archivePurger; + + public function __construct(ArchivePurger $archivePurger = null) + { + $this->archivePurger = $archivePurger ?: new ArchivePurger(); + } + public function schedule() { // general data purge on older archive tables, executed daily @@ -29,21 +44,47 @@ class Tasks extends \Piwik\Plugin\Tasks public function purgeOutdatedArchives() { + $logger = StaticContainer::get('Psr\Log\LoggerInterface'); + + if ($this->willPurgingCausePotentialProblemInUI()) { + $logger->info("Purging temporary archives: skipped (browser triggered archiving not enabled & not running after core:archive)"); + return false; + } + $archiveTables = ArchiveTableCreator::getTablesArchivesInstalled(); + + $logger->info("Purging archives in {tableCount} archive tables.", array('tableCount' => count($archiveTables))); + + // keep track of dates we purge for, since getTablesArchivesInstalled() will return numeric & blob + // tables (so dates will appear two times, and we should only purge once per date) + $datesPurged = array(); + foreach ($archiveTables as $table) { $date = ArchiveTableCreator::getDateFromTableName($table); list($year, $month) = explode('_', $date); // Somehow we may have archive tables created with older dates, prevent exception from being thrown - if ($year > 1990) { - ArchivePurger::purgeOutdatedArchives(Date::factory("$year-$month-15")); + if ($year > 1990 + && empty($datesPurged[$date]) + ) { + $dateObj = Date::factory("$year-$month-15"); + + $this->archivePurger->purgeOutdatedArchives($dateObj); + $this->archivePurger->purgeArchivesWithPeriodRange($dateObj); + + $datesPurged[$date] = true; } } } public function purgeInvalidatedArchives() { - ArchivePurger::purgeInvalidatedArchives(); + $archivesToPurge = new ArchivesToPurgeDistributedList(); + foreach ($archivesToPurge->getAllAsDates() as $date) { + $this->archivePurger->purgeInvalidatedArchivesFrom($date); + + $archivesToPurge->removeDate($date); + } } public function optimizeArchiveTable() @@ -51,4 +92,18 @@ class Tasks extends \Piwik\Plugin\Tasks $archiveTables = ArchiveTableCreator::getTablesArchivesInstalled(); Db::optimizeTables($archiveTables); } + + /** + * we should only purge outdated & custom range archives if we know cron archiving has just run, + * or if browser triggered archiving is enabled. if cron archiving has run, then we know the latest + * archives are in the database, and we can remove temporary ones. if browser triggered archiving is + * enabled, then we know any archives that are wrongly purged, can be re-archived on demand. + * this prevents some situations where "no data" is displayed for reports that should have data. + * + * @return bool + */ + private function willPurgingCausePotentialProblemInUI() + { + return !Rules::isRequestAuthorizedToArchive(); + } }
\ No newline at end of file |