PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
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 6 years ago Scheduler.php 6 years ago Task.php 6 years ago TaskLoader.php 6 years ago Timetable.php 6 years ago
Timetable.php
147 lines
1 <?php
2 /**
3 * Piwik - 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 $optionData = Option::get(self::TIMETABLE_OPTION_STRING);
28 $unserializedTimetable = Common::safe_unserialize($optionData);
29
30 $this->timetable = $unserializedTimetable === false ? array() : $unserializedTimetable;
31 }
32
33 public function getTimetable()
34 {
35 return $this->timetable;
36 }
37
38 public function setTimetable($timetable)
39 {
40 $this->timetable = $timetable;
41 }
42
43 /**
44 * @param Task[] $activeTasks
45 */
46 public function removeInactiveTasks($activeTasks)
47 {
48 $activeTaskNames = array();
49 foreach ($activeTasks as $task) {
50 $activeTaskNames[] = $task->getName();
51 }
52 foreach (array_keys($this->timetable) as $taskName) {
53 if (!in_array($taskName, $activeTaskNames)) {
54 unset($this->timetable[$taskName]);
55 }
56 }
57 $this->save();
58 }
59
60 public function getScheduledTaskNames()
61 {
62 return array_keys($this->timetable);
63 }
64
65 public function getScheduledTaskTime($taskName)
66 {
67 return isset($this->timetable[$taskName]) ? Date::factory($this->timetable[$taskName]) : false;
68 }
69
70 /**
71 * Checks if the task should be executed
72 *
73 * Task has to be executed if :
74 * - the task has already been scheduled once and the current system time is greater than the scheduled time.
75 * - execution is forced, see $forceTaskExecution
76 *
77 * @param string $taskName
78 *
79 * @return boolean
80 */
81 public function shouldExecuteTask($taskName)
82 {
83 $forceTaskExecution = (defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS);
84
85 if ($forceTaskExecution) {
86 return true;
87 }
88
89 return $this->taskHasBeenScheduledOnce($taskName) && time() >= $this->timetable[$taskName];
90 }
91
92 /**
93 * Checks if a task should be rescheduled
94 *
95 * Task has to be rescheduled if :
96 * - the task has to be executed
97 * - the task has never been scheduled before
98 *
99 * @param string $taskName
100 *
101 * @return boolean
102 */
103 public function taskShouldBeRescheduled($taskName)
104 {
105 return !$this->taskHasBeenScheduledOnce($taskName) || $this->shouldExecuteTask($taskName);
106 }
107
108 public function rescheduleTask(Task $task)
109 {
110 $rescheduledTime = $task->getRescheduledTime();
111
112 // update the scheduled time
113 $this->timetable[$task->getName()] = $rescheduledTime;
114 $this->save();
115
116 return Date::factory($rescheduledTime);
117 }
118
119 public function rescheduleTaskAndRunTomorrow(Task $task)
120 {
121 $tomorrow = Date::factory('tomorrow');
122
123 // update the scheduled time
124 $this->timetable[$task->getName()] = $tomorrow->getTimestamp();
125 $this->save();
126
127 return $tomorrow;
128 }
129
130 public function save()
131 {
132 Option::set(self::TIMETABLE_OPTION_STRING, serialize($this->timetable));
133 }
134
135 public function getScheduledTimeForMethod($className, $methodName, $methodParameter = null)
136 {
137 $taskName = Task::getTaskName($className, $methodName, $methodParameter);
138
139 return $this->taskHasBeenScheduledOnce($taskName) ? $this->timetable[$taskName] : false;
140 }
141
142 public function taskHasBeenScheduledOnce($taskName)
143 {
144 return isset($this->timetable[$taskName]);
145 }
146 }
147