Schedule
1 month ago
RetryableException.php
2 years ago
ScheduledTaskLock.php
2 years ago
Scheduler.php
1 year ago
Task.php
1 month ago
TaskLoader.php
6 months ago
Timetable.php
1 month ago
Timetable.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Scheduler; |
| 10 | |
| 11 | use Piwik\Common; |
| 12 | use Piwik\Option; |
| 13 | use Piwik\Date; |
| 14 | /** |
| 15 | * This data structure holds the scheduled times for each active scheduled task. |
| 16 | */ |
| 17 | class Timetable |
| 18 | { |
| 19 | public const TIMETABLE_OPTION_STRING = "TaskScheduler.timetable"; |
| 20 | public const RETRY_OPTION_STRING = "TaskScheduler.retryList"; |
| 21 | private $timetable; |
| 22 | private $retryList; |
| 23 | public function __construct() |
| 24 | { |
| 25 | $this->readFromOption(); |
| 26 | } |
| 27 | public function getTimetable() |
| 28 | { |
| 29 | return $this->timetable; |
| 30 | } |
| 31 | public function setTimetable($timetable) |
| 32 | { |
| 33 | $this->timetable = $timetable; |
| 34 | } |
| 35 | public function setRetryList($retryList) |
| 36 | { |
| 37 | $this->retryList = $retryList; |
| 38 | } |
| 39 | /** |
| 40 | * @param Task[] $activeTasks |
| 41 | */ |
| 42 | public function removeInactiveTasks($activeTasks) |
| 43 | { |
| 44 | $activeTaskNames = array(); |
| 45 | foreach ($activeTasks as $task) { |
| 46 | $activeTaskNames[] = $task->getName(); |
| 47 | } |
| 48 | foreach (array_keys($this->timetable) as $taskName) { |
| 49 | if (!in_array($taskName, $activeTaskNames)) { |
| 50 | unset($this->timetable[$taskName]); |
| 51 | } |
| 52 | } |
| 53 | $this->save(); |
| 54 | } |
| 55 | public function getScheduledTaskNames() |
| 56 | { |
| 57 | return array_keys($this->timetable); |
| 58 | } |
| 59 | public function getScheduledTaskTime($taskName) |
| 60 | { |
| 61 | return isset($this->timetable[$taskName]) ? Date::factory($this->timetable[$taskName]) : \false; |
| 62 | } |
| 63 | /** |
| 64 | * Checks if the task should be executed |
| 65 | * |
| 66 | * Task has to be executed if : |
| 67 | * - the task has already been scheduled once and the current system time is greater than the scheduled time. |
| 68 | * - execution is forced, see $forceTaskExecution |
| 69 | * |
| 70 | * @param string $taskName |
| 71 | * |
| 72 | * @return boolean |
| 73 | */ |
| 74 | public function shouldExecuteTask($taskName) |
| 75 | { |
| 76 | $forceTaskExecution = defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS; |
| 77 | if ($forceTaskExecution) { |
| 78 | return \true; |
| 79 | } |
| 80 | return $this->taskHasBeenScheduledOnce($taskName) && time() >= $this->timetable[$taskName]; |
| 81 | } |
| 82 | /** |
| 83 | * Checks if a task should be rescheduled |
| 84 | * |
| 85 | * Task has to be rescheduled if : |
| 86 | * - the task has to be executed |
| 87 | * - the task has never been scheduled before |
| 88 | * |
| 89 | * @param string $taskName |
| 90 | * |
| 91 | * @return boolean |
| 92 | */ |
| 93 | public function taskShouldBeRescheduled($taskName) |
| 94 | { |
| 95 | return !$this->taskHasBeenScheduledOnce($taskName) || $this->shouldExecuteTask($taskName); |
| 96 | } |
| 97 | public function rescheduleTask(\Piwik\Scheduler\Task $task) |
| 98 | { |
| 99 | $rescheduledTime = $task->getRescheduledTime(); |
| 100 | // update the scheduled time |
| 101 | $this->timetable[$task->getName()] = $rescheduledTime; |
| 102 | $this->save(); |
| 103 | return Date::factory($rescheduledTime); |
| 104 | } |
| 105 | public function rescheduleTaskAndRunTomorrow(\Piwik\Scheduler\Task $task) |
| 106 | { |
| 107 | $tomorrow = Date::factory('tomorrow'); |
| 108 | // update the scheduled time |
| 109 | $this->timetable[$task->getName()] = $tomorrow->getTimestamp(); |
| 110 | $this->save(); |
| 111 | return $tomorrow; |
| 112 | } |
| 113 | public function rescheduleTaskAndRunInOneHour(\Piwik\Scheduler\Task $task) |
| 114 | { |
| 115 | $oneHourFromNow = Date::factory('now')->addHour(1); |
| 116 | // update the scheduled time |
| 117 | $this->timetable[$task->getName()] = $oneHourFromNow->getTimestamp(); |
| 118 | $this->save(); |
| 119 | return $oneHourFromNow; |
| 120 | } |
| 121 | public function save() |
| 122 | { |
| 123 | Option::set(self::TIMETABLE_OPTION_STRING, serialize($this->timetable)); |
| 124 | } |
| 125 | public function getScheduledTimeForMethod($className, $methodName, $methodParameter = null) |
| 126 | { |
| 127 | $taskName = \Piwik\Scheduler\Task::getTaskName($className, $methodName, $methodParameter); |
| 128 | return $this->taskHasBeenScheduledOnce($taskName) ? $this->timetable[$taskName] : \false; |
| 129 | } |
| 130 | public function taskHasBeenScheduledOnce($taskName) |
| 131 | { |
| 132 | return isset($this->timetable[$taskName]); |
| 133 | } |
| 134 | public function readFromOption() |
| 135 | { |
| 136 | Option::clearCachedOption(self::TIMETABLE_OPTION_STRING); |
| 137 | $optionData = Option::get(self::TIMETABLE_OPTION_STRING); |
| 138 | $unserializedTimetable = Common::safe_unserialize($optionData); |
| 139 | $this->timetable = $unserializedTimetable === \false ? array() : $unserializedTimetable; |
| 140 | } |
| 141 | /** |
| 142 | * Read the retry list option from the database |
| 143 | * |
| 144 | * @throws \Throwable |
| 145 | */ |
| 146 | private function readRetryList() |
| 147 | { |
| 148 | Option::clearCachedOption(self::RETRY_OPTION_STRING); |
| 149 | $retryData = Option::get(self::RETRY_OPTION_STRING); |
| 150 | $unserializedRetryList = Common::safe_unserialize($retryData); |
| 151 | $this->retryList = $unserializedRetryList === \false ? array() : $unserializedRetryList; |
| 152 | } |
| 153 | /** |
| 154 | * Save the retry list option to the database |
| 155 | */ |
| 156 | public function saveRetryList() |
| 157 | { |
| 158 | Option::set(self::RETRY_OPTION_STRING, serialize($this->retryList)); |
| 159 | } |
| 160 | /** |
| 161 | * Remove a task from the retry list |
| 162 | */ |
| 163 | public function clearRetryCount(string $taskName) |
| 164 | { |
| 165 | if (isset($this->retryList[$taskName])) { |
| 166 | unset($this->retryList[$taskName]); |
| 167 | $this->saveRetryList(); |
| 168 | } |
| 169 | } |
| 170 | /** |
| 171 | * Increment the retry counter for a task |
| 172 | */ |
| 173 | public function incrementRetryCount(string $taskName) |
| 174 | { |
| 175 | $this->readRetryList(); |
| 176 | if (!isset($this->retryList[$taskName])) { |
| 177 | $this->retryList[$taskName] = 0; |
| 178 | } |
| 179 | $this->retryList[$taskName]++; |
| 180 | $this->saveRetryList(); |
| 181 | } |
| 182 | /** |
| 183 | * Return the current number of retries for a task |
| 184 | */ |
| 185 | public function getRetryCount(string $taskName) : int |
| 186 | { |
| 187 | $this->readRetryList(); |
| 188 | // Ignore excessive retry counts, workaround for SchedulerTest mock |
| 189 | if (!isset($this->retryList[$taskName]) || $this->retryList[$taskName] > 10000) { |
| 190 | return 0; |
| 191 | } |
| 192 | return $this->retryList[$taskName]; |
| 193 | } |
| 194 | } |
| 195 |