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.
34 /** @var array|bool Array of job parameters or false if none */
37 /** @var array Additional queue metadata */
38 public $metadata = array();
43 /** @var bool Expensive jobs may set this to true */
44 protected $removeDuplicates;
46 /** @var string Text for error that occurred last */
49 /*-------------------------------------------------------------------------
51 *------------------------------------------------------------------------*/
55 * @return bool Success
57 abstract public function run();
59 /*-------------------------------------------------------------------------
61 *------------------------------------------------------------------------*/
64 * Create the appropriate object to handle a specific job
66 * @param string $command Job command
67 * @param Title $title Associated title
68 * @param array|bool $params Job parameters
72 public static function factory( $command, Title
$title, $params = false ) {
74 if ( isset( $wgJobClasses[$command] ) ) {
75 $class = $wgJobClasses[$command];
77 return new $class( $title, $params );
79 throw new MWException( "Invalid job command `{$command}`" );
83 * Batch-insert a group of jobs into the queue.
84 * This will be wrapped in a transaction with a forced commit.
86 * This may add duplicate at insert time, but they will be
87 * removed later on, when the first one is popped.
89 * @param array $jobs of Job objects
91 * @deprecated since 1.21
93 public static function batchInsert( $jobs ) {
94 return JobQueueGroup
::singleton()->push( $jobs );
98 * Insert a group of jobs into the queue.
100 * Same as batchInsert() but does not commit and can thus
101 * be rolled-back as part of a larger transaction. However,
102 * large batches of jobs can cause slave lag.
104 * @param array $jobs of Job objects
106 * @deprecated since 1.21
108 public static function safeBatchInsert( $jobs ) {
109 return JobQueueGroup
::singleton()->push( $jobs, JobQueue
::QOS_ATOMIC
);
113 * Pop a job of a certain type. This tries less hard than pop() to
114 * actually find a job; it may be adversely affected by concurrent job
117 * @param $type string
118 * @return Job|bool Returns false if there are no jobs
119 * @deprecated since 1.21
121 public static function pop_type( $type ) {
122 return JobQueueGroup
::singleton()->get( $type )->pop();
126 * Pop a job off the front of the queue.
127 * This is subject to $wgJobTypesExcludedFromDefaultQueue.
129 * @return Job|bool False if there are no jobs
130 * @deprecated since 1.21
132 public static function pop() {
133 return JobQueueGroup
::singleton()->pop();
136 /*-------------------------------------------------------------------------
137 * Non-static functions
138 *------------------------------------------------------------------------*/
143 * @param $params array|bool
145 public function __construct( $command, $title, $params = false ) {
146 $this->command
= $command;
147 $this->title
= $title;
148 $this->params
= $params;
150 // expensive jobs may set this to true
151 $this->removeDuplicates
= false;
157 public function getType() {
158 return $this->command
;
164 public function getTitle() {
171 public function getParams() {
172 return $this->params
;
176 * @return int|null UNIX timestamp to delay running this job until, otherwise null
179 public function getReleaseTimestamp() {
180 return isset( $this->params
['jobReleaseTimestamp'] )
181 ?
wfTimestampOrNull( TS_UNIX
, $this->params
['jobReleaseTimestamp'] )
186 * @return bool Whether only one of each identical set of jobs should be run
188 public function ignoreDuplicates() {
189 return $this->removeDuplicates
;
193 * @return bool Whether this job can be retried on failure by job runners
196 public function allowRetries() {
201 * @return integer Number of actually "work items" handled in this job
202 * @see $wgJobBackoffThrottling
205 public function workItemCount() {
210 * Subclasses may need to override this to make duplication detection work.
211 * The resulting map conveys everything that makes the job unique. This is
212 * only checked if ignoreDuplicates() returns true, meaning that duplicate
213 * jobs are supposed to be ignored.
215 * @return array Map of key/values
218 public function getDeduplicationInfo() {
220 'type' => $this->getType(),
221 'namespace' => $this->getTitle()->getNamespace(),
222 'title' => $this->getTitle()->getDBkey(),
223 'params' => $this->getParams()
225 if ( is_array( $info['params'] ) ) {
226 // Identical jobs with different "root" jobs should count as duplicates
227 unset( $info['params']['rootJobSignature'] );
228 unset( $info['params']['rootJobTimestamp'] );
229 // Likewise for jobs with different delay times
230 unset( $info['params']['jobReleaseTimestamp'] );
237 * @see JobQueue::deduplicateRootJob()
238 * @param string $key A key that identifies the task
239 * @return array Map of:
240 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
241 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
244 public static function newRootJobParams( $key ) {
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 * Insert a single job into the queue.
279 * @return bool true on success
280 * @deprecated since 1.21
282 public function insert() {
283 return JobQueueGroup
::singleton()->push( $this );
289 public function toString() {
291 if ( $this->params
) {
292 foreach ( $this->params
as $key => $value ) {
293 if ( $paramString != '' ) {
296 if ( is_array( $value ) ) {
297 $value = "array(" . count( $value ) . ")";
298 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
299 $value = "object(" . get_class( $value ) . ")";
301 $value = (string)$value;
302 if ( mb_strlen( $value ) > 1024 ) {
303 $value = "string(" . mb_strlen( $value ) . ")";
306 $paramString .= "$key=$value";
310 if ( is_object( $this->title
) ) {
311 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
312 if ( $paramString !== '' ) {
313 $s .= ' ' . $paramString;
318 return "{$this->command} $paramString";
322 protected function setLastError( $error ) {
323 $this->error
= $error;
326 public function getLastError() {