Merge "Fix typo in 1.44 release notes"
[mediawiki.git] / includes / jobqueue / jobs / NullJob.php
blob6cf8212b8d5458ca9b8b1ae0890140c0d5988254
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\MediaWikiServices;
23 /**
24 * No-op job that does nothing.
26 * This is used for testing purposes, e.g. to measure overall system
27 * performance of the JobQueue system, lock contention, etc.
29 * This job can optionally recursively re-queue itself a number of times
30 * or spend a fixed amount of time idling in execution time.
32 * @par Example:
33 * Inserting a null job in the configured job queue:
34 * @code
35 * $ php maintenance/eval.php
36 * > $queue = MediaWikiServices::getInstance()->getJobQueueGroup();
37 * > $job = new NullJob( [ 'lives' => 10 ] );
38 * > $queue->push( $job );
39 * @endcode
41 * You can confirm the job has been enqueued via maintenance/showJobs.php:
43 * @code
44 * $ php maintenance/showJobs.php --group
45 * null: 1 queue; 0 claimed (0 active, 0 abandoned)
46 * @endcode
48 * @ingroup JobQueue
50 class NullJob extends Job implements GenericParameterJob {
51 /**
52 * @param array $params Job parameters (lives, usleep)
54 public function __construct( array $params ) {
55 parent::__construct( 'null', $params );
56 if ( !isset( $this->params['lives'] ) ) {
57 $this->params['lives'] = 1;
59 if ( !isset( $this->params['usleep'] ) ) {
60 $this->params['usleep'] = 0;
62 $this->removeDuplicates = !empty( $this->params['removeDuplicates'] );
65 public function run() {
66 if ( $this->params['usleep'] > 0 ) {
67 usleep( $this->params['usleep'] );
69 if ( $this->params['lives'] > 1 ) {
70 $params = $this->params;
71 $params['lives']--;
72 $job = new self( $params );
73 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
76 return true;