Merge "Remove "or other" option for stubtreshold"
[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 * @return integer Number of actually "work items" handled in this job
202 * @see $wgJobBackoffThrottling
203 * @since 1.23
204 */
205 public function workItemCount() {
206 return 1;
207 }
208
209 /**
210 * Subclasses may need to override this to make duplication detection work.
211 * The resulting map conveys everything that makes the job unique. This is
212 * only checked if ignoreDuplicates() returns true, meaning that duplicate
213 * jobs are supposed to be ignored.
214 *
215 * @return array Map of key/values
216 * @since 1.21
217 */
218 public function getDeduplicationInfo() {
219 $info = array(
220 'type' => $this->getType(),
221 'namespace' => $this->getTitle()->getNamespace(),
222 'title' => $this->getTitle()->getDBkey(),
223 'params' => $this->getParams()
224 );
225 if ( is_array( $info['params'] ) ) {
226 // Identical jobs with different "root" jobs should count as duplicates
227 unset( $info['params']['rootJobSignature'] );
228 unset( $info['params']['rootJobTimestamp'] );
229 // Likewise for jobs with different delay times
230 unset( $info['params']['jobReleaseTimestamp'] );
231 }
232
233 return $info;
234 }
235
236 /**
237 * @see JobQueue::deduplicateRootJob()
238 * @param string $key A key that identifies the task
239 * @return array Map of:
240 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
241 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
242 * @since 1.21
243 */
244 public static function newRootJobParams( $key ) {
245 return array(
246 'rootJobSignature' => sha1( $key ),
247 'rootJobTimestamp' => wfTimestampNow()
248 );
249 }
250
251 /**
252 * @see JobQueue::deduplicateRootJob()
253 * @return array
254 * @since 1.21
255 */
256 public function getRootJobParams() {
257 return array(
258 'rootJobSignature' => isset( $this->params['rootJobSignature'] )
259 ? $this->params['rootJobSignature']
260 : null,
261 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
262 ? $this->params['rootJobTimestamp']
263 : null
264 );
265 }
266
267 /**
268 * @see JobQueue::deduplicateRootJob()
269 * @return bool
270 * @since 1.22
271 */
272 public function hasRootJobParams() {
273 return isset( $this->params['rootJobSignature'] )
274 && isset( $this->params['rootJobTimestamp'] );
275 }
276
277 /**
278 * Insert a single job into the queue.
279 * @return bool true on success
280 * @deprecated since 1.21
281 */
282 public function insert() {
283 return JobQueueGroup::singleton()->push( $this );
284 }
285
286 /**
287 * @return string
288 */
289 public function toString() {
290 $paramString = '';
291 if ( $this->params ) {
292 foreach ( $this->params as $key => $value ) {
293 if ( $paramString != '' ) {
294 $paramString .= ' ';
295 }
296 if ( is_array( $value ) ) {
297 $value = "array(" . count( $value ) . ")";
298 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
299 $value = "object(" . get_class( $value ) . ")";
300 }
301 $value = (string)$value;
302 if ( mb_strlen( $value ) > 1024 ) {
303 $value = "string(" . mb_strlen( $value ) . ")";
304 }
305
306 $paramString .= "$key=$value";
307 }
308 }
309
310 if ( is_object( $this->title ) ) {
311 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
312 if ( $paramString !== '' ) {
313 $s .= ' ' . $paramString;
314 }
315
316 return $s;
317 } else {
318 return "{$this->command} $paramString";
319 }
320 }
321
322 protected function setLastError( $error ) {
323 $this->error = $error;
324 }
325
326 public function getLastError() {
327 return $this->error;
328 }
329 }