2006-02-24 01:56:31 +00:00
|
|
|
<?php
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2014-01-27 21:28:47 +00:00
|
|
|
* Job queue task base code.
|
2012-05-04 06:29:11 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-09-03 18:24:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @defgroup JobQueue JobQueue
|
|
|
|
|
*/
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
/**
|
2007-04-21 12:33:41 +00:00
|
|
|
* Class to both describe a background job and handle jobs.
|
2012-10-17 14:18:32 +00:00
|
|
|
* The queue aspects of this class are now deprecated.
|
2014-01-27 21:28:47 +00:00
|
|
|
* Using the class to push jobs onto queues is deprecated (use JobSpecification).
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup JobQueue
|
2007-04-04 05:22:37 +00:00
|
|
|
*/
|
2014-01-27 21:28:47 +00:00
|
|
|
abstract class Job implements IJobSpecification {
|
2013-11-25 15:32:17 +00:00
|
|
|
/** @var string */
|
|
|
|
|
public $command;
|
2011-02-19 21:56:54 +00:00
|
|
|
|
2013-11-25 15:32:17 +00:00
|
|
|
/** @var array|bool Array of job parameters or false if none */
|
|
|
|
|
public $params;
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2013-11-25 15:32:17 +00:00
|
|
|
/** @var array Additional queue metadata */
|
2012-12-12 00:04:03 +00:00
|
|
|
public $metadata = array();
|
|
|
|
|
|
2013-11-25 15:32:17 +00:00
|
|
|
/** @var Title */
|
|
|
|
|
protected $title;
|
|
|
|
|
|
|
|
|
|
/** @var bool Expensive jobs may set this to true */
|
|
|
|
|
protected $removeDuplicates;
|
|
|
|
|
|
|
|
|
|
/** @var string Text for error that occurred last */
|
|
|
|
|
protected $error;
|
|
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* Abstract functions
|
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run the job
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return bool Success
|
2007-04-21 12:33:41 +00:00
|
|
|
*/
|
2012-08-29 00:01:31 +00:00
|
|
|
abstract public function run();
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* Static functions
|
|
|
|
|
*------------------------------------------------------------------------*/
|
2006-06-18 12:42:16 +00:00
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/**
|
2007-06-21 19:11:24 +00:00
|
|
|
* Create the appropriate object to handle a specific job
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $command Job command
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param Title $title Associated title
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array|bool $params Job parameters
|
2012-08-02 19:36:21 +00:00
|
|
|
* @throws MWException
|
2007-06-21 19:11:24 +00:00
|
|
|
* @return Job
|
2006-06-18 12:42:16 +00:00
|
|
|
*/
|
2013-12-17 08:26:57 +00:00
|
|
|
public static function factory( $command, Title $title, $params = false ) {
|
2007-06-21 19:11:24 +00:00
|
|
|
global $wgJobClasses;
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( isset( $wgJobClasses[$command] ) ) {
|
2007-06-21 19:11:24 +00:00
|
|
|
$class = $wgJobClasses[$command];
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-12-17 08:26:57 +00:00
|
|
|
return new $class( $title, $params );
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
2007-06-21 19:11:24 +00:00
|
|
|
throw new MWException( "Invalid job command `{$command}`" );
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* Batch-insert a group of jobs into the queue.
|
|
|
|
|
* This will be wrapped in a transaction with a forced commit.
|
|
|
|
|
*
|
|
|
|
|
* This may add duplicate at insert time, but they will be
|
|
|
|
|
* removed later on, when the first one is popped.
|
|
|
|
|
*
|
2014-09-23 20:52:39 +00:00
|
|
|
* @param Job[] $jobs Array of Job objects
|
2012-12-09 03:12:12 +00:00
|
|
|
* @return bool
|
2013-05-15 00:53:49 +00:00
|
|
|
* @deprecated since 1.21
|
2007-04-21 12:33:41 +00:00
|
|
|
*/
|
2012-08-29 00:01:31 +00:00
|
|
|
public static function batchInsert( $jobs ) {
|
2014-04-16 18:07:26 +00:00
|
|
|
JobQueueGroup::singleton()->push( $jobs );
|
|
|
|
|
return true;
|
2011-03-18 07:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Insert a group of jobs into the queue.
|
|
|
|
|
*
|
|
|
|
|
* Same as batchInsert() but does not commit and can thus
|
|
|
|
|
* be rolled-back as part of a larger transaction. However,
|
|
|
|
|
* large batches of jobs can cause slave lag.
|
|
|
|
|
*
|
2014-09-23 20:52:39 +00:00
|
|
|
* @param Job[] $jobs Array of Job objects
|
2012-12-09 03:12:12 +00:00
|
|
|
* @return bool
|
2013-05-15 00:53:49 +00:00
|
|
|
* @deprecated since 1.21
|
2011-03-18 07:06:18 +00:00
|
|
|
*/
|
2012-08-29 00:01:31 +00:00
|
|
|
public static function safeBatchInsert( $jobs ) {
|
2014-04-16 18:07:26 +00:00
|
|
|
JobQueueGroup::singleton()->push( $jobs, JobQueue::QOS_ATOMIC );
|
|
|
|
|
return true;
|
2012-06-05 22:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-17 14:18:32 +00:00
|
|
|
/**
|
|
|
|
|
* Pop a job of a certain type. This tries less hard than pop() to
|
|
|
|
|
* actually find a job; it may be adversely affected by concurrent job
|
|
|
|
|
* runners.
|
|
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param string $type
|
2013-03-12 02:01:50 +00:00
|
|
|
* @return Job|bool Returns false if there are no jobs
|
2013-05-15 00:53:49 +00:00
|
|
|
* @deprecated since 1.21
|
2012-10-17 14:18:32 +00:00
|
|
|
*/
|
|
|
|
|
public static function pop_type( $type ) {
|
|
|
|
|
return JobQueueGroup::singleton()->get( $type )->pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pop a job off the front of the queue.
|
|
|
|
|
* This is subject to $wgJobTypesExcludedFromDefaultQueue.
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return Job|bool False if there are no jobs
|
2013-05-15 00:53:49 +00:00
|
|
|
* @deprecated since 1.21
|
2012-10-17 14:18:32 +00:00
|
|
|
*/
|
|
|
|
|
public static function pop() {
|
|
|
|
|
return JobQueueGroup::singleton()->pop();
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* Non-static functions
|
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
|
|
2011-02-28 16:37:34 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param string $command
|
|
|
|
|
* @param Title $title
|
2014-09-23 20:52:39 +00:00
|
|
|
* @param array|bool $params Can not be === true
|
2011-02-28 16:37:34 +00:00
|
|
|
*/
|
2013-12-16 06:31:07 +00:00
|
|
|
public function __construct( $command, $title, $params = false ) {
|
2006-02-24 01:56:31 +00:00
|
|
|
$this->command = $command;
|
|
|
|
|
$this->title = $title;
|
|
|
|
|
$this->params = $params;
|
|
|
|
|
|
2013-11-25 17:12:42 +00:00
|
|
|
// expensive jobs may set this to true
|
|
|
|
|
$this->removeDuplicates = false;
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getType() {
|
|
|
|
|
return $this->command;
|
|
|
|
|
}
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
|
|
|
|
* @return Title
|
|
|
|
|
*/
|
|
|
|
|
public function getTitle() {
|
|
|
|
|
return $this->title;
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2012-08-29 00:01:31 +00:00
|
|
|
public function getParams() {
|
|
|
|
|
return $this->params;
|
2006-05-31 23:56:41 +00:00
|
|
|
}
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2013-03-12 03:40:01 +00:00
|
|
|
/**
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return int|null UNIX timestamp to delay running this job until, otherwise null
|
2013-03-12 03:40:01 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
public function getReleaseTimestamp() {
|
|
|
|
|
return isset( $this->params['jobReleaseTimestamp'] )
|
|
|
|
|
? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
|
|
|
|
|
: null;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-10 11:39:23 +00:00
|
|
|
/**
|
2013-02-20 00:56:47 +00:00
|
|
|
* @return bool Whether only one of each identical set of jobs should be run
|
2011-03-10 11:39:23 +00:00
|
|
|
*/
|
2012-08-29 00:01:31 +00:00
|
|
|
public function ignoreDuplicates() {
|
|
|
|
|
return $this->removeDuplicates;
|
|
|
|
|
}
|
2011-03-10 11:39:23 +00:00
|
|
|
|
2013-02-20 00:56:47 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool Whether this job can be retried on failure by job runners
|
2013-04-03 20:13:36 +00:00
|
|
|
* @since 1.21
|
2013-02-20 00:56:47 +00:00
|
|
|
*/
|
|
|
|
|
public function allowRetries() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 20:57:45 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return int Number of actually "work items" handled in this job
|
2013-12-21 20:57:45 +00:00
|
|
|
* @see $wgJobBackoffThrottling
|
|
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
|
|
|
|
public function workItemCount() {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 22:01:40 +00:00
|
|
|
/**
|
2013-04-03 20:51:02 +00:00
|
|
|
* Subclasses may need to override this to make duplication detection work.
|
|
|
|
|
* The resulting map conveys everything that makes the job unique. This is
|
|
|
|
|
* only checked if ignoreDuplicates() returns true, meaning that duplicate
|
|
|
|
|
* jobs are supposed to be ignored.
|
2012-11-08 22:01:40 +00:00
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return array Map of key/values
|
2013-04-03 20:13:36 +00:00
|
|
|
* @since 1.21
|
2012-11-08 22:01:40 +00:00
|
|
|
*/
|
|
|
|
|
public function getDeduplicationInfo() {
|
|
|
|
|
$info = array(
|
2013-04-20 17:18:13 +00:00
|
|
|
'type' => $this->getType(),
|
2012-11-08 22:01:40 +00:00
|
|
|
'namespace' => $this->getTitle()->getNamespace(),
|
2013-04-20 17:18:13 +00:00
|
|
|
'title' => $this->getTitle()->getDBkey(),
|
|
|
|
|
'params' => $this->getParams()
|
2012-11-08 22:01:40 +00:00
|
|
|
);
|
|
|
|
|
if ( is_array( $info['params'] ) ) {
|
2013-03-27 21:26:47 +00:00
|
|
|
// Identical jobs with different "root" jobs should count as duplicates
|
2012-11-08 22:01:40 +00:00
|
|
|
unset( $info['params']['rootJobSignature'] );
|
|
|
|
|
unset( $info['params']['rootJobTimestamp'] );
|
2013-03-27 21:26:47 +00:00
|
|
|
// Likewise for jobs with different delay times
|
|
|
|
|
unset( $info['params']['jobReleaseTimestamp'] );
|
2012-11-08 22:01:40 +00:00
|
|
|
}
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2012-11-08 22:01:40 +00:00
|
|
|
return $info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-03 20:51:02 +00:00
|
|
|
* @see JobQueue::deduplicateRootJob()
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $key A key that identifies the task
|
2013-11-28 06:43:00 +00:00
|
|
|
* @return array Map of:
|
|
|
|
|
* - rootJobSignature : hash (e.g. SHA1) that identifies the task
|
|
|
|
|
* - rootJobTimestamp : TS_MW timestamp of this instance of the task
|
2013-04-03 20:13:36 +00:00
|
|
|
* @since 1.21
|
2012-11-08 22:01:40 +00:00
|
|
|
*/
|
|
|
|
|
public static function newRootJobParams( $key ) {
|
|
|
|
|
return array(
|
|
|
|
|
'rootJobSignature' => sha1( $key ),
|
|
|
|
|
'rootJobTimestamp' => wfTimestampNow()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-03 20:51:02 +00:00
|
|
|
* @see JobQueue::deduplicateRootJob()
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return array
|
2013-04-03 20:51:02 +00:00
|
|
|
* @since 1.21
|
2012-11-08 22:01:40 +00:00
|
|
|
*/
|
|
|
|
|
public function getRootJobParams() {
|
|
|
|
|
return array(
|
|
|
|
|
'rootJobSignature' => isset( $this->params['rootJobSignature'] )
|
|
|
|
|
? $this->params['rootJobSignature']
|
|
|
|
|
: null,
|
|
|
|
|
'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
|
|
|
|
|
? $this->params['rootJobTimestamp']
|
|
|
|
|
: null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-03 20:51:02 +00:00
|
|
|
/**
|
|
|
|
|
* @see JobQueue::deduplicateRootJob()
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
public function hasRootJobParams() {
|
|
|
|
|
return isset( $this->params['rootJobSignature'] )
|
|
|
|
|
&& isset( $this->params['rootJobTimestamp'] );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
|
|
|
|
* Insert a single job into the queue.
|
2014-07-24 17:43:03 +00:00
|
|
|
* @return bool True on success
|
2013-05-15 00:53:49 +00:00
|
|
|
* @deprecated since 1.21
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
public function insert() {
|
2014-04-16 18:07:26 +00:00
|
|
|
JobQueueGroup::singleton()->push( $this );
|
|
|
|
|
return true;
|
2011-03-10 11:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2012-08-29 00:01:31 +00:00
|
|
|
public function toString() {
|
2014-07-20 19:41:41 +00:00
|
|
|
$truncFunc = function ( $value ) {
|
2014-05-16 22:23:26 +00:00
|
|
|
$value = (string)$value;
|
|
|
|
|
if ( mb_strlen( $value ) > 1024 ) {
|
|
|
|
|
$value = "string(" . mb_strlen( $value ) . ")";
|
|
|
|
|
}
|
|
|
|
|
return $value;
|
|
|
|
|
};
|
|
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
$paramString = '';
|
|
|
|
|
if ( $this->params ) {
|
|
|
|
|
foreach ( $this->params as $key => $value ) {
|
|
|
|
|
if ( $paramString != '' ) {
|
|
|
|
|
$paramString .= ' ';
|
2006-06-15 06:35:24 +00:00
|
|
|
}
|
2013-01-23 18:39:49 +00:00
|
|
|
if ( is_array( $value ) ) {
|
2014-05-16 22:23:26 +00:00
|
|
|
$filteredValue = array();
|
|
|
|
|
foreach ( $value as $k => $v ) {
|
|
|
|
|
if ( is_scalar( $v ) ) {
|
|
|
|
|
$filteredValue[$k] = $truncFunc( $v );
|
|
|
|
|
} else {
|
|
|
|
|
$filteredValue = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $filteredValue ) {
|
|
|
|
|
$value = FormatJson::encode( $filteredValue );
|
|
|
|
|
} else {
|
|
|
|
|
$value = "array(" . count( $value ) . ")";
|
|
|
|
|
}
|
2013-02-15 04:11:26 +00:00
|
|
|
} elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
|
2013-01-23 18:39:49 +00:00
|
|
|
$value = "object(" . get_class( $value ) . ")";
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-16 22:23:26 +00:00
|
|
|
$paramString .= "$key={$truncFunc( $value )}";
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_object( $this->title ) ) {
|
|
|
|
|
$s = "{$this->command} " . $this->title->getPrefixedDBkey();
|
|
|
|
|
if ( $paramString !== '' ) {
|
|
|
|
|
$s .= ' ' . $paramString;
|
|
|
|
|
}
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
return $s;
|
|
|
|
|
} else {
|
|
|
|
|
return "{$this->command} $paramString";
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-22 22:44:34 +00:00
|
|
|
protected function setLastError( $error ) {
|
|
|
|
|
$this->error = $error;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
public function getLastError() {
|
2006-06-18 12:42:16 +00:00
|
|
|
return $this->error;
|
|
|
|
|
}
|
|
|
|
|
}
|