5eb779cb6dfe3709f75a3aa1e3ef39708d2c2613
[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 ( wfReadOnly() ) {
57 $this->error( "Unable to run jobs; the wiki is in read-only mode.", 1 ); // die
58 }
59
60 if ( $this->hasOption( 'procs' ) ) {
61 $procs = intval( $this->getOption( 'procs' ) );
62 if ( $procs < 1 || $procs > 1000 ) {
63 $this->error( "Invalid argument to --procs", true );
64 } elseif ( $procs != 1 ) {
65 $fc = new ForkController( $procs );
66 if ( $fc->start() != 'child' ) {
67 exit( 0 );
68 }
69 }
70 }
71 $maxJobs = $this->getOption( 'maxjobs', false );
72 $maxTime = $this->getOption( 'maxtime', false );
73 $startTime = time();
74 $type = $this->getOption( 'type', false );
75 $wgTitle = Title::newFromText( 'RunJobs.php' );
76 $dbw = wfGetDB( DB_MASTER );
77 $jobsRun = 0; // counter
78
79 $group = JobQueueGroup::singleton();
80 // Handle any required periodic queue maintenance
81 $count = $group->executeReadyPeriodicTasks();
82 if ( $count > 0 ) {
83 $this->runJobsLog( "Executed $count periodic queue task(s)." );
84 }
85
86 $flags = JobQueueGroup::USE_CACHE | JobQueueGroup::USE_PRIORITY;
87 $lastTime = time(); // time since last slave check
88 do {
89 $job = ( $type === false )
90 ? $group->pop( JobQueueGroup::TYPE_DEFAULT, $flags )
91 : $group->pop( $type ); // job from a single queue
92 if ( $job ) { // found a job
93 ++$jobsRun;
94 $this->runJobsLog( $job->toString() . " STARTING" );
95
96 // Set timer to stop the job if too much CPU time is used
97 set_time_limit( $maxTime ?: 0 );
98 // Run the job...
99 wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
100 $t = microtime( true );
101 try {
102 $status = $job->run();
103 $error = $job->getLastError();
104 } catch ( MWException $e ) {
105 $status = false;
106 $error = get_class( $e ) . ': ' . $e->getMessage();
107 $e->report(); // write error to STDERR and the log
108 }
109 $timeMs = intval( ( microtime( true ) - $t ) * 1000 );
110 wfProfileOut( __METHOD__ . '-' . get_class( $job ) );
111 // Disable the timer
112 set_time_limit( 0 );
113
114 // Mark the job as done on success or when the job cannot be retried
115 if ( $status !== false || !$job->allowRetries() ) {
116 $group->ack( $job ); // done
117 }
118
119 if ( $status === false ) {
120 $this->runJobsLog( $job->toString() . " t=$timeMs error={$error}" );
121 } else {
122 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
123 }
124
125 // Break out if we hit the job count or wall time limits...
126 if ( $maxJobs && $jobsRun >= $maxJobs ) {
127 break;
128 } elseif ( $maxTime && ( time() - $startTime ) > $maxTime ) {
129 break;
130 }
131
132 // Don't let any of the main DB slaves get backed up
133 $timePassed = time() - $lastTime;
134 if ( $timePassed >= 5 || $timePassed < 0 ) {
135 wfWaitForSlaves();
136 $lastTime = time();
137 }
138 // Don't let any queue slaves/backups fall behind
139 if ( $jobsRun > 0 && ( $jobsRun % 100 ) == 0 ) {
140 $group->waitForBackups();
141 }
142
143 // Bail if near-OOM instead of in a job
144 $this->assertMemoryOK();
145 }
146 } while ( $job ); // stop when there are no jobs
147 }
148
149 /**
150 * Make sure that this script is not too close to the memory usage limit
151 * @throws MWException
152 */
153 private function assertMemoryOK() {
154 static $maxBytes = null;
155 if ( $maxBytes === null ) {
156 $m = array();
157 if ( preg_match( '!^(\d+)(k|m|g|)$!i', ini_get( 'memory_limit' ), $m ) ) {
158 list( , $num, $unit ) = $m;
159 $conv = array( 'g' => 1024*1024*1024, 'm' => 1024*1024, 'k' => 1024, '' => 1 );
160 $maxBytes = $num * $conv[strtolower( $unit )];
161 } else {
162 $maxBytes = 0;
163 }
164 }
165 $usedBytes = memory_get_usage();
166 if ( $maxBytes && $usedBytes >= .95*$maxBytes ) {
167 throw new MWException( "Detected excessive memory usage ($usedBytes/$maxBytes)." );
168 }
169 }
170
171 /**
172 * Log the job message
173 * @param $msg String The message to log
174 */
175 private function runJobsLog( $msg ) {
176 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
177 wfDebugLog( 'runJobs', $msg );
178 }
179 }
180
181 $maintClass = "RunJobs";
182 require_once RUN_MAINTENANCE_IF_MAIN;