PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.3
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Scheduler / Timetable.php
matomo / app / core / Scheduler Last commit date
Schedule 2 years ago RetryableException.php 2 years ago Scheduler.php 2 years ago Task.php 2 years ago TaskLoader.php 2 years ago Timetable.php 2 years ago
Timetable.php
204 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 *
9 */
10 namespace Piwik\Scheduler;
11
12 use Piwik\Common;
13 use Piwik\Option;
14 use Piwik\Date;
15 /**
16 * This data structure holds the scheduled times for each active scheduled task.
17 */
18 class Timetable
19 {
20 const TIMETABLE_OPTION_STRING = "TaskScheduler.timetable";
21 const RETRY_OPTION_STRING = "TaskScheduler.retryList";
22 private $timetable;
23 private $retryList;
24 public function __construct()
25 {
26 $this->readFromOption();
27 }
28 public function getTimetable()
29 {
30 return $this->timetable;
31 }
32 public function setTimetable($timetable)
33 {
34 $this->timetable = $timetable;
35 }
36 public function setRetryList($retryList)
37 {
38 $this->retryList = $retryList;
39 }
40 /**
41 * @param Task[] $activeTasks
42 */
43 public function removeInactiveTasks($activeTasks)
44 {
45 $activeTaskNames = array();
46 foreach ($activeTasks as $task) {
47 $activeTaskNames[] = $task->getName();
48 }
49 foreach (array_keys($this->timetable) as $taskName) {
50 if (!in_array($taskName, $activeTaskNames)) {
51 unset($this->timetable[$taskName]);
52 }
53 }
54 $this->save();
55 }
56 public function getScheduledTaskNames()
57 {
58 return array_keys($this->timetable);
59 }
60 public function getScheduledTaskTime($taskName)
61 {
62 return isset($this->timetable[$taskName]) ? Date::factory($this->timetable[$taskName]) : false;
63 }
64 /**
65 * Checks if the task should be executed
66 *
67 * Task has to be executed if :
68 * - the task has already been scheduled once and the current system time is greater than the scheduled time.
69 * - execution is forced, see $forceTaskExecution
70 *
71 * @param string $taskName
72 *
73 * @return boolean
74 */
75 public function shouldExecuteTask($taskName)
76 {
77 $forceTaskExecution = defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS;
78 if ($forceTaskExecution) {
79 return true;
80 }
81 return $this->taskHasBeenScheduledOnce($taskName) && time() >= $this->timetable[$taskName];
82 }
83 /**
84 * Checks if a task should be rescheduled
85 *
86 * Task has to be rescheduled if :
87 * - the task has to be executed
88 * - the task has never been scheduled before
89 *
90 * @param string $taskName
91 *
92 * @return boolean
93 */
94 public function taskShouldBeRescheduled($taskName)
95 {
96 return !$this->taskHasBeenScheduledOnce($taskName) || $this->shouldExecuteTask($taskName);
97 }
98 public function rescheduleTask(\Piwik\Scheduler\Task $task)
99 {
100 $rescheduledTime = $task->getRescheduledTime();
101 // update the scheduled time
102 $this->timetable[$task->getName()] = $rescheduledTime;
103 $this->save();
104 return Date::factory($rescheduledTime);
105 }
106 public function rescheduleTaskAndRunTomorrow(\Piwik\Scheduler\Task $task)
107 {
108 $tomorrow = Date::factory('tomorrow');
109 // update the scheduled time
110 $this->timetable[$task->getName()] = $tomorrow->getTimestamp();
111 $this->save();
112 return $tomorrow;
113 }
114 public function rescheduleTaskAndRunInOneHour(\Piwik\Scheduler\Task $task)
115 {
116 $oneHourFromNow = Date::factory('now')->addHour(1);
117 // update the scheduled time
118 $this->timetable[$task->getName()] = $oneHourFromNow->getTimestamp();
119 $this->save();
120 return $oneHourFromNow;
121 }
122 public function save()
123 {
124 Option::set(self::TIMETABLE_OPTION_STRING, serialize($this->timetable));
125 }
126 public function getScheduledTimeForMethod($className, $methodName, $methodParameter = null)
127 {
128 $taskName = \Piwik\Scheduler\Task::getTaskName($className, $methodName, $methodParameter);
129 return $this->taskHasBeenScheduledOnce($taskName) ? $this->timetable[$taskName] : false;
130 }
131 public function taskHasBeenScheduledOnce($taskName)
132 {
133 return isset($this->timetable[$taskName]);
134 }
135 public function readFromOption()
136 {
137 Option::clearCachedOption(self::TIMETABLE_OPTION_STRING);
138 $optionData = Option::get(self::TIMETABLE_OPTION_STRING);
139 $unserializedTimetable = Common::safe_unserialize($optionData);
140 $this->timetable = $unserializedTimetable === false ? array() : $unserializedTimetable;
141 }
142 /**
143 * Read the retry list option from the database
144 *
145 * @throws \Throwable
146 */
147 private function readRetryList()
148 {
149 Option::clearCachedOption(self::RETRY_OPTION_STRING);
150 $retryData = Option::get(self::RETRY_OPTION_STRING);
151 $unserializedRetryList = Common::safe_unserialize($retryData);
152 $this->retryList = $unserializedRetryList === false ? array() : $unserializedRetryList;
153 }
154 /**
155 * Save the retry list option to the database
156 */
157 public function saveRetryList()
158 {
159 Option::set(self::RETRY_OPTION_STRING, serialize($this->retryList));
160 }
161 /**
162 * Remove a task from the retry list
163 *
164 * @param string $taskName
165 */
166 public function clearRetryCount(string $taskName)
167 {
168 if (isset($this->retryList[$taskName])) {
169 unset($this->retryList[$taskName]);
170 $this->saveRetryList();
171 }
172 }
173 /**
174 * Increment the retry counter for a task
175 *
176 * @param string $taskName
177 */
178 public function incrementRetryCount(string $taskName)
179 {
180 $this->readRetryList();
181 if (!isset($this->retryList[$taskName])) {
182 $this->retryList[$taskName] = 0;
183 }
184 $this->retryList[$taskName]++;
185 $this->saveRetryList();
186 }
187 /**
188 * Return the current number of retries for a task
189 *
190 * @param string $taskName
191 *
192 * @return int
193 */
194 public function getRetryCount(string $taskName) : int
195 {
196 $this->readRetryList();
197 // Ignore excessive retry counts, workaround for SchedulerTest mock
198 if (!isset($this->retryList[$taskName]) || $this->retryList[$taskName] > 10000) {
199 return 0;
200 }
201 return $this->retryList[$taskName];
202 }
203 }
204