/** * Tracker requests will automatically trigger the Scheduled tasks. * This is useful for users who don't setup the cron, * but still want daily/weekly/monthly PDF reports emailed automatically. * * This is similar to calling the API CoreAdminHome.runScheduledTasks */ public function runScheduledTasks() { $now = time(); // Currently, there are no hourly tasks. When there are some, // this could be too aggressive minimum interval (some hours would be skipped in case of low traffic) $minimumInterval = TrackerConfig::getConfigValue('scheduled_tasks_min_interval'); // If the user disabled browser archiving, he has already setup a cron // To avoid parallel requests triggering the Scheduled Tasks, // Get last time tasks started executing $cache = Cache::getCacheGeneral(); if ($minimumInterval <= 0 || empty($cache['isBrowserTriggerEnabled'])) { Common::printDebug("-> Scheduled tasks not running in Tracker: Browser archiving is disabled."); return; } $nextRunTime = $cache['lastTrackerCronRun'] + $minimumInterval; if (defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS || $cache['lastTrackerCronRun'] === false || $nextRunTime < $now) { $cache['lastTrackerCronRun'] = $now; Cache::setCacheGeneral($cache); Option::set('lastTrackerCronRun', $cache['lastTrackerCronRun']); Common::printDebug('-> Scheduled Tasks: Starting...'); $invokeScheduledTasksUrl = "?module=API&format=csv&convertToUnicode=0&method=CoreAdminHome.runScheduledTasks&trigger=archivephp"; $cliMulti = new CliMulti(); $cliMulti->runAsSuperUser(); $responses = $cliMulti->request(array($invokeScheduledTasksUrl)); $resultTasks = reset($responses); Common::printDebug($resultTasks); Common::printDebug('Finished Scheduled Tasks.'); } else { Common::printDebug("-> Scheduled tasks not triggered."); } Common::printDebug("Next run will be from: " . date('Y-m-d H:i:s', $nextRunTime) . ' UTC'); }
public function test_getNumUsableCustomVariables_ShouldReadFromCacheIfPossible() { $cache = Cache::getCacheGeneral(); $cache['CustomVariables.NumUsableCustomVariables'] = 10; Cache::setCacheGeneral($cache); $this->assertSame(10, CustomVariables::getNumUsableCustomVariables()); }
public static function getMaxCustomVariables() { $cache = Cache::getCacheGeneral(); $cacheKey = 'CustomVariables.MaxNumCustomVariables'; if (!array_key_exists($cacheKey, $cache)) { $maxCustomVar = 0; foreach (Model::getScopes() as $scope) { $model = new Model($scope); $highestIndex = $model->getHighestCustomVarIndex(); if ($highestIndex > $maxCustomVar) { $maxCustomVar = $highestIndex; } } $cache[$cacheKey] = $maxCustomVar; Cache::setCacheGeneral($cache); } return $cache[$cacheKey]; }
/** * Returns the number of available custom variables that can be used. * * "Can be used" is identifed by the minimum number of available custom variables across all relevant tables. Eg * if there are 6 custom variables installed in log_visit but only 5 in log_conversion, we consider only 5 custom * variables as usable. * @return int */ public static function getNumUsableCustomVariables() { $cache = Cache::getCacheGeneral(); $cacheKey = 'CustomVariables.NumUsableCustomVariables'; if (!array_key_exists($cacheKey, $cache)) { $minCustomVar = null; foreach (Model::getScopes() as $scope) { $model = new Model($scope); $highestIndex = $model->getHighestCustomVarIndex(); if (!isset($minCustomVar)) { $minCustomVar = $highestIndex; } if ($highestIndex < $minCustomVar) { $minCustomVar = $highestIndex; } } if (!isset($minCustomVar)) { $minCustomVar = 0; } $cache[$cacheKey] = $minCustomVar; Cache::setCacheGeneral($cache); } return $cache[$cacheKey]; }
/** * Tracker requests will automatically trigger the Scheduled tasks. * This is useful for users who don't setup the cron, * but still want daily/weekly/monthly PDF reports emailed automatically. * * This is similar to calling the API CoreAdminHome.runScheduledTasks */ protected static function runScheduledTasks() { $now = time(); // Currently, there are no hourly tasks. When there are some, // this could be too aggressive minimum interval (some hours would be skipped in case of low traffic) $minimumInterval = Config::getInstance()->Tracker['scheduled_tasks_min_interval']; // If the user disabled browser archiving, he has already setup a cron // To avoid parallel requests triggering the Scheduled Tasks, // Get last time tasks started executing $cache = Cache::getCacheGeneral(); if ($minimumInterval <= 0 || empty($cache['isBrowserTriggerEnabled'])) { Common::printDebug("-> Scheduled tasks not running in Tracker: Browser archiving is disabled."); return; } $nextRunTime = $cache['lastTrackerCronRun'] + $minimumInterval; if (isset($GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS']) && $GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS'] || $cache['lastTrackerCronRun'] === false || $nextRunTime < $now) { $cache['lastTrackerCronRun'] = $now; Cache::setCacheGeneral($cache); self::initCorePiwikInTrackerMode(); Option::set('lastTrackerCronRun', $cache['lastTrackerCronRun']); Common::printDebug('-> Scheduled Tasks: Starting...'); // save current user privilege and temporarily assume Super User privilege $isSuperUser = Piwik::hasUserSuperUserAccess(); // Scheduled tasks assume Super User is running Piwik::setUserHasSuperUserAccess(); // While each plugins should ensure that necessary languages are loaded, // we ensure English translations at least are loaded Translate::loadEnglishTranslation(); ob_start(); CronArchive::$url = SettingsPiwik::getPiwikUrl(); $cronArchive = new CronArchive(); $cronArchive->runScheduledTasksInTrackerMode(); $resultTasks = ob_get_contents(); ob_clean(); // restore original user privilege Piwik::setUserHasSuperUserAccess($isSuperUser); foreach (explode('</pre>', $resultTasks) as $resultTask) { Common::printDebug(str_replace('<pre>', '', $resultTask)); } Common::printDebug('Finished Scheduled Tasks.'); } else { Common::printDebug("-> Scheduled tasks not triggered."); } Common::printDebug("Next run will be from: " . date('Y-m-d H:i:s', $nextRunTime) . ' UTC'); }
public function test_get_shouldReturnCurrentProvider_IfCurrentProviderIsSet() { Cache::setCacheGeneral(array('currentLocationProviderId' => MockLocationProvider::ID)); $geolocator = new VisitorGeolocator(); $this->assertEquals(MockLocationProvider::ID, $geolocator->getProvider()->getId()); }
public function test_save_shouldClearTrackerCacheEntries() { $this->setSuperUser(); Cache::setCacheGeneral(array('testSetting' => 1)); $this->assertArrayHasKey('testSetting', Cache::getCacheGeneral()); $this->addSystemSetting('mysystemsetting2', 'mytitle2'); $this->settings->save(); $this->assertArrayNotHasKey('testSetting', Cache::getCacheGeneral()); }