/** * @group Core * * @dataProvider testRunTasksTestCases */ public function testRunTasks($expectedTimetable, $expectedExecutedTasks, $timetableBeforeTaskExecution, $configuredTasks) { // temporarily unload plugins $plugins = \Piwik\Plugin\Manager::getInstance()->getLoadedPlugins(); $plugins = array_map(function ($p) { return $p->getPluginName(); }, $plugins); \Piwik\Plugin\Manager::getInstance()->unloadPlugins(); // make sure the get tasks event returns our configured tasks \Piwik\Piwik::addAction(TaskScheduler::GET_TASKS_EVENT, function (&$tasks) use($configuredTasks) { $tasks = $configuredTasks; }); // stub the piwik option object to control the returned option value self::stubPiwikOption(serialize($timetableBeforeTaskExecution)); TaskScheduler::unsetInstance(); // execute tasks $executionResults = TaskScheduler::runTasks(); // assert methods are executed $executedTasks = array(); foreach ($executionResults as $executionResult) { $executedTasks[] = $executionResult['task']; $this->assertNotEmpty($executionResult['output']); } $this->assertEquals($expectedExecutedTasks, $executedTasks); // assert the timetable is correctly updated $timetable = new ScheduledTaskTimetable(); $this->assertEquals($expectedTimetable, $timetable->getTimetable()); // restore loaded plugins & piwik options EventDispatcher::getInstance()->clearObservers(TaskScheduler::GET_TASKS_EVENT); \Piwik\Plugin\Manager::getInstance()->loadPlugins($plugins); self::resetPiwikOption(); }
/** * Will run all scheduled tasks due to run at this time. * * @return array */ public function runScheduledTasks() { Piwik::checkUserHasSuperUserAccess(); return TaskScheduler::runTasks(); }
/** * 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 (see misc/cron/archive.php) */ 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['isBrowserTriggerArchivingEnabled'])) { 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::isUserIsSuperUser(); // Scheduled tasks assume Super User is running Piwik::setUserIsSuperUser(); // While each plugins should ensure that necessary languages are loaded, // we ensure English translations at least are loaded Translate::loadEnglishTranslation(); $resultTasks = TaskScheduler::runTasks(); // restore original user privilege Piwik::setUserIsSuperUser($isSuperUser); 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'); }