MediaWiki REL1_27
Job.php
Go to the documentation of this file.
1<?php
31abstract class Job implements IJobSpecification {
33 public $command;
34
36 public $params;
37
39 public $metadata = [];
40
42 protected $title;
43
46
48 protected $error;
49
51 protected $teardownCallbacks = [];
52
57 abstract public function run();
58
68 public static function factory( $command, Title $title, $params = [] ) {
70
71 if ( isset( $wgJobClasses[$command] ) ) {
72 $class = $wgJobClasses[$command];
73
74 $job = new $class( $title, $params );
75 $job->command = $command;
76
77 return $job;
78 }
79
80 throw new InvalidArgumentException( "Invalid job command '{$command}'" );
81 }
82
88 public function __construct( $command, $title, $params = false ) {
89 $this->command = $command;
90 $this->title = $title;
91 $this->params = is_array( $params ) ? $params : []; // sanity
92
93 // expensive jobs may set this to true
94 $this->removeDuplicates = false;
95
96 if ( !isset( $this->params['requestId'] ) ) {
97 $this->params['requestId'] = WebRequest::getRequestId();
98 }
99 }
100
112 public static function batchInsert( $jobs ) {
113 wfDeprecated( __METHOD__, '1.21' );
114 JobQueueGroup::singleton()->push( $jobs );
115 return true;
116 }
117
121 public function getType() {
122 return $this->command;
123 }
124
128 public function getTitle() {
129 return $this->title;
130 }
131
135 public function getParams() {
136 return $this->params;
137 }
138
143 public function getReleaseTimestamp() {
144 return isset( $this->params['jobReleaseTimestamp'] )
145 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
146 : null;
147 }
148
153 public function getQueuedTimestamp() {
154 return isset( $this->metadata['timestamp'] )
155 ? wfTimestampOrNull( TS_UNIX, $this->metadata['timestamp'] )
156 : null;
157 }
158
165 public function getRequestId() {
166 return isset( $this->params['requestId'] )
167 ? $this->params['requestId']
168 : null;
169 }
170
175 public function getReadyTimestamp() {
176 return $this->getReleaseTimestamp() ?: $this->getQueuedTimestamp();
177 }
178
190 public function ignoreDuplicates() {
192 }
193
198 public function allowRetries() {
199 return true;
200 }
201
207 public function workItemCount() {
208 return 1;
209 }
210
220 public function getDeduplicationInfo() {
221 $info = [
222 'type' => $this->getType(),
223 'namespace' => $this->getTitle()->getNamespace(),
224 'title' => $this->getTitle()->getDBkey(),
225 'params' => $this->getParams()
226 ];
227 if ( is_array( $info['params'] ) ) {
228 // Identical jobs with different "root" jobs should count as duplicates
229 unset( $info['params']['rootJobSignature'] );
230 unset( $info['params']['rootJobTimestamp'] );
231 // Likewise for jobs with different delay times
232 unset( $info['params']['jobReleaseTimestamp'] );
233 // Identical jobs from different requests should count as duplicates
234 unset( $info['params']['requestId'] );
235 // Queues pack and hash this array, so normalize the order
236 ksort( $info['params'] );
237 }
238
239 return $info;
240 }
241
261 public static function newRootJobParams( $key ) {
262 return [
263 'rootJobIsSelf' => true,
264 'rootJobSignature' => sha1( $key ),
265 'rootJobTimestamp' => wfTimestampNow()
266 ];
267 }
268
274 public function getRootJobParams() {
275 return [
276 'rootJobSignature' => isset( $this->params['rootJobSignature'] )
277 ? $this->params['rootJobSignature']
278 : null,
279 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
280 ? $this->params['rootJobTimestamp']
281 : null
282 ];
283 }
284
290 public function hasRootJobParams() {
291 return isset( $this->params['rootJobSignature'] )
292 && isset( $this->params['rootJobTimestamp'] );
293 }
294
299 public function isRootJob() {
300 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
301 }
302
307 protected function addTeardownCallback( $callback ) {
308 $this->teardownCallbacks[] = $callback;
309 }
310
316 public function teardown() {
317 foreach ( $this->teardownCallbacks as $callback ) {
318 call_user_func( $callback );
319 }
320 }
321
327 public function insert() {
328 JobQueueGroup::singleton()->push( $this );
329 return true;
330 }
331
335 public function toString() {
336 $paramString = '';
337 if ( $this->params ) {
338 foreach ( $this->params as $key => $value ) {
339 if ( $paramString != '' ) {
340 $paramString .= ' ';
341 }
342 if ( is_array( $value ) ) {
343 $filteredValue = [];
344 foreach ( $value as $k => $v ) {
345 $json = FormatJson::encode( $v );
346 if ( $json === false || mb_strlen( $json ) > 512 ) {
347 $filteredValue[$k] = gettype( $v ) . '(...)';
348 } else {
349 $filteredValue[$k] = $v;
350 }
351 }
352 if ( count( $filteredValue ) <= 10 ) {
353 $value = FormatJson::encode( $filteredValue );
354 } else {
355 $value = "array(" . count( $value ) . ")";
356 }
357 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
358 $value = "object(" . get_class( $value ) . ")";
359 }
360
361 $flatValue = (string)$value;
362 if ( mb_strlen( $value ) > 1024 ) {
363 $flatValue = "string(" . mb_strlen( $value ) . ")";
364 }
365
366 $paramString .= "$key={$flatValue}";
367 }
368 }
369
370 $metaString = '';
371 foreach ( $this->metadata as $key => $value ) {
372 if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) {
373 $metaString .= ( $metaString ? ",$key=$value" : "$key=$value" );
374 }
375 }
376
378 if ( is_object( $this->title ) ) {
379 $s .= " {$this->title->getPrefixedDBkey()}";
380 }
381 if ( $paramString != '' ) {
382 $s .= " $paramString";
383 }
384 if ( $metaString != '' ) {
385 $s .= " ($metaString)";
386 }
387
388 return $s;
389 }
390
391 protected function setLastError( $error ) {
392 $this->error = $error;
393 }
394
395 public function getLastError() {
396 return $this->error;
397 }
398}
$wgJobClasses
Maps jobs to their handling classes; extensions can add to this to provide custom jobs.
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.
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
static singleton( $wiki=false)
Class to both describe a background job and handle jobs.
Definition Job.php:31
hasRootJobParams()
Definition Job.php:290
string $command
Definition Job.php:33
insert()
Insert a single job into the queue.
Definition Job.php:327
getParams()
Definition Job.php:135
getReadyTimestamp()
Definition Job.php:175
static batchInsert( $jobs)
Batch-insert a group of jobs into the queue.
Definition Job.php:112
toString()
Definition Job.php:335
getType()
Definition Job.php:121
Title $title
Definition Job.php:42
getRootJobParams()
Definition Job.php:274
static factory( $command, Title $title, $params=[])
Create the appropriate object to handle a specific job.
Definition Job.php:68
callable[] $teardownCallbacks
Definition Job.php:51
getQueuedTimestamp()
Definition Job.php:153
bool $removeDuplicates
Expensive jobs may set this to true.
Definition Job.php:45
setLastError( $error)
Definition Job.php:391
isRootJob()
Definition Job.php:299
addTeardownCallback( $callback)
Definition Job.php:307
run()
Run the job.
array $params
Array of job parameters.
Definition Job.php:36
getTitle()
Definition Job.php:128
array $metadata
Additional queue metadata.
Definition Job.php:39
teardown()
Do any final cleanup after run(), deferred updates, and all DB commits happen.
Definition Job.php:316
workItemCount()
Definition Job.php:207
__construct( $command, $title, $params=false)
Definition Job.php:88
getRequestId()
Definition Job.php:165
static newRootJobParams( $key)
Get "root job" parameters for a task.
Definition Job.php:261
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Definition Job.php:220
getReleaseTimestamp()
Definition Job.php:143
ignoreDuplicates()
Whether the queue should reject insertion of this job if a duplicate exists.
Definition Job.php:190
getLastError()
Definition Job.php:395
string $error
Text for error that occurred last.
Definition Job.php:48
allowRetries()
Definition Job.php:198
Represents a title within MediaWiki.
Definition Title.php:34
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:183
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Job queue task description interface.
title
if(count( $args)< 1) $job