Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScheduledTaskTimetable.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. /**
  11. * This data structure holds the scheduled times for each active scheduled task.
  12. */
  13. class ScheduledTaskTimetable
  14. {
  15. const TIMETABLE_OPTION_STRING = "TaskScheduler.timetable";
  16. private $timetable;
  17. public function __construct()
  18. {
  19. $optionData = Option::get(self::TIMETABLE_OPTION_STRING);
  20. $unserializedTimetable = @unserialize($optionData);
  21. $this->timetable = $unserializedTimetable === false ? array() : $unserializedTimetable;
  22. }
  23. public function getTimetable()
  24. {
  25. return $this->timetable;
  26. }
  27. public function setTimetable($timetable)
  28. {
  29. $this->timetable = $timetable;
  30. }
  31. public function removeInactiveTasks($activeTasks)
  32. {
  33. $activeTaskNames = array();
  34. foreach ($activeTasks as $task) {
  35. $activeTaskNames[] = $task->getName();
  36. }
  37. foreach (array_keys($this->timetable) as $taskName) {
  38. if (!in_array($taskName, $activeTaskNames)) {
  39. unset($this->timetable[$taskName]);
  40. }
  41. }
  42. }
  43. public function getScheduledTaskNames()
  44. {
  45. return array_keys($this->timetable);
  46. }
  47. public function getScheduledTaskTime($taskName)
  48. {
  49. return isset($this->timetable[$taskName]) ? Date::factory($this->timetable[$taskName]) : false;
  50. }
  51. /**
  52. * Checks if the task should be executed
  53. *
  54. * Task has to be executed if :
  55. * - the task has already been scheduled once and the current system time is greater than the scheduled time.
  56. * - execution is forced, see $forceTaskExecution
  57. *
  58. * @param string $taskName
  59. *
  60. * @return boolean
  61. */
  62. public function shouldExecuteTask($taskName)
  63. {
  64. $forceTaskExecution =
  65. (isset($GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS']) && $GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS'])
  66. || DEBUG_FORCE_SCHEDULED_TASKS;
  67. return $forceTaskExecution || ($this->taskHasBeenScheduledOnce($taskName) && time() >= $this->timetable[$taskName]);
  68. }
  69. /**
  70. * Checks if a task should be rescheduled
  71. *
  72. * Task has to be rescheduled if :
  73. * - the task has to be executed
  74. * - the task has never been scheduled before
  75. *
  76. * @param string $taskName
  77. *
  78. * @return boolean
  79. */
  80. public function taskShouldBeRescheduled($taskName)
  81. {
  82. return !$this->taskHasBeenScheduledOnce($taskName) || $this->shouldExecuteTask($taskName);
  83. }
  84. public function rescheduleTask($task)
  85. {
  86. // update the scheduled time
  87. $this->timetable[$task->getName()] = $task->getRescheduledTime();
  88. $this->save();
  89. }
  90. public function save()
  91. {
  92. Option::set(self::TIMETABLE_OPTION_STRING, serialize($this->timetable));
  93. }
  94. public function getScheduledTimeForMethod($className, $methodName, $methodParameter = null)
  95. {
  96. $taskName = ScheduledTask::getTaskName($className, $methodName, $methodParameter);
  97. return $this->taskHasBeenScheduledOnce($taskName) ? $this->timetable[$taskName] : false;
  98. }
  99. public function taskHasBeenScheduledOnce($taskName)
  100. {
  101. return isset($this->timetable[$taskName]);
  102. }
  103. }