Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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 * To push jobs onto queues, use JobQueueGroup::singleton()->push();
27 *
28 * @ingroup JobQueue
29 */
30 abstract class Job implements IJobSpecification {
31 /** @var string */
32 public $command;
33
34 /** @var array Array of job parameters */
35 public $params;
36
37 /** @var array Additional queue metadata */
38 public $metadata = [];
39
40 /** @var Title */
41 protected $title;
42
43 /** @var bool Expensive jobs may set this to true */
44 protected $removeDuplicates = false;
45
46 /** @var string Text for error that occurred last */
47 protected $error;
48
49 /** @var callable[] */
50 protected $teardownCallbacks = [];
51
52 /** @var int Bitfield of JOB_* class constants */
53 protected $executionFlags = 0;
54
55 /** @var int Job must not be wrapped in the usual explicit LBFactory transaction round */
56 const JOB_NO_EXPLICIT_TRX_ROUND = 1;
57
58 /**
59 * Run the job
60 * @return bool Success
61 */
62 abstract public function run();
63
64 /**
65 * Create the appropriate object to handle a specific job
66 *
67 * @param string $command Job command
68 * @param array|Title $params Job parameters
69 * @throws InvalidArgumentException
70 * @return Job
71 */
72 public static function factory( $command, $params = [] ) {
73 global $wgJobClasses;
74
75 if ( $params instanceof Title ) {
76 // Backwards compatibility for old signature ($command, $title, $params)
77 $title = $params;
78 $params = func_num_args() >= 3 ? func_get_arg( 2 ) : [];
79 } else {
80 // Subclasses can override getTitle() to return something more meaningful
81 $title = Title::makeTitle( NS_SPECIAL, 'Blankpage' );
82 }
83
84 if ( isset( $wgJobClasses[$command] ) ) {
85 $handler = $wgJobClasses[$command];
86
87 if ( is_callable( $handler ) ) {
88 $job = call_user_func( $handler, $title, $params );
89 } elseif ( class_exists( $handler ) ) {
90 $job = new $handler( $title, $params );
91 } else {
92 $job = null;
93 }
94
95 if ( $job instanceof Job ) {
96 $job->command = $command;
97
98 return $job;
99 } else {
100 throw new InvalidArgumentException( "Could instantiate job '$command': bad spec!" );
101 }
102 }
103
104 throw new InvalidArgumentException( "Invalid job command '{$command}'" );
105 }
106
107 /**
108 * @param string $command
109 * @param array|Title|null $params
110 */
111 public function __construct( $command, $params = null ) {
112 if ( $params instanceof Title ) {
113 // Backwards compatibility for old signature ($command, $title, $params)
114 $title = $params;
115 $params = func_get_arg( 2 );
116 } else {
117 // Subclasses can override getTitle() to return something more meaningful
118 $title = Title::makeTitle( NS_SPECIAL, 'Blankpage' );
119 }
120
121 $this->command = $command;
122 $this->title = $title;
123 $this->params = is_array( $params ) ? $params : [];
124 if ( !isset( $this->params['requestId'] ) ) {
125 $this->params['requestId'] = WebRequest::getRequestId();
126 }
127 }
128
129 /**
130 * @param int $flag JOB_* class constant
131 * @return bool
132 * @since 1.31
133 */
134 public function hasExecutionFlag( $flag ) {
135 return ( $this->executionFlags & $flag ) === $flag;
136 }
137
138 /**
139 * @return string
140 */
141 public function getType() {
142 return $this->command;
143 }
144
145 /**
146 * @return Title
147 */
148 public function getTitle() {
149 return $this->title;
150 }
151
152 /**
153 * @return array
154 */
155 public function getParams() {
156 return $this->params;
157 }
158
159 /**
160 * @param string|null $field Metadata field or null to get all the metadata
161 * @return mixed|null Value; null if missing
162 * @since 1.33
163 */
164 public function getMetadata( $field = null ) {
165 if ( $field === null ) {
166 return $this->metadata;
167 }
168
169 return $this->metadata[$field] ?? null;
170 }
171
172 /**
173 * @param string $field Key name to set the value for
174 * @param mixed $value The value to set the field for
175 * @return mixed|null The prior field value; null if missing
176 * @since 1.33
177 */
178 public function setMetadata( $field, $value ) {
179 $old = $this->getMetadata( $field );
180 if ( $value === null ) {
181 unset( $this->metadata[$field] );
182 } else {
183 $this->metadata[$field] = $value;
184 }
185
186 return $old;
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 int|null UNIX timestamp of when the job was queued, or null
201 * @since 1.26
202 */
203 public function getQueuedTimestamp() {
204 return isset( $this->metadata['timestamp'] )
205 ? wfTimestampOrNull( TS_UNIX, $this->metadata['timestamp'] )
206 : null;
207 }
208
209 /**
210 * @return string|null Id of the request that created this job. Follows
211 * jobs recursively, allowing to track the id of the request that started a
212 * job when jobs insert jobs which insert other jobs.
213 * @since 1.27
214 */
215 public function getRequestId() {
216 return $this->params['requestId'] ?? null;
217 }
218
219 /**
220 * @return int|null UNIX timestamp of when the job was runnable, or null
221 * @since 1.26
222 */
223 public function getReadyTimestamp() {
224 return $this->getReleaseTimestamp() ?: $this->getQueuedTimestamp();
225 }
226
227 /**
228 * Whether the queue should reject insertion of this job if a duplicate exists
229 *
230 * This can be used to avoid duplicated effort or combined with delayed jobs to
231 * coalesce updates into larger batches. Claimed jobs are never treated as
232 * duplicates of new jobs, and some queues may allow a few duplicates due to
233 * network partitions and fail-over. Thus, additional locking is needed to
234 * enforce mutual exclusion if this is really needed.
235 *
236 * @return bool
237 */
238 public function ignoreDuplicates() {
239 return $this->removeDuplicates;
240 }
241
242 /**
243 * @return bool Whether this job can be retried on failure by job runners
244 * @since 1.21
245 */
246 public function allowRetries() {
247 return true;
248 }
249
250 /**
251 * @return int Number of actually "work items" handled in this job
252 * @see $wgJobBackoffThrottling
253 * @since 1.23
254 */
255 public function workItemCount() {
256 return 1;
257 }
258
259 /**
260 * Subclasses may need to override this to make duplication detection work.
261 * The resulting map conveys everything that makes the job unique. This is
262 * only checked if ignoreDuplicates() returns true, meaning that duplicate
263 * jobs are supposed to be ignored.
264 *
265 * @return array Map of key/values
266 * @since 1.21
267 */
268 public function getDeduplicationInfo() {
269 $info = [
270 'type' => $this->getType(),
271 'namespace' => $this->getTitle()->getNamespace(),
272 'title' => $this->getTitle()->getDBkey(),
273 'params' => $this->getParams()
274 ];
275 if ( is_array( $info['params'] ) ) {
276 // Identical jobs with different "root" jobs should count as duplicates
277 unset( $info['params']['rootJobSignature'] );
278 unset( $info['params']['rootJobTimestamp'] );
279 // Likewise for jobs with different delay times
280 unset( $info['params']['jobReleaseTimestamp'] );
281 // Identical jobs from different requests should count as duplicates
282 unset( $info['params']['requestId'] );
283 // Queues pack and hash this array, so normalize the order
284 ksort( $info['params'] );
285 }
286
287 return $info;
288 }
289
290 /**
291 * Get "root job" parameters for a task
292 *
293 * This is used to no-op redundant jobs, including child jobs of jobs,
294 * as long as the children inherit the root job parameters. When a job
295 * with root job parameters and "rootJobIsSelf" set is pushed, the
296 * deduplicateRootJob() method is automatically called on it. If the
297 * root job is only virtual and not actually pushed (e.g. the sub-jobs
298 * are inserted directly), then call deduplicateRootJob() directly.
299 *
300 * @see JobQueue::deduplicateRootJob()
301 *
302 * @param string $key A key that identifies the task
303 * @return array Map of:
304 * - rootJobIsSelf : true
305 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
306 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
307 * @since 1.21
308 */
309 public static function newRootJobParams( $key ) {
310 return [
311 'rootJobIsSelf' => true,
312 'rootJobSignature' => sha1( $key ),
313 'rootJobTimestamp' => wfTimestampNow()
314 ];
315 }
316
317 /**
318 * @see JobQueue::deduplicateRootJob()
319 * @return array
320 * @since 1.21
321 */
322 public function getRootJobParams() {
323 return [
324 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
325 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
326 ];
327 }
328
329 /**
330 * @see JobQueue::deduplicateRootJob()
331 * @return bool
332 * @since 1.22
333 */
334 public function hasRootJobParams() {
335 return isset( $this->params['rootJobSignature'] )
336 && isset( $this->params['rootJobTimestamp'] );
337 }
338
339 /**
340 * @see JobQueue::deduplicateRootJob()
341 * @return bool Whether this is job is a root job
342 */
343 public function isRootJob() {
344 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
345 }
346
347 /**
348 * @param callable $callback A function with one parameter, the success status, which will be
349 * false if the job failed or it succeeded but the DB changes could not be committed or
350 * any deferred updates threw an exception. (This parameter was added in 1.28.)
351 * @since 1.27
352 */
353 protected function addTeardownCallback( $callback ) {
354 $this->teardownCallbacks[] = $callback;
355 }
356
357 /**
358 * Do any final cleanup after run(), deferred updates, and all DB commits happen
359 * @param bool $status Whether the job, its deferred updates, and DB commit all succeeded
360 * @since 1.27
361 */
362 public function teardown( $status ) {
363 foreach ( $this->teardownCallbacks as $callback ) {
364 call_user_func( $callback, $status );
365 }
366 }
367
368 /**
369 * @return string
370 */
371 public function toString() {
372 $paramString = '';
373 if ( $this->params ) {
374 foreach ( $this->params as $key => $value ) {
375 if ( $paramString != '' ) {
376 $paramString .= ' ';
377 }
378 if ( is_array( $value ) ) {
379 $filteredValue = [];
380 foreach ( $value as $k => $v ) {
381 $json = FormatJson::encode( $v );
382 if ( $json === false || mb_strlen( $json ) > 512 ) {
383 $filteredValue[$k] = gettype( $v ) . '(...)';
384 } else {
385 $filteredValue[$k] = $v;
386 }
387 }
388 if ( count( $filteredValue ) <= 10 ) {
389 $value = FormatJson::encode( $filteredValue );
390 } else {
391 $value = "array(" . count( $value ) . ")";
392 }
393 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
394 $value = "object(" . get_class( $value ) . ")";
395 }
396
397 $flatValue = (string)$value;
398 if ( mb_strlen( $value ) > 1024 ) {
399 $flatValue = "string(" . mb_strlen( $value ) . ")";
400 }
401
402 $paramString .= "$key={$flatValue}";
403 }
404 }
405
406 $metaString = '';
407 foreach ( $this->metadata as $key => $value ) {
408 if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) {
409 $metaString .= ( $metaString ? ",$key=$value" : "$key=$value" );
410 }
411 }
412
413 $s = $this->command;
414 if ( is_object( $this->title ) ) {
415 $s .= " {$this->title->getPrefixedDBkey()}";
416 }
417 if ( $paramString != '' ) {
418 $s .= " $paramString";
419 }
420 if ( $metaString != '' ) {
421 $s .= " ($metaString)";
422 }
423
424 return $s;
425 }
426
427 protected function setLastError( $error ) {
428 $this->error = $error;
429 }
430
431 public function getLastError() {
432 return $this->error;
433 }
434 }