Standardised file description headers:
[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( 'type', 'Type of job to run', false, true );
35 $this->addOption( 'procs', 'Number of processes to use', false, true );
36 }
37
38 public function memoryLimit() {
39 // Don't eat all memory on the machine if we get a bad job.
40 return "150M";
41 }
42
43 public function execute() {
44 global $wgTitle;
45 if ( $this->hasOption( 'procs' ) ) {
46 $procs = intval( $this->getOption( 'procs' ) );
47 if ( $procs < 1 || $procs > 1000 ) {
48 $this->error( "Invalid argument to --procs", true );
49 }
50 $fc = new ForkController( $procs );
51 if ( $fc->start( $procs ) != 'child' ) {
52 exit( 0 );
53 }
54 }
55 $maxJobs = $this->getOption( 'maxjobs', 10000 );
56 $type = $this->getOption( 'type', false );
57 $wgTitle = Title::newFromText( 'RunJobs.php' );
58 $dbw = wfGetDB( DB_MASTER );
59 $n = 0;
60 $conds = '';
61 if ( $type !== false )
62 $conds = "job_cmd = " . $dbw->addQuotes( $type );
63
64 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
65 $offset = 0;
66 for ( ; ; ) {
67 $job = !$type ? Job::pop( $offset ) : Job::pop_type( $type );
68
69 if ( !$job )
70 break;
71
72 wfWaitForSlaves( 5 );
73 $t = microtime( true );
74 $offset = $job->id;
75 $status = $job->run();
76 $t = microtime( true ) - $t;
77 $timeMs = intval( $t * 1000 );
78 if ( !$status ) {
79 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
80 } else {
81 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
82 }
83 if ( $maxJobs && ++$n > $maxJobs ) {
84 break 2;
85 }
86 }
87 }
88 }
89
90 /**
91 * Log the job message
92 * @param $msg String The message to log
93 */
94 private function runJobsLog( $msg ) {
95 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
96 wfDebugLog( 'runJobs', $msg );
97 }
98 }
99
100 $maintClass = "RunJobs";
101 require_once( DO_MAINTENANCE );