Don't check namespace in SpecialWantedtemplates
[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 /**
29 * Maintenance script that runs pending jobs.
30 *
31 * @ingroup Maintenance
32 */
33 class RunJobs extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Run pending jobs";
37 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
38 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
39 $this->addOption( 'type', 'Type of job to run', false, true );
40 $this->addOption( 'procs', 'Number of processes to use', false, true );
41 $this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
42 $this->addOption( 'result', 'Set to JSON to print only a JSON response', false, true );
43 }
44
45 public function memoryLimit() {
46 if ( $this->hasOption( 'memory-limit' ) ) {
47 return parent::memoryLimit();
48 }
49
50 // Don't eat all memory on the machine if we get a bad job.
51 return "150M";
52 }
53
54 public function execute() {
55 global $wgCommandLineMode;
56
57 if ( $this->hasOption( 'procs' ) ) {
58 $procs = intval( $this->getOption( 'procs' ) );
59 if ( $procs < 1 || $procs > 1000 ) {
60 $this->error( "Invalid argument to --procs", true );
61 } elseif ( $procs != 1 ) {
62 $fc = new ForkController( $procs );
63 if ( $fc->start() != 'child' ) {
64 exit( 0 );
65 }
66 }
67 }
68
69 $outputJSON = ( $this->getOption( 'result' ) === 'json' );
70
71 // Enable DBO_TRX for atomicity; JobRunner manages transactions
72 // and works well in web server mode already (@TODO: this is a hack)
73 $wgCommandLineMode = false;
74
75 $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
76 if ( !$outputJSON ) {
77 $runner->setDebugHandler( array( $this, 'debugInternal' ) );
78 }
79
80 $response = $runner->run( array(
81 'type' => $this->getOption( 'type', false ),
82 'maxJobs' => $this->getOption( 'maxjobs', false ),
83 'maxTime' => $this->getOption( 'maxtime', false ),
84 'throttle' => $this->hasOption( 'nothrottle' ) ? false : true,
85 ) );
86
87 if ( $outputJSON ) {
88 $this->output( FormatJson::encode( $response, true ) );
89 }
90
91 $wgCommandLineMode = true;
92 }
93
94 /**
95 * @param string $s
96 */
97 public function debugInternal( $s ) {
98 $this->output( $s );
99 }
100 }
101
102 $maintClass = "RunJobs";
103 require_once RUN_MAINTENANCE_IF_MAIN;