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 |