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