3 * Job queue task base code.
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.
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.
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
21 * @defgroup JobQueue JobQueue
25 * Class to both describe a background job and handle jobs.
26 * To push jobs onto queues, use JobQueueGroup::singleton()->push();
30 abstract class Job
implements IJobSpecification
{
34 /** @var array Array of job parameters */
37 /** @var array Additional queue metadata */
38 public $metadata = [];
43 /** @var bool Expensive jobs may set this to true */
44 protected $removeDuplicates = false;
46 /** @var string Text for error that occurred last */
49 /** @var callable[] */
50 protected $teardownCallbacks = [];
52 /** @var int Bitfield of JOB_* class constants */
53 protected $executionFlags = 0;
55 /** @var int Job must not be wrapped in the usual explicit LBFactory transaction round */
56 const JOB_NO_EXPLICIT_TRX_ROUND
= 1;
60 * @return bool Success
62 abstract public function run();
65 * Create the appropriate object to handle a specific job
67 * @param string $command Job command
68 * @param array|Title $params Job parameters
69 * @throws InvalidArgumentException
72 public static function factory( $command, $params = [] ) {
75 if ( $params instanceof Title
) {
76 // Backwards compatibility for old signature ($command, $title, $params)
78 $params = func_num_args() >= 3 ?
func_get_arg( 2 ) : [];
80 // Subclasses can override getTitle() to return something more meaningful
81 $title = Title
::makeTitle( NS_SPECIAL
, 'Blankpage' );
84 if ( isset( $wgJobClasses[$command] ) ) {
85 $handler = $wgJobClasses[$command];
87 if ( is_callable( $handler ) ) {
88 $job = call_user_func( $handler, $title, $params );
89 } elseif ( class_exists( $handler ) ) {
90 $job = new $handler( $title, $params );
95 if ( $job instanceof Job
) {
96 $job->command
= $command;
100 throw new InvalidArgumentException( "Could instantiate job '$command': bad spec!" );
104 throw new InvalidArgumentException( "Invalid job command '{$command}'" );
108 * @param string $command
109 * @param array|Title|null $params
111 public function __construct( $command, $params = null ) {
112 if ( $params instanceof Title
) {
113 // Backwards compatibility for old signature ($command, $title, $params)
115 $params = func_get_arg( 2 );
117 // Subclasses can override getTitle() to return something more meaningful
118 $title = Title
::makeTitle( NS_SPECIAL
, 'Blankpage' );
121 $this->command
= $command;
122 $this->title
= $title;
123 $this->params
= is_array( $params ) ?
$params : [];
124 if ( !isset( $this->params
['requestId'] ) ) {
125 $this->params
['requestId'] = WebRequest
::getRequestId();
130 * @param int $flag JOB_* class constant
134 public function hasExecutionFlag( $flag ) {
135 return ( $this->executionFlags
& $flag ) === $flag;
141 public function getType() {
142 return $this->command
;
148 public function getTitle() {
155 public function getParams() {
156 return $this->params
;
160 * @param string|null $field Metadata field or null to get all the metadata
161 * @return mixed|null Value; null if missing
164 public function getMetadata( $field = null ) {
165 if ( $field === null ) {
166 return $this->metadata
;
169 return $this->metadata
[$field] ??
null;
173 * @param string $field Key name to set the value for
174 * @param mixed $value The value to set the field for
175 * @return mixed|null The prior field value; null if missing
178 public function setMetadata( $field, $value ) {
179 $old = $this->getMetadata( $field );
180 if ( $value === null ) {
181 unset( $this->metadata
[$field] );
183 $this->metadata
[$field] = $value;
190 * @return int|null UNIX timestamp to delay running this job until, otherwise null
193 public function getReleaseTimestamp() {
194 return isset( $this->params
['jobReleaseTimestamp'] )
195 ?
wfTimestampOrNull( TS_UNIX
, $this->params
['jobReleaseTimestamp'] )
200 * @return int|null UNIX timestamp of when the job was queued, or null
203 public function getQueuedTimestamp() {
204 return isset( $this->metadata
['timestamp'] )
205 ?
wfTimestampOrNull( TS_UNIX
, $this->metadata
['timestamp'] )
210 * @return string|null Id of the request that created this job. Follows
211 * jobs recursively, allowing to track the id of the request that started a
212 * job when jobs insert jobs which insert other jobs.
215 public function getRequestId() {
216 return $this->params
['requestId'] ??
null;
220 * @return int|null UNIX timestamp of when the job was runnable, or null
223 public function getReadyTimestamp() {
224 return $this->getReleaseTimestamp() ?
: $this->getQueuedTimestamp();
228 * Whether the queue should reject insertion of this job if a duplicate exists
230 * This can be used to avoid duplicated effort or combined with delayed jobs to
231 * coalesce updates into larger batches. Claimed jobs are never treated as
232 * duplicates of new jobs, and some queues may allow a few duplicates due to
233 * network partitions and fail-over. Thus, additional locking is needed to
234 * enforce mutual exclusion if this is really needed.
238 public function ignoreDuplicates() {
239 return $this->removeDuplicates
;
243 * @return bool Whether this job can be retried on failure by job runners
246 public function allowRetries() {
251 * @return int Number of actually "work items" handled in this job
252 * @see $wgJobBackoffThrottling
255 public function workItemCount() {
260 * Subclasses may need to override this to make duplication detection work.
261 * The resulting map conveys everything that makes the job unique. This is
262 * only checked if ignoreDuplicates() returns true, meaning that duplicate
263 * jobs are supposed to be ignored.
265 * @return array Map of key/values
268 public function getDeduplicationInfo() {
270 'type' => $this->getType(),
271 'namespace' => $this->getTitle()->getNamespace(),
272 'title' => $this->getTitle()->getDBkey(),
273 'params' => $this->getParams()
275 if ( is_array( $info['params'] ) ) {
276 // Identical jobs with different "root" jobs should count as duplicates
277 unset( $info['params']['rootJobSignature'] );
278 unset( $info['params']['rootJobTimestamp'] );
279 // Likewise for jobs with different delay times
280 unset( $info['params']['jobReleaseTimestamp'] );
281 // Identical jobs from different requests should count as duplicates
282 unset( $info['params']['requestId'] );
283 // Queues pack and hash this array, so normalize the order
284 ksort( $info['params'] );
291 * Get "root job" parameters for a task
293 * This is used to no-op redundant jobs, including child jobs of jobs,
294 * as long as the children inherit the root job parameters. When a job
295 * with root job parameters and "rootJobIsSelf" set is pushed, the
296 * deduplicateRootJob() method is automatically called on it. If the
297 * root job is only virtual and not actually pushed (e.g. the sub-jobs
298 * are inserted directly), then call deduplicateRootJob() directly.
300 * @see JobQueue::deduplicateRootJob()
302 * @param string $key A key that identifies the task
303 * @return array Map of:
304 * - rootJobIsSelf : true
305 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
306 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
309 public static function newRootJobParams( $key ) {
311 'rootJobIsSelf' => true,
312 'rootJobSignature' => sha1( $key ),
313 'rootJobTimestamp' => wfTimestampNow()
318 * @see JobQueue::deduplicateRootJob()
322 public function getRootJobParams() {
324 'rootJobSignature' => $this->params
['rootJobSignature'] ??
null,
325 'rootJobTimestamp' => $this->params
['rootJobTimestamp'] ??
null
330 * @see JobQueue::deduplicateRootJob()
334 public function hasRootJobParams() {
335 return isset( $this->params
['rootJobSignature'] )
336 && isset( $this->params
['rootJobTimestamp'] );
340 * @see JobQueue::deduplicateRootJob()
341 * @return bool Whether this is job is a root job
343 public function isRootJob() {
344 return $this->hasRootJobParams() && !empty( $this->params
['rootJobIsSelf'] );
348 * @param callable $callback A function with one parameter, the success status, which will be
349 * false if the job failed or it succeeded but the DB changes could not be committed or
350 * any deferred updates threw an exception. (This parameter was added in 1.28.)
353 protected function addTeardownCallback( $callback ) {
354 $this->teardownCallbacks
[] = $callback;
358 * Do any final cleanup after run(), deferred updates, and all DB commits happen
359 * @param bool $status Whether the job, its deferred updates, and DB commit all succeeded
362 public function teardown( $status ) {
363 foreach ( $this->teardownCallbacks
as $callback ) {
364 call_user_func( $callback, $status );
371 public function toString() {
373 if ( $this->params
) {
374 foreach ( $this->params
as $key => $value ) {
375 if ( $paramString != '' ) {
378 if ( is_array( $value ) ) {
380 foreach ( $value as $k => $v ) {
381 $json = FormatJson
::encode( $v );
382 if ( $json === false ||
mb_strlen( $json ) > 512 ) {
383 $filteredValue[$k] = gettype( $v ) . '(...)';
385 $filteredValue[$k] = $v;
388 if ( count( $filteredValue ) <= 10 ) {
389 $value = FormatJson
::encode( $filteredValue );
391 $value = "array(" . count( $value ) . ")";
393 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
394 $value = "object(" . get_class( $value ) . ")";
397 $flatValue = (string)$value;
398 if ( mb_strlen( $value ) > 1024 ) {
399 $flatValue = "string(" . mb_strlen( $value ) . ")";
402 $paramString .= "$key={$flatValue}";
407 foreach ( $this->metadata
as $key => $value ) {
408 if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) {
409 $metaString .= ( $metaString ?
",$key=$value" : "$key=$value" );
414 if ( is_object( $this->title
) ) {
415 $s .= " {$this->title->getPrefixedDBkey()}";
417 if ( $paramString != '' ) {
418 $s .= " $paramString";
420 if ( $metaString != '' ) {
421 $s .= " ($metaString)";
427 protected function setLastError( $error ) {
428 $this->error
= $error;
431 public function getLastError() {