JobSpecification.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Job queue task description base code.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Job queue task description base code
  24. *
  25. * Example usage:
  26. * @code
  27. * $job = new JobSpecification(
  28. * 'null',
  29. * [ 'lives' => 1, 'usleep' => 100, 'pi' => 3.141569 ],
  30. * [ 'removeDuplicates' => 1 ]
  31. * );
  32. * JobQueueGroup::singleton()->push( $job )
  33. * @endcode
  34. *
  35. * @ingroup JobQueue
  36. * @since 1.23
  37. */
  38. class JobSpecification implements IJobSpecification {
  39. /** @var string */
  40. protected $type;
  41. /** @var array Array of job parameters or false if none */
  42. protected $params;
  43. /** @var Title */
  44. protected $title;
  45. /** @var array */
  46. protected $opts;
  47. /**
  48. * @param string $type
  49. * @param array $params Map of key/values
  50. * @param array $opts Map of key/values; includes 'removeDuplicates'
  51. * @param Title|null $title Optional descriptive title
  52. */
  53. public function __construct(
  54. $type, array $params, array $opts = [], Title $title = null
  55. ) {
  56. $this->validateParams( $params );
  57. $this->validateParams( $opts );
  58. $this->type = $type;
  59. if ( $title instanceof Title ) {
  60. // Make sure JobQueue classes can pull the title from parameters alone
  61. if ( $title->getDBkey() !== '' ) {
  62. $params += [
  63. 'namespace' => $title->getNamespace(),
  64. 'title' => $title->getDBkey()
  65. ];
  66. }
  67. } else {
  68. $title = Title::makeTitle( NS_SPECIAL, '' );
  69. }
  70. $this->params = $params;
  71. $this->title = $title;
  72. $this->opts = $opts;
  73. }
  74. /**
  75. * @param array $params
  76. */
  77. protected function validateParams( array $params ) {
  78. foreach ( $params as $p => $v ) {
  79. if ( is_array( $v ) ) {
  80. $this->validateParams( $v );
  81. } elseif ( !is_scalar( $v ) && $v !== null ) {
  82. throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
  83. }
  84. }
  85. }
  86. public function getType() {
  87. return $this->type;
  88. }
  89. public function getTitle() {
  90. return $this->title;
  91. }
  92. public function getParams() {
  93. return $this->params;
  94. }
  95. public function getReleaseTimestamp() {
  96. return isset( $this->params['jobReleaseTimestamp'] )
  97. ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
  98. : null;
  99. }
  100. public function ignoreDuplicates() {
  101. return !empty( $this->opts['removeDuplicates'] );
  102. }
  103. public function getDeduplicationInfo() {
  104. $info = [
  105. 'type' => $this->getType(),
  106. 'namespace' => $this->getTitle()->getNamespace(),
  107. 'title' => $this->getTitle()->getDBkey(),
  108. 'params' => $this->getParams()
  109. ];
  110. if ( is_array( $info['params'] ) ) {
  111. // Identical jobs with different "root" jobs should count as duplicates
  112. unset( $info['params']['rootJobSignature'] );
  113. unset( $info['params']['rootJobTimestamp'] );
  114. // Likewise for jobs with different delay times
  115. unset( $info['params']['jobReleaseTimestamp'] );
  116. }
  117. return $info;
  118. }
  119. public function getRootJobParams() {
  120. return [
  121. 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
  122. 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
  123. ];
  124. }
  125. public function hasRootJobParams() {
  126. return isset( $this->params['rootJobSignature'] )
  127. && isset( $this->params['rootJobTimestamp'] );
  128. }
  129. public function isRootJob() {
  130. return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
  131. }
  132. /**
  133. * @return array Field/value map that can immediately be serialized
  134. * @since 1.25
  135. */
  136. public function toSerializableArray() {
  137. return [
  138. 'type' => $this->type,
  139. 'params' => $this->params,
  140. 'opts' => $this->opts,
  141. 'title' => [
  142. 'ns' => $this->title->getNamespace(),
  143. 'key' => $this->title->getDBkey()
  144. ]
  145. ];
  146. }
  147. /**
  148. * @param array $map Field/value map
  149. * @return JobSpecification
  150. * @since 1.25
  151. */
  152. public static function newFromArray( array $map ) {
  153. $title = Title::makeTitle( $map['title']['ns'], $map['title']['key'] );
  154. return new self( $map['type'], $map['params'], $map['opts'], $title );
  155. }
  156. }