static public function runTasks() { // Gets the array where rescheduled timetables are stored $option = Piwik_GetOption(self::TIMETABLE_OPTION_STRING); $timetable = self::getTimetableFromOption($option); if($timetable === false) { return; } if(isset($GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS']) && $GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS']) { $timetable = array(); } // Collects tasks Piwik_PostEvent(self::GET_TASKS_EVENT, $tasks); $return = array(); // Loop through each task foreach ($tasks as $task) { $scheduledTime = $task->getScheduledTime(); $className = $task->getClassName(); $methodName = $task->getMethodName(); $fullyQualifiedMethodName = get_class($className) . '.' . $methodName; /* * Task has to be executed if : * - it is the first time, ie. rescheduledTime is not set * - that task has already been executed and the current system time is greater than the * rescheduled time. */ if ( !isset($timetable[$fullyQualifiedMethodName]) || (isset($timetable[$fullyQualifiedMethodName]) && time() >= $timetable[$fullyQualifiedMethodName]) ) { // Updates the rescheduled time $timetable[$fullyQualifiedMethodName] = $scheduledTime->getRescheduledTime(); Piwik_SetOption(self::TIMETABLE_OPTION_STRING, serialize($timetable)); // Run the task try { $timer = new Piwik_Timer; call_user_func ( array($className,$methodName) ); $message = $timer->__toString(); } catch(Exception $e) { $message = 'ERROR: '.$e->getMessage(); } $return[] = array('task' => $fullyQualifiedMethodName, 'output' => $message); } } return $return; }
/** * runTasks collects tasks defined within piwik plugins, runs them if they are scheduled and reschedules * the tasks that have been executed. * * @return array */ public static function runTasks() { // Gets the array where rescheduled timetables are stored $option = Piwik_GetOption(self::TIMETABLE_OPTION_STRING); $timetable = self::getTimetableFromOption($option); if ($timetable === false) { return; } $forceScheduledTasks = false; if (isset($GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS']) && $GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS'] || DEBUG_FORCE_SCHEDULED_TASKS) { $forceScheduledTasks = true; $timetable = array(); } // Collects tasks Piwik_PostEvent(self::GET_TASKS_EVENT, $tasks); $return = array(); // for every priority level, starting with the highest and concluding with the lowest for ($priority = Piwik_ScheduledTask::HIGHEST_PRIORITY; $priority <= Piwik_ScheduledTask::LOWEST_PRIORITY; ++$priority) { // Loop through each task foreach ($tasks as $task) { // if the task does not have the current priority level, don't execute it yet if ($task->getPriority() != $priority) { continue; } $scheduledTime = $task->getScheduledTime(); $className = $task->getClassName(); $methodName = $task->getMethodName(); $fullyQualifiedMethodName = get_class($className) . '.' . $methodName; /* * Task has to be executed if : * - it is the first time, ie. rescheduledTime is not set * - that task has already been executed and the current system time is greater than the * rescheduled time. */ if (!isset($timetable[$fullyQualifiedMethodName]) || isset($timetable[$fullyQualifiedMethodName]) && time() >= $timetable[$fullyQualifiedMethodName] || $forceScheduledTasks) { // Updates the rescheduled time $timetable[$fullyQualifiedMethodName] = $scheduledTime->getRescheduledTime(); Piwik_SetOption(self::TIMETABLE_OPTION_STRING, serialize($timetable)); self::$running = true; // Run the task try { $timer = new Piwik_Timer(); call_user_func(array($className, $methodName)); $message = $timer->__toString(); } catch (Exception $e) { $message = 'ERROR: ' . $e->getMessage(); } self::$running = false; $return[] = array('task' => $fullyQualifiedMethodName, 'output' => $message); } } } return $return; }
/** * @return bool True on success, false if some request failed */ private function archiveVisitsAndSegments($idsite, $period, $lastTimestampWebsiteProcessed, Piwik_Timer $timerWebsite = null) { $timer = new Piwik_Timer(); $aCurl = array(); $mh = curl_multi_init(); $url = $this->piwikUrl . $this->getVisitsRequestUrl($idsite, $period, $lastTimestampWebsiteProcessed) . $this->requestPrepend; // already processed above for "day" if ($period != "day") { $ch = $this->getNewCurlHandle($url); curl_multi_add_handle($mh, $ch); $aCurl[$url] = $ch; $this->requests++; } $urlNoSegment = $url; foreach ($this->segments as $segment) { $segmentUrl = $url . '&segment=' . urlencode($segment); $ch = $this->getNewCurlHandle($segmentUrl); curl_multi_add_handle($mh, $ch); $aCurl[$segmentUrl] = $ch; $this->requests++; } $running = null; do { usleep(10000); curl_multi_exec($mh, $running); } while ($running > 0); $success = true; $visitsAllDaysInPeriod = false; foreach ($aCurl as $url => $ch) { $content = curl_multi_getcontent($ch); $successResponse = $this->checkResponse($content, $url); $success = $successResponse && $success; if ($url == $urlNoSegment && $successResponse) { $stats = unserialize($content); if (!is_array($stats)) { $this->logError("Error unserializing the following response: " . $content); } $visitsAllDaysInPeriod = @array_sum($stats); } } foreach ($aCurl as $ch) { curl_multi_remove_handle($mh, $ch); } curl_multi_close($mh); $this->log("Archived website id = {$idsite}, period = {$period}, " . ($period != "day" ? (int) $visitsAllDaysInPeriod . " visits, " : "") . (!empty($timerWebsite) ? $timerWebsite->__toString() : $timer->__toString())); return $success; }