Completed
Branch master (86dc85)
by
unknown
23:45
created

Job::isRootJob()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 0
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
	/**
54
	 * Run the job
55
	 * @return bool Success
56
	 */
57
	abstract public function run();
58
59
	/**
60
	 * Create the appropriate object to handle a specific job
61
	 *
62
	 * @param string $command Job command
63
	 * @param Title $title Associated title
64
	 * @param array $params Job parameters
65
	 * @throws MWException
66
	 * @return Job
67
	 */
68
	public static function factory( $command, Title $title, $params = [] ) {
69
		global $wgJobClasses;
70
71
		if ( isset( $wgJobClasses[$command] ) ) {
72
			$class = $wgJobClasses[$command];
73
74
			$job = new $class( $title, $params );
75
			$job->command = $command;
76
77
			return $job;
78
		}
79
80
		throw new InvalidArgumentException( "Invalid job command '{$command}'" );
81
	}
82
83
	/**
84
	 * @param string $command
85
	 * @param Title $title
86
	 * @param array|bool $params Can not be === true
87
	 */
88
	public function __construct( $command, $title, $params = false ) {
89
		$this->command = $command;
90
		$this->title = $title;
91
		$this->params = is_array( $params ) ? $params : []; // sanity
92
93
		// expensive jobs may set this to true
94
		$this->removeDuplicates = false;
95
96
		if ( !isset( $this->params['requestId'] ) ) {
97
			$this->params['requestId'] = WebRequest::getRequestId();
98
		}
99
	}
100
101
	/**
102
	 * Batch-insert a group of jobs into the queue.
103
	 * This will be wrapped in a transaction with a forced commit.
104
	 *
105
	 * This may add duplicate at insert time, but they will be
106
	 * removed later on, when the first one is popped.
107
	 *
108
	 * @param Job[] $jobs Array of Job objects
109
	 * @return bool
110
	 * @deprecated since 1.21
111
	 */
112
	public static function batchInsert( $jobs ) {
113
		wfDeprecated( __METHOD__, '1.21' );
114
		JobQueueGroup::singleton()->push( $jobs );
115
		return true;
116
	}
117
118
	/**
119
	 * @return string
120
	 */
121
	public function getType() {
122
		return $this->command;
123
	}
124
125
	/**
126
	 * @return Title
127
	 */
128
	public function getTitle() {
129
		return $this->title;
130
	}
131
132
	/**
133
	 * @return array
134
	 */
135
	public function getParams() {
136
		return $this->params;
137
	}
138
139
	/**
140
	 * @return int|null UNIX timestamp to delay running this job until, otherwise null
141
	 * @since 1.22
142
	 */
143
	public function getReleaseTimestamp() {
144
		return isset( $this->params['jobReleaseTimestamp'] )
145
			? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
146
			: null;
147
	}
148
149
	/**
150
	 * @return int|null UNIX timestamp of when the job was queued, or null
151
	 * @since 1.26
152
	 */
153
	public function getQueuedTimestamp() {
154
		return isset( $this->metadata['timestamp'] )
155
			? wfTimestampOrNull( TS_UNIX, $this->metadata['timestamp'] )
156
			: null;
157
	}
158
159
	/**
160
	 * @return string|null Id of the request that created this job. Follows
161
	 *  jobs recursively, allowing to track the id of the request that started a
162
	 *  job when jobs insert jobs which insert other jobs.
163
	 * @since 1.27
164
	 */
165
	public function getRequestId() {
166
		return isset( $this->params['requestId'] )
167
			? $this->params['requestId']
168
			: null;
169
	}
170
171
	/**
172
	 * @return int|null UNIX timestamp of when the job was runnable, or null
173
	 * @since 1.26
174
	 */
175
	public function getReadyTimestamp() {
176
		return $this->getReleaseTimestamp() ?: $this->getQueuedTimestamp();
177
	}
178
179
	/**
180
	 * Whether the queue should reject insertion of this job if a duplicate exists
181
	 *
182
	 * This can be used to avoid duplicated effort or combined with delayed jobs to
183
	 * coalesce updates into larger batches. Claimed jobs are never treated as
184
	 * duplicates of new jobs, and some queues may allow a few duplicates due to
185
	 * network partitions and fail-over. Thus, additional locking is needed to
186
	 * enforce mutual exclusion if this is really needed.
187
	 *
188
	 * @return bool
189
	 */
190
	public function ignoreDuplicates() {
191
		return $this->removeDuplicates;
192
	}
193
194
	/**
195
	 * @return bool Whether this job can be retried on failure by job runners
196
	 * @since 1.21
197
	 */
198
	public function allowRetries() {
199
		return true;
200
	}
201
202
	/**
203
	 * @return int Number of actually "work items" handled in this job
204
	 * @see $wgJobBackoffThrottling
205
	 * @since 1.23
206
	 */
207
	public function workItemCount() {
208
		return 1;
209
	}
210
211
	/**
212
	 * Subclasses may need to override this to make duplication detection work.
213
	 * The resulting map conveys everything that makes the job unique. This is
214
	 * only checked if ignoreDuplicates() returns true, meaning that duplicate
215
	 * jobs are supposed to be ignored.
216
	 *
217
	 * @return array Map of key/values
218
	 * @since 1.21
219
	 */
