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