MediaWiki master
Job.php
Go to the documentation of this file.
1<?php
26
41abstract class Job implements RunnableJob {
43 public $command;
44
46 public $params;
47
49 public $metadata = [];
50
52 protected $title;
53
55 protected $removeDuplicates = false;
56
58 protected $error;
59
61 protected $teardownCallbacks = [];
62
64 protected $executionFlags = 0;
65
76 public static function factory( $command, $params = [] ) {
77 $factory = MediaWikiServices::getInstance()->getJobFactory();
78
79 // FIXME: fix handling for legacy signature!
80 // @phan-suppress-next-line PhanParamTooFewUnpack one argument is known to be present.
81 return $factory->newJob( ...func_get_args() );
82 }
83
90 public function __construct( $command, $params = null ) {
91 if ( $params instanceof PageReference ) {
92 // Backwards compatibility for old signature ($command, $title, $params)
93 $page = $params;
94 $params = func_num_args() >= 3 ? func_get_arg( 2 ) : [];
95 } else {
96 // Newer jobs may choose to not have a top-level title (e.g. GenericParameterJob)
97 $page = null;
98 }
99
100 if ( !is_array( $params ) ) {
101 throw new InvalidArgumentException( '$params must be an array' );
102 }
103
104 if (
105 $page &&
106 !isset( $params['namespace'] ) &&
107 !isset( $params['title'] )
108 ) {
109 // When constructing this class for submitting to the queue,
110 // normalise the $page arg of old job classes as part of $params.
111 $params['namespace'] = $page->getNamespace();
112 $params['title'] = $page->getDBkey();
113 }
114
115 $this->command = $command;
116 $this->params = $params + [
117 'requestId' => Telemetry::getInstance()->getRequestId(),
118 ];
119
120 if ( $this->title === null ) {
121 // Set this field for access via getTitle().
122 $this->title = ( isset( $params['namespace'] ) && isset( $params['title'] ) )
123 ? Title::makeTitle( $params['namespace'], $params['title'] )
124 // GenericParameterJob classes without namespace/title params
125 // should not use getTitle(). Set an invalid title as placeholder.
126 : Title::makeTitle( NS_SPECIAL, '' );
127 }
128 }
129
134 public function hasExecutionFlag( $flag ) {
135 return ( $this->executionFlags & $flag ) === $flag;
136 }
137
142 public function getType() {
143 return $this->command;
144 }
145
149 final public function getTitle() {
150 return $this->title;
151 }
152
157 public function getParams() {
158 return $this->params;
159 }
160
167 public function getMetadata( $field = null ) {
168 if ( $field === null ) {
169 return $this->metadata;
170 }
171
172 return $this->metadata[$field] ?? null;
173 }
174
182 public function setMetadata( $field, $value ) {
183 $old = $this->getMetadata( $field );
184 if ( $value === null ) {
185 unset( $this->metadata[$field] );
186 } else {
187 $this->metadata[$field] = $value;
188 }
189
190 return $old;
191 }
192
198 public function getReleaseTimestamp() {
199 $time = wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] ?? null );
200 return $time ? (int)$time : null;
201 }
202
207 public function getQueuedTimestamp() {
208 $time = wfTimestampOrNull( TS_UNIX, $this->metadata['timestamp'] ?? null );
209 return $time ? (int)$time : null;
210 }
211
216 public function getRequestId() {
217 return $this->params['requestId'] ?? null;
218 }
219
224 public function getReadyTimestamp() {
225 return $this->getReleaseTimestamp() ?: $this->getQueuedTimestamp();
226 }
227
241 public function ignoreDuplicates() {
242 return $this->removeDuplicates;
243 }
244
249 public function allowRetries() {
250 return true;
251 }
252
257 public function workItemCount() {
258 return 1;
259 }
260
271 public function getDeduplicationInfo() {
272 $info = [
273 'type' => $this->getType(),
274 'params' => $this->getParams()
275 ];
276 if ( is_array( $info['params'] ) ) {
277 // Identical jobs with different "root" jobs should count as duplicates
278 unset( $info['params']['rootJobSignature'] );
279 unset( $info['params']['rootJobTimestamp'] );
280 // Likewise for jobs with different delay times
281 unset( $info['params']['jobReleaseTimestamp'] );
282 // Identical jobs from different requests should count as duplicates
283 unset( $info['params']['requestId'] );
284 // Queues pack and hash this array, so normalize the order
285 ksort( $info['params'] );
286 }
287
288 return $info;
289 }
290
310 public static function newRootJobParams( $key ) {
311 return [
312 'rootJobIsSelf' => true,
313 'rootJobSignature' => sha1( $key ),
314 'rootJobTimestamp' => wfTimestampNow()
315 ];
316 }
317
324 public function getRootJobParams() {
325 return [
326 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
327 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
328 ];
329 }
330
337 public function hasRootJobParams() {
338 return isset( $this->params['rootJobSignature'] )
339 && isset( $this->params['rootJobTimestamp'] );
340 }
341
347 public function isRootJob() {
348 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
349 }
350
357 protected function addTeardownCallback( $callback ) {
358 $this->teardownCallbacks[] = $callback;
359 }
360
365 public function teardown( $status ) {
366 foreach ( $this->teardownCallbacks as $callback ) {
367 call_user_func( $callback, $status );
368 }
369 }
370
375 public function toString() {
376 $paramString = '';
377 if ( $this->params ) {
378 foreach ( $this->params as $key => $value ) {
379 if ( $paramString != '' ) {
380 $paramString .= ' ';
381 }
382 if ( is_array( $value ) ) {
383 $filteredValue = [];
384 foreach ( $value as $k => $v ) {
385 $json = FormatJson::encode( $v );
386 if ( $json === false || mb_strlen( $json ) > 512 ) {
387 $filteredValue[$k] = gettype( $v ) . '(...)';
388 } else {
389 $filteredValue[$k] = $v;
390 }
391 }
392 if ( count( $filteredValue ) <= 10 ) {
393 $value = FormatJson::encode( $filteredValue );
394 } else {
395 $value = "array(" . count( $value ) . ")";
396 }
397 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
398 $value = "object(" . get_class( $value ) . ")";
399 }
400
401 $flatValue = (string)$value;
402 if ( mb_strlen( $flatValue ) > 1024 ) {
403 $flatValue = "string(" . mb_strlen( $value ) . ")";
404 }
405
406 // Remove newline characters from the value, since
407 // newlines indicate new job lines in log files
408 $flatValue = preg_replace( '/\s+/', ' ', $flatValue );
409
410 $paramString .= "$key={$flatValue}";
411 }
412 }
413
414 $metaString = '';
415 foreach ( $this->metadata as $key => $value ) {
416 if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) {
417 $metaString .= ( $metaString ? ",$key=$value" : "$key=$value" );
418 }
419 }
420
421 $s = $this->command;
422 if ( is_object( $this->title ) ) {
423 $s .= ' ' . $this->title->getPrefixedDBkey();
424 }
425 if ( $paramString != '' ) {
426 $s .= " $paramString";
427 }
428 if ( $metaString != '' ) {
429 $s .= " ($metaString)";
430 }
431
432 return $s;
433 }
434
435 protected function setLastError( $error ) {
436 $this->error = $error;
437 }
438
443 public function getLastError() {
444 return $this->error;
445 }
446}
const NS_SPECIAL
Definition Defines.php:54
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.
array $params
The job parameters.
Describe and execute a background job.
Definition Job.php:41
hasRootJobParams()
Definition Job.php:337
string $command
Definition Job.php:43
setMetadata( $field, $value)
Definition Job.php:182
getParams()
array Parameters that specify sources, targets, and options for execution
Definition Job.php:157
getReadyTimestamp()
int|null UNIX timestamp of when the job was runnable, or null 1.26
Definition Job.php:224
toString()
string Debugging string describing the job
Definition Job.php:375
getType()
string Job type that defines what sort of changes this job makes
Definition Job.php:142
Title $title
Definition Job.php:52
getRootJobParams()
Definition Job.php:324
hasExecutionFlag( $flag)
bool 1.31
Definition Job.php:134
callable[] $teardownCallbacks
Definition Job.php:61
getQueuedTimestamp()
Definition Job.php:207
bool $removeDuplicates
Expensive jobs may set this to true.
Definition Job.php:55
setLastError( $error)
Definition Job.php:435
getMetadata( $field=null)
Definition Job.php:167
teardown( $status)
Definition Job.php:365
isRootJob()
Definition Job.php:347
addTeardownCallback( $callback)
Definition Job.php:357
array $params
Array of job parameters.
Definition Job.php:46
getTitle()
Definition Job.php:149
array $metadata
Additional queue metadata.
Definition Job.php:49
static factory( $command, $params=[])
Create the appropriate object to handle a specific job.
Definition Job.php:76
int $executionFlags
Bitfield of JOB_* class constants.
Definition Job.php:64
workItemCount()
Definition Job.php:257
getRequestId()
string|null Id of the request that created this job. Follows jobs recursively, allowing to track the ...
Definition Job.php:216
static newRootJobParams( $key)
Get "root job" parameters for a task.
Definition Job.php:310
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Definition Job.php:271
getReleaseTimestamp()
Definition Job.php:198
ignoreDuplicates()
Whether the queue should reject insertion of this job if a duplicate exists.
Definition Job.php:241
getLastError()
string
Definition Job.php:443
string $error
Text for error that occurred last.
Definition Job.php:58
allowRetries()
In some setups (i.e. when using change-propagation) jobs may still be retried even when this is false...
Definition Job.php:249
__construct( $command, $params=null)
Definition Job.php:90
Service for handling telemetry data.
Definition Telemetry.php:29
JSON formatter wrapper class.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:79
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().