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