| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Scheduler; |
|
|
| use Piwik\Common; |
| use Piwik\Option; |
| use Piwik\Date; |
|
|
| |
| |
| |
| class Timetable |
| { |
| public const TIMETABLE_OPTION_STRING = "TaskScheduler.timetable"; |
| public const RETRY_OPTION_STRING = "TaskScheduler.retryList"; |
|
|
| private $timetable; |
| private $retryList; |
|
|
| public function __construct() |
| { |
| $this->readFromOption(); |
| } |
|
|
| public function getTimetable() |
| { |
| return $this->timetable; |
| } |
|
|
| public function setTimetable($timetable) |
| { |
| $this->timetable = $timetable; |
| } |
|
|
| public function setRetryList($retryList) |
| { |
| $this->retryList = $retryList; |
| } |
|
|
| |
| |
| |
| public function removeInactiveTasks($activeTasks) |
| { |
| $activeTaskNames = array(); |
| foreach ($activeTasks as $task) { |
| $activeTaskNames[] = $task->getName(); |
| } |
| foreach (array_keys($this->timetable) as $taskName) { |
| if (!in_array($taskName, $activeTaskNames)) { |
| unset($this->timetable[$taskName]); |
| } |
| } |
| $this->save(); |
| } |
|
|
| public function getScheduledTaskNames() |
| { |
| return array_keys($this->timetable); |
| } |
|
|
| public function getScheduledTaskTime($taskName) |
| { |
| return isset($this->timetable[$taskName]) ? Date::factory($this->timetable[$taskName]) : false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function shouldExecuteTask($taskName) |
| { |
| $forceTaskExecution = (defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS); |
|
|
| if ($forceTaskExecution) { |
| return true; |
| } |
|
|
| return $this->taskHasBeenScheduledOnce($taskName) && time() >= $this->timetable[$taskName]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function taskShouldBeRescheduled($taskName) |
| { |
| return !$this->taskHasBeenScheduledOnce($taskName) || $this->shouldExecuteTask($taskName); |
| } |
|
|
| public function rescheduleTask(Task $task) |
| { |
| $rescheduledTime = $task->getRescheduledTime(); |
|
|
| |
| $this->timetable[$task->getName()] = $rescheduledTime; |
| $this->save(); |
|
|
| return Date::factory($rescheduledTime); |
| } |
|
|
| public function rescheduleTaskAndRunTomorrow(Task $task) |
| { |
| $tomorrow = Date::factory('tomorrow'); |
|
|
| |
| $this->timetable[$task->getName()] = $tomorrow->getTimestamp(); |
| $this->save(); |
|
|
| return $tomorrow; |
| } |
|
|
| public function rescheduleTaskAndRunInOneHour(Task $task) |
| { |
| $oneHourFromNow = Date::factory('now')->addHour(1); |
|
|
| |
| $this->timetable[$task->getName()] = $oneHourFromNow->getTimestamp(); |
| $this->save(); |
|
|
| return $oneHourFromNow; |
| } |
|
|
| public function save() |
| { |
| Option::set(self::TIMETABLE_OPTION_STRING, serialize($this->timetable)); |
| } |
|
|
| public function getScheduledTimeForMethod($className, $methodName, $methodParameter = null) |
| { |
| $taskName = Task::getTaskName($className, $methodName, $methodParameter); |
|
|
| return $this->taskHasBeenScheduledOnce($taskName) ? $this->timetable[$taskName] : false; |
| } |
|
|
| public function taskHasBeenScheduledOnce($taskName) |
| { |
| return isset($this->timetable[$taskName]); |
| } |
|
|
| public function readFromOption() |
| { |
| Option::clearCachedOption(self::TIMETABLE_OPTION_STRING); |
| $optionData = Option::get(self::TIMETABLE_OPTION_STRING); |
| $unserializedTimetable = Common::safe_unserialize($optionData); |
|
|
| $this->timetable = $unserializedTimetable === false ? array() : $unserializedTimetable; |
| } |
|
|
| |
| |
| |
| |
| |
| private function readRetryList() |
| { |
| Option::clearCachedOption(self::RETRY_OPTION_STRING); |
| $retryData = Option::get(self::RETRY_OPTION_STRING); |
| $unserializedRetryList = Common::safe_unserialize($retryData); |
|
|
| $this->retryList = $unserializedRetryList === false ? array() : $unserializedRetryList; |
| } |
|
|
| |
| |
| |
| public function saveRetryList() |
| { |
| Option::set(self::RETRY_OPTION_STRING, serialize($this->retryList)); |
| } |
|
|
| |
| |
| |
| public function clearRetryCount(string $taskName) |
| { |
| if (isset($this->retryList[$taskName])) { |
| unset($this->retryList[$taskName]); |
| $this->saveRetryList(); |
| } |
| } |
|
|
| |
| |
| |
| public function incrementRetryCount(string $taskName) |
| { |
| $this->readRetryList(); |
| if (!isset($this->retryList[$taskName])) { |
| $this->retryList[$taskName] = 0; |
| } |
| $this->retryList[$taskName]++; |
| $this->saveRetryList(); |
| } |
|
|
| |
| |
| |
| public function getRetryCount(string $taskName): int |
| { |
| $this->readRetryList(); |
|
|
| |
| if (!isset($this->retryList[$taskName]) || $this->retryList[$taskName] > 10000) { |
| return 0; |
| } |
|
|
| return $this->retryList[$taskName]; |
| } |
| } |
|
|