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.

ScheduledTask.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. use Exception;
  11. use Piwik\ScheduledTime;
  12. /**
  13. * Contains metadata referencing PHP code that should be executed at regular
  14. * intervals.
  15. *
  16. * See the {@link TaskScheduler} docs to learn more about scheduled tasks.
  17. *
  18. *
  19. * @api
  20. */
  21. class ScheduledTask
  22. {
  23. const LOWEST_PRIORITY = 12;
  24. const LOW_PRIORITY = 9;
  25. const NORMAL_PRIORITY = 6;
  26. const HIGH_PRIORITY = 3;
  27. const HIGHEST_PRIORITY = 0;
  28. /**
  29. * Object instance on which the method will be executed by the task scheduler
  30. * @var string
  31. */
  32. private $objectInstance;
  33. /**
  34. * Class name where the specified method is located
  35. * @var string
  36. */
  37. private $className;
  38. /**
  39. * Class method to run when task is scheduled
  40. * @var string
  41. */
  42. private $methodName;
  43. /**
  44. * Parameter to pass to the executed method
  45. * @var string
  46. */
  47. private $methodParameter;
  48. /**
  49. * The scheduled time policy
  50. * @var ScheduledTime
  51. */
  52. private $scheduledTime;
  53. /**
  54. * The priority of a task. Affects the order in which this task will be run.
  55. * @var int
  56. */
  57. private $priority;
  58. /**
  59. * Constructor.
  60. *
  61. * @param mixed $objectInstance The object or class that contains the method to execute regularly.
  62. * Usually this will be a {@link Plugin} instance.
  63. * @param string $methodName The name of the method that will be regularly executed.
  64. * @param mixed|null $methodParameter An optional parameter to pass to the method when executed.
  65. * Must be convertible to string.
  66. * @param ScheduledTime|null $scheduledTime A {@link ScheduledTime} instance that describes when the method
  67. * should be executed and how long before the next execution.
  68. * @param int $priority The priority of the task. Tasks with a higher priority will be executed first.
  69. * Tasks with low priority will be executed last.
  70. */
  71. public function __construct($objectInstance, $methodName, $methodParameter, $scheduledTime,
  72. $priority = self::NORMAL_PRIORITY)
  73. {
  74. $this->className = $this->getClassNameFromInstance($objectInstance);
  75. if ($priority < self::HIGHEST_PRIORITY || $priority > self::LOWEST_PRIORITY) {
  76. throw new Exception("Invalid priority for ScheduledTask '$this->className.$methodName': $priority");
  77. }
  78. $this->objectInstance = $objectInstance;
  79. $this->methodName = $methodName;
  80. $this->scheduledTime = $scheduledTime;
  81. $this->methodParameter = $methodParameter;
  82. $this->priority = $priority;
  83. }
  84. protected function getClassNameFromInstance($_objectInstance)
  85. {
  86. if (is_string($_objectInstance)) {
  87. return $_objectInstance;
  88. }
  89. $namespaced = get_class($_objectInstance);
  90. return $namespaced;
  91. }
  92. /**
  93. * Returns the object instance that contains the method to execute. Returns a class
  94. * name if the method is static.
  95. *
  96. * @return mixed
  97. */
  98. public function getObjectInstance()
  99. {
  100. return $this->objectInstance;
  101. }
  102. /**
  103. * Returns the name of the class that contains the method to execute.
  104. *
  105. * @return string
  106. */
  107. public function getClassName()
  108. {
  109. return $this->className;
  110. }
  111. /**
  112. * Returns the name of the method that will be executed.
  113. *
  114. * @return string
  115. */
  116. public function getMethodName()
  117. {
  118. return $this->methodName;
  119. }
  120. /**
  121. * Returns the value that will be passed to the method when executed, or `null` if
  122. * no value will be supplied.
  123. *
  124. * @return string|null
  125. */
  126. public function getMethodParameter()
  127. {
  128. return $this->methodParameter;
  129. }
  130. /**
  131. * Returns a {@link ScheduledTime} instance that describes when the method should be executed
  132. * and how long before the next execution.
  133. *
  134. * @return ScheduledTime
  135. */
  136. public function getScheduledTime()
  137. {
  138. return $this->scheduledTime;
  139. }
  140. /**
  141. * Returns the time in milliseconds when this task will be executed next.
  142. *
  143. * @return int
  144. */
  145. public function getRescheduledTime()
  146. {
  147. return $this->getScheduledTime()->getRescheduledTime();
  148. }
  149. /**
  150. * Returns the task priority. The priority will be an integer whose value is
  151. * between {@link HIGH_PRIORITY} and {@link LOW_PRIORITY}.
  152. *
  153. * @return int
  154. */
  155. public function getPriority()
  156. {
  157. return $this->priority;
  158. }
  159. /**
  160. * Returns a unique name for this scheduled task. The name is stored in the DB and is used
  161. * to store a task's previous execution time. The name is created using:
  162. *
  163. * - the name of the class that contains the method to execute,
  164. * - the name of the method to regularly execute,
  165. * - and the value that is passed to the executed task.
  166. *
  167. * @return string
  168. */
  169. public function getName()
  170. {
  171. return self::getTaskName($this->getClassName(), $this->getMethodName(), $this->getMethodParameter());
  172. }
  173. /**
  174. * @ignore
  175. */
  176. public static function getTaskName($className, $methodName, $methodParameter = null)
  177. {
  178. return $className . '.' . $methodName . ($methodParameter == null ? '' : '_' . $methodParameter);
  179. }
  180. }