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