PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.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 5 years ago Scheduler.php 5 years ago Task.php 5 years ago TaskLoader.php 5 years ago Timetable.php 5 years ago
Timetable.php
152 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9
10 namespace Piwik\Scheduler;
11
12 use Piwik\Common;
13 use Piwik\Option;
14 use Piwik\Date;
15
16 /**
17 * This data structure holds the scheduled times for each active scheduled task.
18 */
19 class Timetable
20 {
21 const TIMETABLE_OPTION_STRING = "TaskScheduler.timetable";
22
23 private $timetable;
24
25 public function __construct()
26 {
27 $this->readFromOption();
28 }
29
30 public function getTimetable()
31 {
32 return $this->timetable;
33 }
34
35 public function setTimetable($timetable)
36 {
37 $this->timetable = $timetable;
38 }
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
57 public function getScheduledTaskNames()
58 {
59 return array_keys($this->timetable);
60 }
61
62 public function getScheduledTaskTime($taskName)
63 {
64 return isset($this->timetable[$taskName]) ? Date::factory($this->timetable[$taskName]) : false;
65 }
66
67 /**
68 * Checks if the task should be executed
69 *
70 * Task has to be executed if :
71 * - the task has already been scheduled once and the current system time is greater than the scheduled time.
72 * - execution is forced, see $forceTaskExecution
73 *
74 * @param string $taskName
75 *
76 * @return boolean
77 */
78 public function shouldExecuteTask($taskName)
79 {
80 $forceTaskExecution = (defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS);
81
82 if ($forceTaskExecution) {
83 return true;
84 }
85
86 return $this->taskHasBeenScheduledOnce($taskName) && time() >= $this->timetable[$taskName];
87 }
88
89 /**
90 * Checks if a task should be rescheduled
91 *
92 * Task has to be rescheduled if :
93 * - the task has to be executed
94 * - the task has never been scheduled before
95 *
96 * @param string $taskName
97 *
98 * @return boolean
99 */
100 public function taskShouldBeRescheduled($taskName)
101 {
102 return !$this->taskHasBeenScheduledOnce($taskName) || $this->shouldExecuteTask($taskName);
103 }
104
105 public function rescheduleTask(Task $task)
106 {
107 $rescheduledTime = $task->getRescheduledTime();
108
109 // update the scheduled time
110 $this->timetable[$task->getName()] = $rescheduledTime;
111 $this->save();
112
113 return Date::factory($rescheduledTime);
114 }
115
116 public function rescheduleTaskAndRunTomorrow(Task $task)
117 {
118 $tomorrow = Date::factory('tomorrow');
119
120 // update the scheduled time
121 $this->timetable[$task->getName()] = $tomorrow->getTimestamp();
122 $this->save();
123
124 return $tomorrow;
125 }
126
127 public function save()
128 {
129 Option::set(self::TIMETABLE_OPTION_STRING, serialize($this->timetable));
130 }
131
132 public function getScheduledTimeForMethod($className, $methodName, $methodParameter = null)
133 {
134 $taskName = Task::getTaskName($className, $methodName, $methodParameter);
135
136 return $this->taskHasBeenScheduledOnce($taskName) ? $this->timetable[$taskName] : false;
137 }
138
139 public function taskHasBeenScheduledOnce($taskName)
140 {
141 return isset($this->timetable[$taskName]);
142 }
143
144 public function readFromOption()
145 {
146 $optionData = Option::get(self::TIMETABLE_OPTION_STRING);
147 $unserializedTimetable = Common::safe_unserialize($optionData);
148
149 $this->timetable = $unserializedTimetable === false ? array() : $unserializedTimetable;
150 }
151 }
152