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.
31 /** @var int Job identifier */
37 /** @var array|bool Array of job parameters or false if none */
40 /** @var array Additional queue metadata */
41 public $metadata = array();
46 /** @var bool Expensive jobs may set this to true */
47 protected $removeDuplicates;
49 /** @var string Text for error that occurred last */
52 /*-------------------------------------------------------------------------
54 *------------------------------------------------------------------------*/
58 * @return bool Success
60 abstract public function run();
62 /*-------------------------------------------------------------------------
64 *------------------------------------------------------------------------*/
67 * Create the appropriate object to handle a specific job
69 * @param string $command Job command
70 * @param Title $title Associated title
71 * @param array|bool $params Job parameters
72 * @param int $id Job identifier
76 public static function factory( $command, Title
$title, $params = false, $id = 0 ) {
78 if ( isset( $wgJobClasses[$command] ) ) {
79 $class = $wgJobClasses[$command];
81 return new $class( $title, $params, $id );
83 throw new MWException( "Invalid job command `{$command}`" );
87 * Batch-insert a group of jobs into the queue.
88 * This will be wrapped in a transaction with a forced commit.
90 * This may add duplicate at insert time, but they will be
91 * removed later on, when the first one is popped.
93 * @param array $jobs of Job objects
95 * @deprecated since 1.21
97 public static function batchInsert( $jobs ) {
98 return JobQueueGroup
::singleton()->push( $jobs );
102 * Insert a group of jobs into the queue.
104 * Same as batchInsert() but does not commit and can thus
105 * be rolled-back as part of a larger transaction. However,
106 * large batches of jobs can cause slave lag.
108 * @param array $jobs of Job objects
110 * @deprecated since 1.21
112 public static function safeBatchInsert( $jobs ) {
113 return JobQueueGroup
::singleton()->push( $jobs, JobQueue
::QOS_ATOMIC
);
117 * Pop a job of a certain type. This tries less hard than pop() to
118 * actually find a job; it may be adversely affected by concurrent job
121 * @param $type string
122 * @return Job|bool Returns false if there are no jobs
123 * @deprecated since 1.21
125 public static function pop_type( $type ) {
126 return JobQueueGroup
::singleton()->get( $type )->pop();
130 * Pop a job off the front of the queue.
131 * This is subject to $wgJobTypesExcludedFromDefaultQueue.
133 * @return Job|bool False if there are no jobs
134 * @deprecated since 1.21
136 public static function pop() {
137 return JobQueueGroup
::singleton()->pop();
140 /*-------------------------------------------------------------------------
141 * Non-static functions
142 *------------------------------------------------------------------------*/
147 * @param $params array|bool
150 public function __construct( $command, $title, $params = false, $id = 0 ) {
151 $this->command
= $command;
152 $this->title
= $title;
153 $this->params
= $params;
156 // expensive jobs may set this to true
157 $this->removeDuplicates
= false;
161 * @return int May be 0 for jobs stored outside the DB
162 * @deprecated since 1.22
164 public function getId() {
171 public function getType() {
172 return $this->command
;
178 public function getTitle() {
185 public function getParams() {
186 return $this->params
;
190 * @return int|null UNIX timestamp to delay running this job until, otherwise null
193 public function getReleaseTimestamp() {
194 return isset( $this->params
['jobReleaseTimestamp'] )
195 ?
wfTimestampOrNull( TS_UNIX
, $this->params
['jobReleaseTimestamp'] )
200 * @return bool Whether only one of each identical set of jobs should be run
202 public function ignoreDuplicates() {
203 return $this->removeDuplicates
;
207 * @return bool Whether this job can be retried on failure by job runners
210 public function allowRetries() {
215 * Subclasses may need to override this to make duplication detection work.
216 * The resulting map conveys everything that makes the job unique. This is
217 * only checked if ignoreDuplicates() returns true, meaning that duplicate
218 * jobs are supposed to be ignored.
220 * @return array Map of key/values
223 public function getDeduplicationInfo() {
225 'type' => $this->getType(),
226 'namespace' => $this->getTitle()->getNamespace(),
227 'title' => $this->getTitle()->getDBkey(),
228 'params' => $this->getParams()
230 if ( is_array( $info['params'] ) ) {
231 // Identical jobs with different "root" jobs should count as duplicates
232 unset( $info['params']['rootJobSignature'] );
233 unset( $info['params']['rootJobTimestamp'] );
234 // Likewise for jobs with different delay times
235 unset( $info['params']['jobReleaseTimestamp'] );
242 * @see JobQueue::deduplicateRootJob()
243 * @param string $key A key that identifies the task
244 * @return array Map of:
245 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
246 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
249 public static function newRootJobParams( $key ) {
251 'rootJobSignature' => sha1( $key ),
252 'rootJobTimestamp' => wfTimestampNow()
257 * @see JobQueue::deduplicateRootJob()
261 public function getRootJobParams() {
263 'rootJobSignature' => isset( $this->params
['rootJobSignature'] )
264 ?
$this->params
['rootJobSignature']
266 'rootJobTimestamp' => isset( $this->params
['rootJobTimestamp'] )
267 ?
$this->params
['rootJobTimestamp']
273 * @see JobQueue::deduplicateRootJob()
277 public function hasRootJobParams() {
278 return isset( $this->params
['rootJobSignature'] )
279 && isset( $this->params
['rootJobTimestamp'] );
283 * Insert a single job into the queue.
284 * @return bool true on success
285 * @deprecated since 1.21
287 public function insert() {
288 return JobQueueGroup
::singleton()->push( $this );
294 public function toString() {
296 if ( $this->params
) {
297 foreach ( $this->params
as $key => $value ) {
298 if ( $paramString != '' ) {
301 if ( is_array( $value ) ) {
302 $value = "array(" . count( $value ) . ")";
303 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
304 $value = "object(" . get_class( $value ) . ")";
306 $value = (string)$value;
307 if ( mb_strlen( $value ) > 1024 ) {
308 $value = "string(" . mb_strlen( $value ) . ")";
311 $paramString .= "$key=$value";
315 if ( is_object( $this->title
) ) {
316 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
317 if ( $paramString !== '' ) {
318 $s .= ' ' . $paramString;
323 return "{$this->command} $paramString";
327 protected function setLastError( $error ) {
328 $this->error
= $error;
331 public function getLastError() {