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