Merge maintenance-work branch (now with less errors!):
[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( "Maintenance.php" );
28
29 class RunJobs extends Maintenance {
30 public function __construct() {
31 global $wgUseNormalUser;
32 parent::__construct();
33 $this->mDescription = "Run pending jobs";
34 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', 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 $wgUseNormalUser = true;
38 }
39
40 public function execute() {
41 global $wgTitle;
42 if ( $this->hasOption( 'procs' ) ) {
43 $procs = intval( $this->getOption('procs') );
44 if ( $procs < 1 || $procs > 1000 ) {
45 $this->error( "Invalid argument to --procs\n", true );
46 }
47 $fc = new ForkController( $procs );
48 if ( $fc->start( $procs ) != 'child' ) {
49 exit( 0 );
50 }
51 }
52 $maxJobs = $this->getOption( 'maxjobs', 10000 );
53 $type = $this->getOption( 'type', false );
54 $wgTitle = Title::newFromText( 'RunJobs.php' );
55 $dbw = wfGetDB( DB_MASTER );
56 $n = 0;
57 $conds = '';
58 if ($type !== false)
59 $conds = "job_cmd = " . $dbw->addQuotes($type);
60
61 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
62 $offset=0;
63 for (;;) {
64 $job = ($type == false) ?
65 Job::pop($offset)
66 : Job::pop_type($type);
67
68 if ($job == false)
69 break;
70
71 wfWaitForSlaves( 5 );
72 $t = microtime( true );
73 $offset=$job->id;
74 $status = $job->run();
75 $t = microtime( true ) - $t;
76 $timeMs = intval( $t * 1000 );
77 if ( !$status ) {
78 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
79 } else {
80 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
81 }
82 if ( $maxJobs && ++$n > $maxJobs ) {
83 break 2;
84 }
85 }
86 }
87 }
88
89 /**
90 * Log the job message
91 * @param $msg String The message to log
92 */
93 private function runJobsLog( $msg ) {
94 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
95 wfDebugLog( 'runJobs', $msg );
96 }
97 }
98
99 $maintClass = "RunJobs";
100 require_once( DO_MAINTENANCE );