Merge "Fix bug causing API to list anons as autoconfirmed in certain cases"
[lhc/web/wiklou.git] / maintenance / runJobs.php
1 <?php
2 /**
3 * This script starts pending jobs.
4 *
5 * Usage:
6 * --maxjobs <num> (default 10000)
7 * --type <job_cmd>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @ingroup Maintenance
25 */
26
27 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
28
29 class RunJobs extends Maintenance {
30 public function __construct() {
31 parent::__construct();
32 $this->mDescription = "Run pending jobs";
33 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
34 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
35 $this->addOption( 'type', 'Type of job to run', false, true );
36 $this->addOption( 'procs', 'Number of processes to use', false, true );
37 }
38
39 public function memoryLimit() {
40 if ( $this->hasOption( 'memory-limit' ) ) {
41 return parent::memoryLimit();
42 }
43 // Don't eat all memory on the machine if we get a bad job.
44 return "150M";
45 }
46
47 public function execute() {
48 global $wgTitle;
49 if ( $this->hasOption( 'procs' ) ) {
50 $procs = intval( $this->getOption( 'procs' ) );
51 if ( $procs < 1 || $procs > 1000 ) {
52 $this->error( "Invalid argument to --procs", true );
53 }
54 $fc = new ForkController( $procs );
55 if ( $fc->start() != 'child' ) {
56 exit( 0 );
57 }
58 }
59 $maxJobs = $this->getOption( 'maxjobs', false );
60 $maxTime = $this->getOption( 'maxtime', false );
61 $startTime = time();
62 $type = $this->getOption( 'type', false );
63 $wgTitle = Title::newFromText( 'RunJobs.php' );
64 $dbw = wfGetDB( DB_MASTER );
65 $n = 0;
66
67 if ( $type === false ) {
68 $conds = Job::defaultQueueConditions( );
69 } else {
70 $conds = array( 'job_cmd' => $type );
71 }
72
73 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
74 $offset = 0;
75 for ( ; ; ) {
76 $job = !$type ? Job::pop( $offset ) : Job::pop_type( $type );
77
78 if ( !$job ) {
79 break;
80 }
81
82 wfWaitForSlaves();
83 $t = microtime( true );
84 $offset = $job->id;
85 $this->runJobsLog( $job->toString() . " STARTING" );
86 $status = $job->run();
87 $t = microtime( true ) - $t;
88 $timeMs = intval( $t * 1000 );
89 if ( !$status ) {
90 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
91 } else {
92 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
93 }
94
95 if ( $maxJobs && ++$n > $maxJobs ) {
96 break 2;
97 }
98 if ( $maxTime && time() - $startTime > $maxTime ) {
99 break 2;
100 }
101 }
102 }
103 }
104
105 /**
106 * Log the job message
107 * @param $msg String The message to log
108 */
109 private function runJobsLog( $msg ) {
110 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
111 wfDebugLog( 'runJobs', $msg );
112 }
113 }
114
115 $maintClass = "RunJobs";
116 require_once( RUN_MAINTENANCE_IF_MAIN );