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 |