Merge "CologneBlue rewrite: rewrite bottomLinks()"
[lhc/web/wiklou.git] / maintenance / runJobs.php
1 <?php
2 /**
3 * Run pending jobs.
4 *
5 * Options:
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 * @file
25 * @ingroup Maintenance
26 */
27
28 require_once( __DIR__ . '/Maintenance.php' );
29
30 /**
31 * Maintenance script that runs pending jobs.
32 *
33 * @ingroup Maintenance
34 */
35 class RunJobs extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->mDescription = "Run pending jobs";
39 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
40 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
41 $this->addOption( 'type', 'Type of job to run', false, true );
42 $this->addOption( 'procs', 'Number of processes to use', false, true );
43 }
44
45 public function memoryLimit() {
46 if ( $this->hasOption( 'memory-limit' ) ) {
47 return parent::memoryLimit();
48 }
49 // Don't eat all memory on the machine if we get a bad job.
50 return "150M";
51 }
52
53 public function execute() {
54 global $wgTitle;
55
56 if ( $this->hasOption( 'procs' ) ) {
57 $procs = intval( $this->getOption( 'procs' ) );
58 if ( $procs < 1 || $procs > 1000 ) {
59 $this->error( "Invalid argument to --procs", true );
60 }
61 $fc = new ForkController( $procs );
62 if ( $fc->start() != 'child' ) {
63 exit( 0 );
64 }
65 }
66 $maxJobs = $this->getOption( 'maxjobs', false );
67 $maxTime = $this->getOption( 'maxtime', false );
68 $startTime = time();
69 $type = $this->getOption( 'type', false );
70 $wgTitle = Title::newFromText( 'RunJobs.php' );
71 $dbw = wfGetDB( DB_MASTER );
72 $n = 0;
73
74 $group = JobQueueGroup::singleton();
75 do {
76 $job = ( $type === false )
77 ? $group->pop() // job from any queue
78 : $group->get( $type )->pop(); // job from a single queue
79 if ( $job ) { // found a job
80 // Perform the job (logging success/failure and runtime)...
81 $t = microtime( true );
82 $this->runJobsLog( $job->toString() . " STARTING" );
83 $status = $job->run();
84 $group->ack( $job ); // done
85 $t = microtime( true ) - $t;
86 $timeMs = intval( $t * 1000 );
87 if ( !$status ) {
88 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
89 } else {
90 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
91 }
92 // Break out if we hit the job count or wall time limits...
93 if ( $maxJobs && ++$n >= $maxJobs ) {
94 break;
95 }
96 if ( $maxTime && ( time() - $startTime ) > $maxTime ) {
97 break;
98 }
99 // Don't let any slaves/backups fall behind...
100 $group->get( $type )->waitForBackups();
101 }
102 } while ( $job ); // stop when there are no jobs
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 );