Merge "Introducing pp_sortkey."
[lhc/web/wiklou.git] / includes / jobqueue / Job.php
1 <?php
2 /**
3 * Job queue task base code.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @defgroup JobQueue JobQueue
22 */
23
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).
28 *
29 * @ingroup JobQueue
30 */
31 abstract class Job implements IJobSpecification {
32 /** @var string */
33 public $command;
34
35 /** @var array|bool Array of job parameters or false if none */
36 public $params;
37
38 /** @var array Additional queue metadata */
39 public $metadata = array();
40
41 /** @var Title */
42 protected $title;
43
44 /** @var bool Expensive jobs may set this to true */
45 protected $removeDuplicates;
46
47 /** @var string Text for error that occurred last */
48 protected $error;
49
50 /*-------------------------------------------------------------------------
51 * Abstract functions
52 *------------------------------------------------------------------------*/
53
54 /**
55 * Run the job
56 * @return bool Success
57 */
58 abstract public function run();
59
60 /*-------------------------------------------------------------------------
61 * Static functions
62 *------------------------------------------------------------------------*/
63
64 /**
65 * Create the appropriate object to handle a specific job
66 *
67 * @param string $command Job command
68 * @param Title $title Associated title
69 * @param array|bool $params Job parameters
70 * @throws MWException
71 * @return Job
72 */
73 public static function factory( $command, Title $title, $params = false ) {
74 global $wgJobClasses;
75 if ( isset( $wgJobClasses[$command] ) ) {
76 $class = $wgJobClasses[$command];
77
78 return new $class( $title, $params );
79 }
80 throw new MWException( "Invalid job command `{$command}`" );
81 }
82
83 /**
84 * Batch-insert a group of jobs into the queue.
85 * This will be wrapped in a transaction with a forced commit.
86 *
87 * This may add duplicate at insert time, but they will be
88 * removed later on, when the first one is popped.
89 *
90 * @param array $jobs Array of Job objects
91 * @return bool
92 * @deprecated since 1.21
93 */
94 public static function batchInsert( $jobs ) {
95 return JobQueueGroup::singleton()->push( $jobs );
96 }
97
98 /**
99 * Insert a group of jobs into the queue.
100 *
101 * Same as batchInsert() but does not commit and can thus
102 * be rolled-back as part of a larger transaction. However,
103 * large batches of jobs can cause slave lag.
104 *
105 * @param array $jobs Array of Job objects
106 * @return bool
107 * @deprecated since 1.21
108 */
109 public static function safeBatchInsert( $jobs ) {
110 return JobQueueGroup::singleton()->push( $jobs, JobQueue::QOS_ATOMIC );
111 }
112
113 /**
114 * Pop a job of a certain type. This tries less hard than pop() to
115 * actually find a job; it may be adversely affected by concurrent job
116 * runners.
117 *
118 * @param string $type
119 * @return Job|bool Returns false if there are no jobs
120 * @deprecated since 1.21
121 */
122 public static function pop_type( $type ) {
123 return JobQueueGroup::singleton()->get( $type )->pop();
124 }
125
126 /**
127 * Pop a job off the front of the queue.
128 * This is subject to $wgJobTypesExcludedFromDefaultQueue.
129 *
130 * @return Job|bool False if there are no jobs
131 * @deprecated since 1.21
132 */
133 public static function pop() {
134 return JobQueueGroup::singleton()->pop();
135 }
136
137 /*-------------------------------------------------------------------------
138 * Non-static functions
139 *------------------------------------------------------------------------*/
140
141 /**
142 * @param string $command
143 * @param Title $title
144 * @param array|bool $params
145 */
146 public function __construct( $command, $title, $params = false ) {
147 $this->command = $command;
148 $this->title = $title;
149 $this->params = $params;
150
151 // expensive jobs may set this to true
152 $this->removeDuplicates = false;
153 }
154
155 /**
156 * @return string
157 */
158 public function getType() {
159 return $this->command;
160 }
161
162 /**
163 * @return Title
164 */
165 public function getTitle() {
166 return $this->title;
167 }
168
169 /**
170 * @return array
171 */
172 public function getParams() {
173 return $this->params;
174 }
175
176 /**
177 * @return int|null UNIX timestamp to delay running this job until, otherwise null
178 * @since 1.22
179 */
180 public function getReleaseTimestamp() {
181 return isset( $this->params['jobReleaseTimestamp'] )
182 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
183 : null;
184 }
185
186 /**
187 * @return bool Whether only one of each identical set of jobs should be run
188 */
189 public function ignoreDuplicates() {
190 return $this->removeDuplicates;
191 }
192
193 /**
194 * @return bool Whether this job can be retried on failure by job runners
195 * @since 1.21
196 */
197 public function allowRetries() {
198 return true;
199 }
200
201 /**
202 * @return int Number of actually "work items" handled in this job
203 * @see $wgJobBackoffThrottling
204 * @since 1.23
205 */
206 public function workItemCount() {
207 return 1;
208 }
209
210 /**
211 * Subclasses may need to override this to make duplication detection work.
212 * The resulting map conveys everything that makes the job unique. This is
213 * only checked if ignoreDuplicates() returns true, meaning that duplicate
214 * jobs are supposed to be ignored.
215 *
216 * @return array Map of key/values
217 * @since 1.21
218 */
219 public function getDeduplicationInfo() {
220 $info = array(
221 'type' => $this->getType(),
222 'namespace' => $this->getTitle()->getNamespace(),
223 'title' => $this->getTitle()->getDBkey(),
224 'params' => $this->getParams()
225 );
226 if ( is_array( $info['params'] ) ) {
227 // Identical jobs with different "root" jobs should count as duplicates
228 unset( $info['params']['rootJobSignature'] );
229 unset( $info['params']['rootJobTimestamp'] );
230 // Likewise for jobs with different delay times
231 unset( $info['params']['jobReleaseTimestamp'] );
232 }
233
234 return $info;
235 }
236
237 /**
238 * @see JobQueue::deduplicateRootJob()
239 * @param string $key A key that identifies the task
240 * @return array Map of:
241 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
242 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
243 * @since 1.21
244 */
245 public static function newRootJobParams( $key ) {
246 return array(
247 'rootJobSignature' => sha1( $key ),
248 'rootJobTimestamp' => wfTimestampNow()
249 );
250 }
251
252 /**
253 * @see JobQueue::deduplicateRootJob()
254 * @return array
255 * @since 1.21
256 */
257 public function getRootJobParams() {
258 return array(
259 'rootJobSignature' => isset( $this->params['rootJobSignature'] )
260 ? $this->params['rootJobSignature']
261 : null,
262 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
263 ? $this->params['rootJobTimestamp']
264 : null
265 );
266 }
267
268 /**
269 * @see JobQueue::deduplicateRootJob()
270 * @return bool
271 * @since 1.22
272 */
273 public function hasRootJobParams() {
274 return isset( $this->params['rootJobSignature'] )
275 && isset( $this->params['rootJobTimestamp'] );
276 }
277
278 /**
279 * Insert a single job into the queue.
280 * @return bool true on success
281 * @deprecated since 1.21
282 */
283 public function insert() {
284 return JobQueueGroup::singleton()->push( $this );
285 }
286
287 /**
288 * @return string
289 */
290 public function toString() {
291 $paramString = '';
292 if ( $this->params ) {
293 foreach ( $this->params as $key => $value ) {
294 if ( $paramString != '' ) {
295 $paramString .= ' ';
296 }
297 if ( is_array( $value ) ) {
298 $value = "array(" . count( $value ) . ")";
299 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
300 $value = "object(" . get_class( $value ) . ")";
301 }
302 $value = (string)$value;
303 if ( mb_strlen( $value ) > 1024 ) {
304 $value = "string(" . mb_strlen( $value ) . ")";
305 }
306
307 $paramString .= "$key=$value";
308 }
309 }
310
311 if ( is_object( $this->title ) ) {
312 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
313 if ( $paramString !== '' ) {
314 $s .= ' ' . $paramString;
315 }
316
317 return $s;
318 } else {
319 return "{$this->command} $paramString";
320 }
321 }
322
323 protected function setLastError( $error ) {
324 $this->error = $error;
325 }
326
327 public function getLastError() {
328 return $this->error;
329 }
330 }