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