JobSpecification.php 5.8 KB

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