MediaWiki 1.40.4
Job.php
Go to the documentation of this file.
1<?php
27
39abstract class Job implements RunnableJob {
41 public $command;
42
44 public $params;
45
47 public $metadata = [];
48
50 protected $title;
51
53 protected $removeDuplicates = false;
54
56 protected $error;
57
59 protected $teardownCallbacks = [];
60
62 protected $executionFlags = 0;
63
74 public static function factory( $command, $params = [] ) {
75 $factory = MediaWikiServices::getInstance()->getJobFactory();
76
77 // FIXME: fix handling for legacy signature!
78 // @phan-suppress-next-line PhanParamTooFewUnpack one argument is known to be present.
79 return $factory->newJob( ...func_get_args() );
80 }
81
88 public function __construct( $command, $params = null ) {
89 if ( $params instanceof PageReference ) {
90 // Backwards compatibility for old signature ($command, $title, $params)
91 $page = $params;
92 $params = func_num_args() >= 3 ? func_get_arg( 2 ) : [];
93 } else {
94 // Newer jobs may choose to not have a top-level title (e.g. GenericParameterJob)
95 $page = null;
96 }
97
98 if ( !is_array( $params ) ) {
99 throw new InvalidArgumentException( '$params must be an array' );
100 }
101
102 if (
103 $page &&
104 !isset( $params['namespace'] ) &&
105 !isset( $params['title'] )
106 ) {
107 // When constructing this class for submitting to the queue,
108 // normalise the $page arg of old job classes as part of $params.
109 $params['namespace'] = $page->getNamespace();
110 $params['title'] = $page->getDBkey();
111 }
112
113 $this->command = $command;
114 $this->params = $params + [ 'requestId' => WebRequest::getRequestId() ];
115
116 if ( $this->title === null ) {
117 // Set this field for access via getTitle().
118 $this->title = ( isset( $params['namespace'] ) && isset( $params['title'] ) )
119 ? Title::makeTitle( $params['namespace'], $params['title'] )
120 // GenericParameterJob classes without namespace/title params
121 // should not use getTitle(). Set an invalid title as placeholder.
122 : Title::makeTitle( NS_SPECIAL, '' );
123 }
124 }
125
130 public function hasExecutionFlag( $flag ) {
131 return ( $this->executionFlags & $flag ) === $flag;
132 }
133
138 public function getType() {
139 return $this->command;
140 }
141
145 final public function getTitle() {
146 return $this->title;
147 }
148
153 public function getParams() {
154 return $this->params;
155 }
156
163 public function getMetadata( $field = null ) {
164 if ( $field === null ) {
165 return $this->metadata;
166 }
167
168 return $this->metadata[$field] ?? null;
169 }
170
178 public function setMetadata( $field, $value ) {
179 $old = $this->getMetadata( $field );
180 if ( $value === null ) {
181 unset( $this->metadata[$field] );
182 } else {
183 $this->metadata[$field] = $value;
184 }
185
186 return $old;
187 }
188
194 public function getReleaseTimestamp() {
195 return isset( $this->params['jobReleaseTimestamp'] )
196 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
197 : null;
198 }
199
204 public function getQueuedTimestamp() {
205 return isset( $this->metadata['timestamp'] )
206 ? wfTimestampOrNull( TS_UNIX, $this->metadata['timestamp'] )
207 : null;
208 }
209
214 public function getRequestId() {
215 return $this->params['requestId'] ?? null;
216 }
217
222 public function getReadyTimestamp() {
223 return $this->getReleaseTimestamp() ?: $this->getQueuedTimestamp();
224 }
225
239 public function ignoreDuplicates() {
240 return $this->removeDuplicates;
241 }
242
247 public function allowRetries() {
248 return true;
249 }
250
255 public function workItemCount() {
256 return 1;
257 }
258
269 public function getDeduplicationInfo() {
270 $info = [
271 'type' => $this->getType(),
272 'params' => $this->getParams()
273 ];
274 if ( is_array( $info['params'] ) ) {
275 // Identical jobs with different "root" jobs should count as duplicates
276 unset( $info['params']['rootJobSignature'] );
277 unset( $info['params']['rootJobTimestamp'] );
278 // Likewise for jobs with different delay times
279 unset( $info['params']['jobReleaseTimestamp'] );
280 // Identical jobs from different requests should count as duplicates
281 unset( $info['params']['requestId'] );
282 // Queues pack and hash this array, so normalize the order
283 ksort( $info['params'] );
284 }
285
286 return $info;
287 }
288
308 public static function newRootJobParams( $key ) {
309 return [
310 'rootJobIsSelf' => true,
311 'rootJobSignature' => sha1( $key ),
312 'rootJobTimestamp' => wfTimestampNow()
313 ];
314 }
315
322 public function getRootJobParams() {
323 return [
324 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
325 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
326 ];
327 }
328
335 public function hasRootJobParams() {
336 return isset( $this->params['rootJobSignature'] )
337 && isset( $this->params['rootJobTimestamp'] );
338 }
339
345 public function isRootJob() {
346 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
347 }
348
355 protected function addTeardownCallback( $callback ) {
356 $this->teardownCallbacks[] = $callback;
357 }
358
363 public function teardown( $status ) {
364 foreach ( $this->teardownCallbacks as $callback ) {
365 call_user_func( $callback, $status );
366 }
367 }
368
373 public function toString() {
374 $paramString = '';
375 if ( $this->params ) {
376 foreach ( $this->params as $key => $value ) {
377 if ( $paramString != '' ) {
378 $paramString .= ' ';
379 }
380 if ( is_array( $value ) ) {
381 $filteredValue = [];
382 foreach ( $value as $k => $v ) {
383 $json = FormatJson::encode( $v );
384 if ( $json === false || mb_strlen( $json ) > 512 ) {
385 $filteredValue[$k] = gettype( $v ) . '(...)';
386 } else {
387 $filteredValue[$k] = $v;
388 }
389 }
390 if ( count( $filteredValue ) <= 10 ) {
391 $value = FormatJson::encode( $filteredValue );
392 } else {
393 $value = "array(" . count( $value ) . ")";
394 }
395 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
396 $value = "object(" . get_class( $value ) . ")";
397 }
398
399 $flatValue = (string)$value;
400 if ( mb_strlen( $flatValue ) > 1024 ) {
401 $flatValue = "string(" . mb_strlen( $value ) . ")";
402 }
403
404 $paramString .= "$key={$flatValue}";
405 }
406 }
407
408 $metaString = '';
409 foreach ( $this->metadata as $key => $value ) {
410 if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) {
411 $metaString .= ( $metaString ? ",$key=$value" : "$key=$value" );
412 }
413 }
414
415 $s = $this->command;
416 if ( is_object( $this->title ) ) {
417 $s .= ' ' . $this->title->getPrefixedDBkey();
418 }
419 if ( $paramString != '' ) {
420 $s .= " $paramString";
421 }
422 if ( $metaString != '' ) {
423 $s .= " ($metaString)";
424 }
425
426 return $s;
427 }
428
429 protected function setLastError( $error ) {
430 $this->error = $error;
431 }
432
437 public function getLastError() {
438 return $this->error;
439 }
440}
const NS_SPECIAL
Definition Defines.php:53
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Class to both describe a background job and handle jobs.
Definition Job.php:39
hasRootJobParams()
Definition Job.php:335
string $command
Definition Job.php:41
setMetadata( $field, $value)
Definition Job.php:178
getParams()
array Parameters that specify sources, targets, and options for execution
Definition Job.php:153
getReadyTimestamp()
int|null UNIX timestamp of when the job was runnable, or null 1.26
Definition Job.php:222
toString()
string Debugging string describing the job
Definition Job.php:373
getType()
string Job type that defines what sort of changes this job makes
Definition Job.php:138
Title $title
Definition Job.php:50
getRootJobParams()
Definition Job.php:322
hasExecutionFlag( $flag)
bool 1.31
Definition Job.php:130
callable[] $teardownCallbacks
Definition Job.php:59
getQueuedTimestamp()
Definition Job.php:204
bool $removeDuplicates
Expensive jobs may set this to true.
Definition Job.php:53
setLastError( $error)
Definition Job.php:429
getMetadata( $field=null)
Definition Job.php:163
teardown( $status)
Definition Job.php:363
isRootJob()
Definition Job.php:345
addTeardownCallback( $callback)
Definition Job.php:355
array $params
Array of job parameters.
Definition Job.php:44
getTitle()
Definition Job.php:145
array $metadata
Additional queue metadata.
Definition Job.php:47
static factory( $command, $params=[])
Create the appropriate object to handle a specific job.
Definition Job.php:74
int $executionFlags
Bitfield of JOB_* class constants.
Definition Job.php:62
workItemCount()
Definition Job.php:255
getRequestId()
string|null Id of the request that created this job. Follows jobs recursively, allowing to track the ...
Definition Job.php:214
static newRootJobParams( $key)
Get "root job" parameters for a task.
Definition Job.php:308
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Definition Job.php:269
getReleaseTimestamp()
Definition Job.php:194
ignoreDuplicates()
Whether the queue should reject insertion of this job if a duplicate exists.
Definition Job.php:239
getLastError()
string
Definition Job.php:437
string $error
Text for error that occurred last.
Definition Job.php:56
allowRetries()
bool Whether this job can be retried on failure by job runners 1.21
Definition Job.php:247
__construct( $command, $params=null)
Definition Job.php:88
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Job that has a run() method and metadata accessors for JobQueue::pop() and JobQueue::ack()