220
	public function getDeduplicationInfo() {
221
		$info = [
222
			'type' => $this->getType(),
223
			'namespace' => $this->getTitle()->getNamespace(),
224
			'title' => $this->getTitle()->getDBkey(),
225
			'params' => $this->getParams()
226
		];
227
		if ( is_array( $info['params'] ) ) {
228
			// Identical jobs with different "root" jobs should count as duplicates
229
			unset( $info['params']['rootJobSignature'] );
230
			unset( $info['params']['rootJobTimestamp'] );
231
			// Likewise for jobs with different delay times
232
			unset( $info['params']['jobReleaseTimestamp'] );
233
			// Identical jobs from different requests should count as duplicates
234
			unset( $info['params']['requestId'] );
235
			// Queues pack and hash this array, so normalize the order
236
			ksort( $info['params'] );
237
		}
238
239
		return $info;
240
	}
241
242
	/**
243
	 * Get "root job" parameters for a task
244
	 *
245
	 * This is used to no-op redundant jobs, including child jobs of jobs,
246
	 * as long as the children inherit the root job parameters. When a job
247
	 * with root job parameters and "rootJobIsSelf" set is pushed, the
248
	 * deduplicateRootJob() method is automatically called on it. If the
249
	 * root job is only virtual and not actually pushed (e.g. the sub-jobs
250
	 * are inserted directly), then call deduplicateRootJob() directly.
251
	 *
252
	 * @see JobQueue::deduplicateRootJob()
253
	 *
254
	 * @param string $key A key that identifies the task
255
	 * @return array Map of:
256
	 *   - rootJobIsSelf    : true
257
	 *   - rootJobSignature : hash (e.g. SHA1) that identifies the task
258
	 *   - rootJobTimestamp : TS_MW timestamp of this instance of the task
259
	 * @since 1.21
260
	 */
261
	public static function newRootJobParams( $key ) {
262
		return [
263
			'rootJobIsSelf'    => true,
264
			'rootJobSignature' => sha1( $key ),
265
			'rootJobTimestamp' => wfTimestampNow()
266
		];
267
	}
268
269
	/**
270
	 * @see JobQueue::deduplicateRootJob()
271
	 * @return array
272
	 * @since 1.21
273
	 */
274 View Code Duplication
	public function getRootJobParams() {
275
		return [
276
			'rootJobSignature' => isset( $this->params['rootJobSignature'] )
277
				? $this->params['rootJobSignature']
278
				: null,
279
			'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
280
				? $this->params['rootJobTimestamp']
281
				: null
282
		];
283
	}
284
285
	/**
286
	 * @see JobQueue::deduplicateRootJob()
287
	 * @return bool
288
	 * @since 1.22
289
	 */
290
	public function hasRootJobParams() {
291
		return isset( $this->params['rootJobSignature'] )
292
			&& isset( $this->params['rootJobTimestamp'] );
293
	}
294
295
	/**
296
	 * @see JobQueue::deduplicateRootJob()
297
	 * @return bool Whether this is job is a root job
298
	 */
299
	public function isRootJob() {
300
		return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
301
	}
302
303
	/**
304
	 * @param callable $callback
305
	 * @since 1.27
306
	 */
307
	protected function addTeardownCallback( $callback ) {
308
		$this->teardownCallbacks[] = $callback;
309
	}
310
311
	/**
312
	 * Do any final cleanup after run(), deferred updates, and all DB commits happen
313
	 *
314
	 * @since 1.27
315
	 */
316
	public function teardown() {
317
		foreach ( $this->teardownCallbacks as $callback ) {
318
			call_user_func( $callback );
319
		}
320
	}
321
322
	/**
323
	 * Insert a single job into the queue.
324
	 * @return bool True on success
325
	 * @deprecated since 1.21
326
	 */
327
	public function insert() {
328
		JobQueueGroup::singleton()->push( $this );
329
		return true;
330
	}
331
332
	/**
333
	 * @return string
334
	 */
335
	public function toString() {
336
		$paramString = '';
337
		if ( $this->params ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
338
			foreach ( $this->params as $key => $value ) {
339
				if ( $paramString != '' ) {
340
					$paramString .= ' ';
341
				}
342
				if ( is_array( $value ) ) {
343
					$filteredValue = [];
344
					foreach ( $value as $k => $v ) {
345
						$json = FormatJson::encode( $v );
346
						if ( $json === false || mb_strlen( $json ) > 512 ) {
347
							$filteredValue[$k] = gettype( $v ) . '(...)';
348
						} else {
349
							$filteredValue[$k] = $v;
350
						}
351
					}
352
					if ( count( $filteredValue ) <= 10 ) {
353
						$value = FormatJson::encode( $filteredValue );
354
					} else {
355
						$value = "array(" . count( $value ) . ")";
356
					}
357
				} elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
358
					$value = "object(" . get_class( $value ) . ")";
359
				}
360
361
				$flatValue = (string)$value;
362
				if ( mb_strlen( $value ) > 1024 ) {
363
					$flatValue = "string(" . mb_strlen( $value ) . ")";
364
				}
365
366
				$paramString .= "$key={$flatValue}";
367
			}
368
		}
369
370
		$metaString = '';
371
		foreach ( $this->metadata as $key => $value ) {
372
			if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) {
373
				$metaString .= ( $metaString ? ",$key=$value" : "$key=$value" );
374
			}
375
		}
376
377
		$s = $this->command;
378
		if ( is_object( $this->title ) ) {
379
			$s .= " {$this->title->getPrefixedDBkey()}";
380
		}
381
		if ( $paramString != '' ) {
382
			$s .= " $paramString";
383
		}
384
		if ( $metaString != '' ) {
385
			$s .= " ($metaString)";
386
		}
387
388
		return $s;
389
	}
390
391
	protected function setLastError( $error ) {
392
		$this->error = $error;
393
	}
394
395
	public function getLastError() {
396
		return $this->error;
397
	}
398
}
399