Merge "Use our fork of less.php" into REL1_31
[lhc/web/wiklou.git] / maintenance / runJobs.php
1 <?php
2 /**
3 * Run pending jobs.
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 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 use MediaWiki\Logger\LoggerFactory;
27
28 // So extensions (and other code) can check whether they're running in job mode
29 define( 'MEDIAWIKI_JOB_RUNNER', true );
30
31 /**
32 * Maintenance script that runs pending jobs.
33 *
34 * @ingroup Maintenance
35 */
36 class RunJobs extends Maintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Run pending jobs' );
40 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
41 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
42 $this->addOption( 'type', 'Type of job to run', false, true );
43 $this->addOption( 'procs', 'Number of processes to use', false, true );
44 $this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
45 $this->addOption( 'result', 'Set to "json" to print only a JSON response', false, true );
46 $this->addOption( 'wait', 'Wait for new jobs instead of exiting', false, false );
47 }
48
49 public function memoryLimit() {
50 if ( $this->hasOption( 'memory-limit' ) ) {
51 return parent::memoryLimit();
52 }
53
54 // Don't eat all memory on the machine if we get a bad job.
55 return "150M";
56 }
57
58 public function execute() {
59 if ( $this->hasOption( 'procs' ) ) {
60 $procs = intval( $this->getOption( 'procs' ) );
61 if ( $procs < 1 || $procs > 1000 ) {
62 $this->fatalError( "Invalid argument to --procs" );
63 } elseif ( $procs != 1 ) {
64 $fc = new ForkController( $procs );
65 if ( $fc->start() != 'child' ) {
66 exit( 0 );
67 }
68 }
69 }
70
71 $outputJSON = ( $this->getOption( 'result' ) === 'json' );
72 $wait = $this->hasOption( 'wait' );
73
74 $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
75 if ( !$outputJSON ) {
76 $runner->setDebugHandler( [ $this, 'debugInternal' ] );
77 }
78
79 $type = $this->getOption( 'type', false );
80 $maxJobs = $this->getOption( 'maxjobs', false );
81 $maxTime = $this->getOption( 'maxtime', false );
82 $throttle = !$this->hasOption( 'nothrottle' );
83
84 while ( true ) {
85 $response = $runner->run( [
86 'type' => $type,
87 'maxJobs' => $maxJobs,
88 'maxTime' => $maxTime,
89 'throttle' => $throttle,
90 ] );
91
92 if ( $outputJSON ) {
93 $this->output( FormatJson::encode( $response, true ) );
94 }
95
96 if (
97 !$wait ||
98 $response['reached'] === 'time-limit' ||
99 $response['reached'] === 'job-limit' ||
100 $response['reached'] === 'memory-limit'
101 ) {
102 break;
103 }
104
105 if ( $maxJobs !== false ) {
106 $maxJobs -= count( $response['jobs'] );
107 }
108
109 sleep( 1 );
110 }
111 }
112
113 /**
114 * @param string $s
115 */
116 public function debugInternal( $s ) {
117 $this->output( $s );
118 }
119 }
120
121 $maintClass = RunJobs::class;
122 require_once RUN_MAINTENANCE_IF_MAIN;