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 = array();
44 /** @var bool Expensive jobs may set this to true */
45 protected $removeDuplicates;
47 /** @var string Text for error that occurred last */
52 * @return bool Success
54 abstract public function run();
57 * Create the appropriate object to handle a specific job
59 * @param string $command Job command
60 * @param Title $title Associated title
61 * @param array $params Job parameters
65 public static function factory( $command, Title
$title, $params = array() ) {
67 if ( isset( $wgJobClasses[$command] ) ) {
68 $class = $wgJobClasses[$command];
70 return new $class( $title, $params );
72 throw new MWException( "Invalid job command `{$command}`" );
76 * @param string $command
78 * @param array|bool $params Can not be === true
80 public function __construct( $command, $title, $params = false ) {
81 $this->command
= $command;
82 $this->title
= $title;
83 $this->params
= is_array( $params ) ?
$params : array(); // sanity
85 // expensive jobs may set this to true
86 $this->removeDuplicates
= false;
90 * Batch-insert a group of jobs into the queue.
91 * This will be wrapped in a transaction with a forced commit.
93 * This may add duplicate at insert time, but they will be
94 * removed later on, when the first one is popped.
96 * @param Job[] $jobs Array of Job objects
98 * @deprecated since 1.21
100 public static function batchInsert( $jobs ) {
101 wfDeprecated( __METHOD__
, '1.21' );
102 JobQueueGroup
::singleton()->push( $jobs );
109 public function getType() {
110 return $this->command
;
116 public function getTitle() {
123 public function getParams() {
124 return $this->params
;
128 * @return int|null UNIX timestamp to delay running this job until, otherwise null
131 public function getReleaseTimestamp() {
132 return isset( $this->params
['jobReleaseTimestamp'] )
133 ?
wfTimestampOrNull( TS_UNIX
, $this->params
['jobReleaseTimestamp'] )
138 * @return int|null UNIX timestamp of when the job was queued, or null
141 public function getQueuedTimestamp() {
142 return isset( $this->metadata
['timestamp'] )
143 ?
wfTimestampOrNull( TS_UNIX
, $this->metadata
['timestamp'] )
148 * Whether the queue should reject insertion of this job if a duplicate exists
150 * This can be used to avoid duplicated effort or combined with delayed jobs to
151 * coalesce updates into larger batches. Claimed jobs are never treated as
152 * duplicates of new jobs, and some queues may allow a few duplicates due to
153 * network partitions and fail-over. Thus, additional locking is needed to
154 * enforce mutual exclusion if this is really needed.
158 public function ignoreDuplicates() {
159 return $this->removeDuplicates
;
163 * @return bool Whether this job can be retried on failure by job runners
166 public function allowRetries() {
171 * @return int Number of actually "work items" handled in this job
172 * @see $wgJobBackoffThrottling
175 public function workItemCount() {
180 * Subclasses may need to override this to make duplication detection work.
181 * The resulting map conveys everything that makes the job unique. This is
182 * only checked if ignoreDuplicates() returns true, meaning that duplicate
183 * jobs are supposed to be ignored.
185 * @return array Map of key/values
188 public function getDeduplicationInfo() {
190 'type' => $this->getType(),
191 'namespace' => $this->getTitle()->getNamespace(),
192 'title' => $this->getTitle()->getDBkey(),
193 'params' => $this->getParams()
195 if ( is_array( $info['params'] ) ) {
196 // Identical jobs with different "root" jobs should count as duplicates
197 unset( $info['params']['rootJobSignature'] );
198 unset( $info['params']['rootJobTimestamp'] );
199 // Likewise for jobs with different delay times
200 unset( $info['params']['jobReleaseTimestamp'] );
201 // Queues pack and hash this array, so normalize the order
202 ksort( $info['params'] );
209 * Get "root job" parameters for a task
211 * This is used to no-op redundant jobs, including child jobs of jobs,
212 * as long as the children inherit the root job parameters. When a job
213 * with root job parameters and "rootJobIsSelf" set is pushed, the
214 * deduplicateRootJob() method is automatically called on it. If the
215 * root job is only virtual and not actually pushed (e.g. the sub-jobs
216 * are inserted directly), then call deduplicateRootJob() directly.
218 * @see JobQueue::deduplicateRootJob()
220 * @param string $key A key that identifies the task
221 * @return array Map of:
222 * - rootJobIsSelf : true
223 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
224 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
227 public static function newRootJobParams( $key ) {
229 'rootJobIsSelf' => true,
230 'rootJobSignature' => sha1( $key ),
231 'rootJobTimestamp' => wfTimestampNow()
236 * @see JobQueue::deduplicateRootJob()
240 public function getRootJobParams() {
242 'rootJobSignature' => isset( $this->params
['rootJobSignature'] )
243 ?
$this->params
['rootJobSignature']
245 'rootJobTimestamp' => isset( $this->params
['rootJobTimestamp'] )
246 ?
$this->params
['rootJobTimestamp']
252 * @see JobQueue::deduplicateRootJob()
256 public function hasRootJobParams() {
257 return isset( $this->params
['rootJobSignature'] )
258 && isset( $this->params
['rootJobTimestamp'] );
262 * @see JobQueue::deduplicateRootJob()
263 * @return bool Whether this is job is a root job
265 public function isRootJob() {
266 return $this->hasRootJobParams() && !empty( $this->params
['rootJobIsSelf'] );
270 * Insert a single job into the queue.
271 * @return bool True on success
272 * @deprecated since 1.21
274 public function insert() {
275 JobQueueGroup
::singleton()->push( $this );
282 public function toString() {
283 $truncFunc = function ( $value ) {
284 $value = (string)$value;
285 if ( mb_strlen( $value ) > 1024 ) {
286 $value = "string(" . mb_strlen( $value ) . ")";
292 if ( $this->params
) {
293 foreach ( $this->params
as $key => $value ) {
294 if ( $paramString != '' ) {
297 if ( is_array( $value ) ) {
298 $filteredValue = array();
299 foreach ( $value as $k => $v ) {
300 if ( is_scalar( $v ) ) {
301 $filteredValue[$k] = $truncFunc( $v );
303 $filteredValue = null;
307 if ( $filteredValue && count( $filteredValue ) < 10 ) {
308 $value = FormatJson
::encode( $filteredValue );
310 $value = "array(" . count( $value ) . ")";
312 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
313 $value = "object(" . get_class( $value ) . ")";
316 $paramString .= "$key={$truncFunc( $value )}";
321 foreach ( $this->metadata
as $key => $value ) {
322 if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) {
323 $metaString .= ( $metaString ?
",$key=$value" : "$key=$value" );
328 if ( is_object( $this->title
) ) {
329 $s .= " {$this->title->getPrefixedDBkey()}";
331 if ( $paramString != '' ) {
332 $s .= " $paramString";
334 if ( $metaString != '' ) {
335 $s .= " ($metaString)";
341 protected function setLastError( $error ) {
342 $this->error
= $error;
345 public function getLastError() {