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 * The queue aspects of this class are now deprecated.
27 * Using the class to push jobs onto queues is deprecated (use JobSpecification).
31 abstract class Job
implements IJobSpecification
{
35 /** @var array Array of job parameters */
38 /** @var array Additional queue metadata */
39 public $metadata = [];
44 /** @var bool Expensive jobs may set this to true */
45 protected $removeDuplicates;
47 /** @var string Text for error that occurred last */
50 /** @var callable[] */
51 protected $teardownCallbacks = [];
55 * @return bool Success
57 abstract public function run();
60 * Create the appropriate object to handle a specific job
62 * @param string $command Job command
63 * @param Title $title Associated title
64 * @param array $params Job parameters
68 public static function factory( $command, Title
$title, $params = [] ) {
71 if ( isset( $wgJobClasses[$command] ) ) {
72 $class = $wgJobClasses[$command];
74 $job = new $class( $title, $params );
75 $job->command
= $command;
80 throw new InvalidArgumentException( "Invalid job command '{$command}'" );
84 * @param string $command
86 * @param array|bool $params Can not be === true
88 public function __construct( $command, $title, $params = false ) {
89 $this->command
= $command;
90 $this->title
= $title;
91 $this->params
= is_array( $params ) ?
$params : []; // sanity
93 // expensive jobs may set this to true
94 $this->removeDuplicates
= false;
98 * Batch-insert a group of jobs into the queue.
99 * This will be wrapped in a transaction with a forced commit.
101 * This may add duplicate at insert time, but they will be
102 * removed later on, when the first one is popped.
104 * @param Job[] $jobs Array of Job objects
106 * @deprecated since 1.21
108 public static function batchInsert( $jobs ) {
109 wfDeprecated( __METHOD__
, '1.21' );
110 JobQueueGroup
::singleton()->push( $jobs );
117 public function getType() {
118 return $this->command
;
124 public function getTitle() {
131 public function getParams() {
132 return $this->params
;
136 * @return int|null UNIX timestamp to delay running this job until, otherwise null
139 public function getReleaseTimestamp() {
140 return isset( $this->params
['jobReleaseTimestamp'] )
141 ?
wfTimestampOrNull( TS_UNIX
, $this->params
['jobReleaseTimestamp'] )
146 * @return int|null UNIX timestamp of when the job was queued, or null
149 public function getQueuedTimestamp() {
150 return isset( $this->metadata
['timestamp'] )
151 ?
wfTimestampOrNull( TS_UNIX
, $this->metadata
['timestamp'] )
156 * @return int|null UNIX timestamp of when the job was runnable, or null
159 public function getReadyTimestamp() {
160 return $this->getReleaseTimestamp() ?
: $this->getQueuedTimestamp();
164 * Whether the queue should reject insertion of this job if a duplicate exists
166 * This can be used to avoid duplicated effort or combined with delayed jobs to
167 * coalesce updates into larger batches. Claimed jobs are never treated as
168 * duplicates of new jobs, and some queues may allow a few duplicates due to
169 * network partitions and fail-over. Thus, additional locking is needed to
170 * enforce mutual exclusion if this is really needed.
174 public function ignoreDuplicates() {
175 return $this->removeDuplicates
;
179 * @return bool Whether this job can be retried on failure by job runners
182 public function allowRetries() {
187 * @return int Number of actually "work items" handled in this job
188 * @see $wgJobBackoffThrottling
191 public function workItemCount() {
196 * Subclasses may need to override this to make duplication detection work.
197 * The resulting map conveys everything that makes the job unique. This is
198 * only checked if ignoreDuplicates() returns true, meaning that duplicate
199 * jobs are supposed to be ignored.
201 * @return array Map of key/values
204 public function getDeduplicationInfo() {
206 'type' => $this->getType(),
207 'namespace' => $this->getTitle()->getNamespace(),
208 'title' => $this->getTitle()->getDBkey(),
209 'params' => $this->getParams()
211 if ( is_array( $info['params'] ) ) {
212 // Identical jobs with different "root" jobs should count as duplicates
213 unset( $info['params']['rootJobSignature'] );
214 unset( $info['params']['rootJobTimestamp'] );
215 // Likewise for jobs with different delay times
216 unset( $info['params']['jobReleaseTimestamp'] );
217 // Queues pack and hash this array, so normalize the order
218 ksort( $info['params'] );
225 * Get "root job" parameters for a task
227 * This is used to no-op redundant jobs, including child jobs of jobs,
228 * as long as the children inherit the root job parameters. When a job
229 * with root job parameters and "rootJobIsSelf" set is pushed, the
230 * deduplicateRootJob() method is automatically called on it. If the
231 * root job is only virtual and not actually pushed (e.g. the sub-jobs
232 * are inserted directly), then call deduplicateRootJob() directly.
234 * @see JobQueue::deduplicateRootJob()
236 * @param string $key A key that identifies the task
237 * @return array Map of:
238 * - rootJobIsSelf : true
239 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
240 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
243 public static function newRootJobParams( $key ) {
245 'rootJobIsSelf' => true,
246 'rootJobSignature' => sha1( $key ),
247 'rootJobTimestamp' => wfTimestampNow()
252 * @see JobQueue::deduplicateRootJob()
256 public function getRootJobParams() {
258 'rootJobSignature' => isset( $this->params
['rootJobSignature'] )
259 ?
$this->params
['rootJobSignature']
261 'rootJobTimestamp' => isset( $this->params
['rootJobTimestamp'] )
262 ?
$this->params
['rootJobTimestamp']
268 * @see JobQueue::deduplicateRootJob()
272 public function hasRootJobParams() {
273 return isset( $this->params
['rootJobSignature'] )
274 && isset( $this->params
['rootJobTimestamp'] );
278 * @see JobQueue::deduplicateRootJob()
279 * @return bool Whether this is job is a root job
281 public function isRootJob() {
282 return $this->hasRootJobParams() && !empty( $this->params
['rootJobIsSelf'] );
286 * @param callable $callback
289 protected function addTeardownCallback( $callback ) {
290 $this->teardownCallbacks
[] = $callback;
294 * Do any final cleanup after run(), deferred updates, and all DB commits happen
298 public function teardown() {
299 foreach ( $this->teardownCallbacks
as $callback ) {
300 call_user_func( $callback );
305 * Insert a single job into the queue.
306 * @return bool True on success
307 * @deprecated since 1.21
309 public function insert() {
310 JobQueueGroup
::singleton()->push( $this );
317 public function toString() {
318 $truncFunc = function ( $value ) {
319 $value = (string)$value;
320 if ( mb_strlen( $value ) > 1024 ) {
321 $value = "string(" . mb_strlen( $value ) . ")";
327 if ( $this->params
) {
328 foreach ( $this->params
as $key => $value ) {
329 if ( $paramString != '' ) {
332 if ( is_array( $value ) ) {
334 foreach ( $value as $k => $v ) {
335 if ( is_scalar( $v ) ) {
336 $filteredValue[$k] = $truncFunc( $v );
338 $filteredValue = null;
342 if ( $filteredValue && count( $filteredValue ) < 10 ) {
343 $value = FormatJson
::encode( $filteredValue );
345 $value = "array(" . count( $value ) . ")";
347 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
348 $value = "object(" . get_class( $value ) . ")";
351 $paramString .= "$key={$truncFunc( $value )}";
356 foreach ( $this->metadata
as $key => $value ) {
357 if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) {
358 $metaString .= ( $metaString ?
",$key=$value" : "$key=$value" );
363 if ( is_object( $this->title
) ) {
364 $s .= " {$this->title->getPrefixedDBkey()}";
366 if ( $paramString != '' ) {
367 $s .= " $paramString";
369 if ( $metaString != '' ) {
370 $s .= " ($metaString)";
376 protected function setLastError( $error ) {
377 $this->error
= $error;
380 public function getLastError() {