1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Scheduler;
use Piwik\Common;
use Piwik\Option;
use Piwik\Date;
/**
* This data structure holds the scheduled times for each active scheduled task.
*/
class Timetable
{
public const TIMETABLE_OPTION_STRING = "TaskScheduler.timetable";
public const RETRY_OPTION_STRING = "TaskScheduler.retryList";
private $timetable;
private $retryList;
public function __construct()
{
$this->readFromOption();
}
public function getTimetable()
{
return $this->timetable;
}
public function setTimetable($timetable)
{
$this->timetable = $timetable;
}
public function setRetryList($retryList)
{
$this->retryList = $retryList;
}
/**
* @param Task[] $activeTasks
*/
public function removeInactiveTasks($activeTasks)
{
$activeTaskNames = array();
foreach ($activeTasks as $task) {
$activeTaskNames[] = $task->getName();
}
foreach (array_keys($this->timetable) as $taskName) {
if (!in_array($taskName, $activeTaskNames)) {
unset($this->timetable[$taskName]);
}
}
$this->save();
}
public function getScheduledTaskNames()
{
return array_keys($this->timetable);
}
public function getScheduledTaskTime($taskName)
{
return isset($this->timetable[$taskName]) ? Date::factory($this->timetable[$taskName]) : false;
}
/**
* Checks if the task should be executed
*
* Task has to be executed if :
* - the task has already been scheduled once and the current system time is greater than the scheduled time.
* - execution is forced, see $forceTaskExecution
*
* @param string $taskName
*
* @return boolean
*/
public function shouldExecuteTask($taskName)
{
$forceTaskExecution = (defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS);
if ($forceTaskExecution) {
return true;
}
return $this->taskHasBeenScheduledOnce($taskName) && time() >= $this->timetable[$taskName];
}
/**
* Checks if a task should be rescheduled
*
* Task has to be rescheduled if :
* - the task has to be executed
* - the task has never been scheduled before
*
* @param string $taskName
*
* @return boolean
*/
public function taskShouldBeRescheduled($taskName)
{
return !$this->taskHasBeenScheduledOnce($taskName) || $this->shouldExecuteTask($taskName);
}
public function rescheduleTask(Task $task)
{
$rescheduledTime = $task->getRescheduledTime();
// update the scheduled time
$this->timetable[$task->getName()] = $rescheduledTime;
$this->save();
return Date::factory($rescheduledTime);
}
public function rescheduleTaskAndRunTomorrow(Task $task)
{
$tomorrow = Date::factory('tomorrow');
// update the scheduled time
$this->timetable[$task->getName()] = $tomorrow->getTimestamp();
$this->save();
return $tomorrow;
}
public function rescheduleTaskAndRunInOneHour(Task $task)
{
$oneHourFromNow = Date::factory('now')->addHour(1);
// update the scheduled time
$this->timetable[$task->getName()] = $oneHourFromNow->getTimestamp();
$this->save();
return $oneHourFromNow;
}
public function save()
{
Option::set(self::TIMETABLE_OPTION_STRING, serialize($this->timetable));
}
public function getScheduledTimeForMethod($className, $methodName, $methodParameter = null)
{
$taskName = Task::getTaskName($className, $methodName, $methodParameter);
return $this->taskHasBeenScheduledOnce($taskName) ? $this->timetable[$taskName] : false;
}
public function taskHasBeenScheduledOnce($taskName)
{
return isset($this->timetable[$taskName]);
}
public function readFromOption()
{
Option::clearCachedOption(self::TIMETABLE_OPTION_STRING);
$optionData = Option::get(self::TIMETABLE_OPTION_STRING);
$unserializedTimetable = Common::safe_unserialize($optionData);
$this->timetable = $unserializedTimetable === false ? array() : $unserializedTimetable;
}
/**
* Read the retry list option from the database
*
* @throws \Throwable
*/
private function readRetryList()
{
Option::clearCachedOption(self::RETRY_OPTION_STRING);
$retryData = Option::get(self::RETRY_OPTION_STRING);
$unserializedRetryList = Common::safe_unserialize($retryData);
$this->retryList = $unserializedRetryList === false ? array() : $unserializedRetryList;
}
/**
* Save the retry list option to the database
*/
public function saveRetryList()
{
Option::set(self::RETRY_OPTION_STRING, serialize($this->retryList));
}
/**
* Remove a task from the retry list
*
*/
public function clearRetryCount(string $taskName)
{
if (isset($this->retryList[$taskName])) {
unset($this->retryList[$taskName]);
$this->saveRetryList();
}
}
/**
* Increment the retry counter for a task
*
*/
public function incrementRetryCount(string $taskName)
{
$this->readRetryList();
if (!isset($this->retryList[$taskName])) {
$this->retryList[$taskName] = 0;
}
$this->retryList[$taskName]++;
$this->saveRetryList();
}
/**
* Return the current number of retries for a task
*
*
*/
public function getRetryCount(string $taskName): int
{
$this->readRetryList();
// Ignore excessive retry counts, workaround for SchedulerTest mock
if (!isset($this->retryList[$taskName]) || $this->retryList[$taskName] > 10000) {
return 0;
}
return $this->retryList[$taskName];
}
}
|