Implement extension registration from an extension.json file
[mediawiki.git] / includes / jobqueue / Job.php
blob56fd1c6d0ba01081820fbc4f5e80f9e7166c3ab1
1 <?php
2 /**
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
20 * @file
21 * @defgroup JobQueue JobQueue
24 /**
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).
29 * @ingroup JobQueue
31 abstract class Job implements IJobSpecification {
32 /** @var string */
33 public $command;
35 /** @var array|bool Array of job parameters or false if none */
36 public $params;
38 /** @var array Additional queue metadata */
39 public $metadata = array();
41 /** @var Title */
42 protected $title;
44 /** @var bool Expensive jobs may set this to true */
45 protected $removeDuplicates;
47 /** @var string Text for error that occurred last */
48 protected $error;
50 /**
51 * Run the job
52 * @return bool Success
54 abstract public function run();
56 /**
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|bool $params Job parameters
62 * @throws MWException
63 * @return Job
65 public static function factory( $command, Title $title, $params = false ) {
66 global $wgJobClasses;
67 if ( isset( $wgJobClasses[$command] ) ) {
68 $class = $wgJobClasses[$command];
70 return new $class( $title, $params );
72 throw new MWException( "Invalid job command `{$command}`" );
75 /**
76 * @param string $command
77 * @param Title $title
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 = $params;
85 // expensive jobs may set this to true
86 $this->removeDuplicates = false;
89 /**
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
97 * @return bool
98 * @deprecated since 1.21
100 public static function batchInsert( $jobs ) {
101 wfDeprecated( __METHOD__, '1.21' );
102 JobQueueGroup::singleton()->push( $jobs );
103 return true;
107 * @return string
109 public function getType() {
110 return $this->command;
114 * @return Title
116 public function getTitle() {
117 return $this->title;
121 * @return array
123 public function getParams() {
124 return $this->params;
128 * @return int|null UNIX timestamp to delay running this job until, otherwise null
129 * @since 1.22
131 public function getReleaseTimestamp() {
132 return isset( $this->params['jobReleaseTimestamp'] )
133 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
134 : null;
138 * @return bool Whether only one of each identical set of jobs should be run
140 public function ignoreDuplicates() {
141 return $this->removeDuplicates;
145 * @return bool Whether this job can be retried on failure by job runners
146 * @since 1.21
148 public function allowRetries() {
149 return true;
153 * @return int Number of actually "work items" handled in this job
154 * @see $wgJobBackoffThrottling
155 * @since 1.23
157 public function workItemCount() {
158 return 1;
162 * Subclasses may need to override this to make duplication detection work.
163 * The resulting map conveys everything that makes the job unique. This is
164 * only checked if ignoreDuplicates() returns true, meaning that duplicate
165 * jobs are supposed to be ignored.
167 * @return array Map of key/values
168 * @since 1.21
170 public function getDeduplicationInfo() {
171 $info = array(
172 'type' => $this->getType(),
173 'namespace' => $this->getTitle()->getNamespace(),
174 'title' => $this->getTitle()->getDBkey(),
175 'params' => $this->getParams()
177 if ( is_array( $info['params'] ) ) {
178 // Identical jobs with different "root" jobs should count as duplicates
179 unset( $info['params']['rootJobSignature'] );
180 unset( $info['params']['rootJobTimestamp'] );
181 // Likewise for jobs with different delay times
182 unset( $info['params']['jobReleaseTimestamp'] );
185 return $info;
189 * @see JobQueue::deduplicateRootJob()
190 * @param string $key A key that identifies the task
191 * @return array Map of:
192 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
193 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
194 * @since 1.21
196 public static function newRootJobParams( $key ) {
197 return array(
198 'rootJobSignature' => sha1( $key ),
199 'rootJobTimestamp' => wfTimestampNow()
204 * @see JobQueue::deduplicateRootJob()
205 * @return array
206 * @since 1.21
208 public function getRootJobParams() {
209 return array(
210 'rootJobSignature' => isset( $this->params['rootJobSignature'] )
211 ? $this->params['rootJobSignature']
212 : null,
213 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
214 ? $this->params['rootJobTimestamp']
215 : null
220 * @see JobQueue::deduplicateRootJob()
221 * @return bool
222 * @since 1.22
224 public function hasRootJobParams() {
225 return isset( $this->params['rootJobSignature'] )
226 && isset( $this->params['rootJobTimestamp'] );
230 * Insert a single job into the queue.
231 * @return bool True on success
232 * @deprecated since 1.21
234 public function insert() {
235 JobQueueGroup::singleton()->push( $this );
236 return true;
240 * @return string
242 public function toString() {
243 $truncFunc = function ( $value ) {
244 $value = (string)$value;
245 if ( mb_strlen( $value ) > 1024 ) {
246 $value = "string(" . mb_strlen( $value ) . ")";
248 return $value;
251 $paramString = '';
252 if ( $this->params ) {
253 foreach ( $this->params as $key => $value ) {
254 if ( $paramString != '' ) {
255 $paramString .= ' ';
257 if ( is_array( $value ) ) {
258 $filteredValue = array();
259 foreach ( $value as $k => $v ) {
260 if ( is_scalar( $v ) ) {
261 $filteredValue[$k] = $truncFunc( $v );
262 } else {
263 $filteredValue = null;
264 break;
267 if ( $filteredValue && count( $filteredValue ) < 10 ) {
268 $value = FormatJson::encode( $filteredValue );
269 } else {
270 $value = "array(" . count( $value ) . ")";
272 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
273 $value = "object(" . get_class( $value ) . ")";
276 $paramString .= "$key={$truncFunc( $value )}";
280 $metaString = '';
281 foreach ( $this->metadata as $key => $value ) {
282 if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) {
283 $metaString .= ( $metaString ? ",$key=$value" : "$key=$value" );
287 $s = $this->command;
288 if ( is_object( $this->title ) ) {
289 $s .= " {$this->title->getPrefixedDBkey()}";
291 if ( $paramString != '' ) {
292 $s .= " $paramString";
294 if ( $metaString != '' ) {
295 $s .= " ($metaString)";
298 return $s;
301 protected function setLastError( $error ) {
302 $this->error = $error;
305 public function getLastError() {
306 return $this->error;