JobSpecification.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 interface
  24. *
  25. * @ingroup JobQueue
  26. * @since 1.23
  27. */
  28. interface IJobSpecification {
  29. /**
  30. * @return string Job type
  31. */
  32. public function getType();
  33. /**
  34. * @return array
  35. */
  36. public function getParams();
  37. /**
  38. * @return int|null UNIX timestamp to delay running this job until, otherwise null
  39. */
  40. public function getReleaseTimestamp();
  41. /**
  42. * @return bool Whether only one of each identical set of jobs should be run
  43. */
  44. public function ignoreDuplicates();
  45. /**
  46. * Subclasses may need to override this to make duplication detection work.
  47. * The resulting map conveys everything that makes the job unique. This is
  48. * only checked if ignoreDuplicates() returns true, meaning that duplicate
  49. * jobs are supposed to be ignored.
  50. *
  51. * @return array Map of key/values
  52. */
  53. public function getDeduplicationInfo();
  54. /**
  55. * @see JobQueue::deduplicateRootJob()
  56. * @return array
  57. * @since 1.26
  58. */
  59. public function getRootJobParams();
  60. /**
  61. * @see JobQueue::deduplicateRootJob()
  62. * @return bool
  63. * @since 1.22
  64. */
  65. public function hasRootJobParams();
  66. /**
  67. * @see JobQueue::deduplicateRootJob()
  68. * @return bool Whether this is job is a root job
  69. */
  70. public function isRootJob();
  71. /**
  72. * @return Title Descriptive title (this can simply be informative)
  73. */
  74. public function getTitle();
  75. }
  76. /**
  77. * Job queue task description base code
  78. *
  79. * Example usage:
  80. * @code
  81. * $job = new JobSpecification(
  82. * 'null',
  83. * array( 'lives' => 1, 'usleep' => 100, 'pi' => 3.141569 ),
  84. * array( 'removeDuplicates' => 1 ),
  85. * Title::makeTitle( NS_SPECIAL, 'nullity' )
  86. * );
  87. * JobQueueGroup::singleton()->push( $job )
  88. * @endcode
  89. *
  90. * @ingroup JobQueue
  91. * @since 1.23
  92. */
  93. class JobSpecification implements IJobSpecification {
  94. /** @var string */
  95. protected $type;
  96. /** @var array Array of job parameters or false if none */
  97. protected $params;
  98. /** @var Title */
  99. protected $title;
  100. /** @var array */
  101. protected $opts;
  102. /**
  103. * @param string $type
  104. * @param array $params Map of key/values
  105. * @param array $opts Map of key/values; includes 'removeDuplicates'
  106. * @param Title|null $title Optional descriptive title
  107. */
  108. public function __construct(
  109. $type, array $params, array $opts = [], Title $title = null
  110. ) {
  111. $this->validateParams( $params );
  112. $this->validateParams( $opts );
  113. $this->type = $type;
  114. $this->params = $params;
  115. $this->title = $title ?: Title::makeTitle( NS_SPECIAL, 'Badtitle/' . static::class );
  116. $this->opts = $opts;
  117. }
  118. /**
  119. * @param array $params
  120. */
  121. protected function validateParams( array $params ) {
  122. foreach ( $params as $p => $v ) {
  123. if ( is_array( $v ) ) {
  124. $this->validateParams( $v );
  125. } elseif ( !is_scalar( $v ) && $v !== null ) {
  126. throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
  127. }
  128. }
  129. }
  130. public function getType() {
  131. return $this->type;
  132. }
  133. public function getTitle() {
  134. return $this->title;
  135. }
  136. public function getParams() {
  137. return $this->params;
  138. }
  139. public function getReleaseTimestamp() {
  140. return isset( $this->params['jobReleaseTimestamp'] )
  141. ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
  142. : null;
  143. }
  144. public function ignoreDuplicates() {
  145. return !empty( $this->opts['removeDuplicates'] );
  146. }
  147. public function getDeduplicationInfo() {
  148. $info = [
  149. 'type' => $this->getType(),
  150. 'namespace' => $this->getTitle()->getNamespace(),
  151. 'title' => $this->getTitle()->getDBkey(),
  152. 'params' => $this->getParams()
  153. ];
  154. if ( is_array( $info['params'] ) ) {
  155. // Identical jobs with different "root" jobs should count as duplicates
  156. unset( $info['params']['rootJobSignature'] );
  157. unset( $info['params']['rootJobTimestamp'] );
  158. // Likewise for jobs with different delay times
  159. unset( $info['params']['jobReleaseTimestamp'] );
  160. }
  161. return $info;
  162. }
  163. public function getRootJobParams() {
  164. return [
  165. 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
  166. 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
  167. ];
  168. }
  169. public function hasRootJobParams() {
  170. return isset( $this->params['rootJobSignature'] )
  171. && isset( $this->params['rootJobTimestamp'] );
  172. }
  173. public function isRootJob() {
  174. return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
  175. }
  176. /**
  177. * @return array Field/value map that can immediately be serialized
  178. * @since 1.25
  179. */
  180. public function toSerializableArray() {
  181. return [
  182. 'type' => $this->type,
  183. 'params' => $this->params,
  184. 'opts' => $this->opts,
  185. 'title' => [
  186. 'ns' => $this->title->getNamespace(),
  187. 'key' => $this->title->getDBkey()
  188. ]
  189. ];
  190. }
  191. /**
  192. * @param array $map Field/value map
  193. * @return JobSpecification
  194. * @since 1.25
  195. */
  196. public static function newFromArray( array $map ) {
  197. $title = Title::makeTitle( $map['title']['ns'], $map['title']['key'] );
  198. return new self( $map['type'], $map['params'], $map['opts'], $title );
  199. }
  200. }