PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
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 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