Merge "Script to convert PHP i18n to JSON"
[lhc/web/wiklou.git] / maintenance / runJobs.php
1 <?php
2 /**
3 * Run pending jobs.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script that runs pending jobs.
28 *
29 * @ingroup Maintenance
30 */
31 class RunJobs extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Run pending jobs";
35 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
36 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
37 $this->addOption( 'type', 'Type of job to run', false, true );
38 $this->addOption( 'procs', 'Number of processes to use', false, true );
39 }
40
41 public function memoryLimit() {
42 if ( $this->hasOption( 'memory-limit' ) ) {
43 return parent::memoryLimit();
44 }
45 // Don't eat all memory on the machine if we get a bad job.
46 return "150M";
47 }
48
49 public function execute() {
50 global $wgTitle;
51
52 if ( wfReadOnly() ) {
53 $this->error( "Unable to run jobs; the wiki is in read-only mode.", 1 ); // die
54 }
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 } elseif ( $procs != 1 ) {
61 $fc = new ForkController( $procs );
62 if ( $fc->start() != 'child' ) {
63 exit( 0 );
64 }
65 }
66 }
67 $maxJobs = $this->getOption( 'maxjobs', false );
68 $maxTime = $this->getOption( 'maxtime', false );
69 $startTime = time();
70 $type = $this->getOption( 'type', false );
71 $wgTitle = Title::newFromText( 'RunJobs.php' );
72 $jobsRun = 0; // counter
73
74 $group = JobQueueGroup::singleton();
75 // Handle any required periodic queue maintenance
76 $count = $group->executeReadyPeriodicTasks();
77 if ( $count > 0 ) {
78 $this->runJobsLog( "Executed $count periodic queue task(s)." );
79 }
80
81 $flags = JobQueueGroup::USE_CACHE | JobQueueGroup::USE_PRIORITY;
82 $lastTime = time(); // time since last slave check
83 do {
84 $job = ( $type === false )
85 ? $group->pop( JobQueueGroup::TYPE_DEFAULT, $flags )
86 : $group->pop( $type ); // job from a single queue
87 if ( $job ) { // found a job
88 ++$jobsRun;
89 $this->runJobsLog( $job->toString() . " STARTING" );
90
91 // Set timer to stop the job if too much CPU time is used
92 set_time_limit( $maxTime ?: 0 );
93 // Run the job...
94 wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
95 $t = microtime( true );
96 try {
97 $status = $job->run();
98 $error = $job->getLastError();
99 } catch ( MWException $e ) {
100 $status = false;
101 $error = get_class( $e ) . ': ' . $e->getMessage();
102 $e->report(); // write error to STDERR and the log
103 }
104 $timeMs = intval( ( microtime( true ) - $t ) * 1000 );
105 wfProfileOut( __METHOD__ . '-' . get_class( $job ) );
106 // Disable the timer
107 set_time_limit( 0 );
108
109 // Mark the job as done on success or when the job cannot be retried
110 if ( $status !== false || !$job->allowRetries() ) {
111 $group->ack( $job ); // done
112 }
113
114 if ( $status === false ) {
115 $this->runJobsLog( $job->toString() . " t=$timeMs error={$error}" );
116 } else {
117 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
118 }
119
120 // Break out if we hit the job count or wall time limits...
121 if ( $maxJobs && $jobsRun >= $maxJobs ) {
122 break;
123 } elseif ( $maxTime && ( time() - $startTime ) > $maxTime ) {
124 break;
125 }
126
127 // Don't let any of the main DB slaves get backed up
128 $timePassed = time() - $lastTime;
129 if ( $timePassed >= 5 || $timePassed < 0 ) {
130 wfWaitForSlaves();
131 $lastTime = time();
132 }
133 // Don't let any queue slaves/backups fall behind
134 if ( $jobsRun > 0 && ( $jobsRun % 100 ) == 0 ) {
135 $group->waitForBackups();
136 }
137
138 // Bail if near-OOM instead of in a job
139 $this->assertMemoryOK();
140 }
141 } while ( $job ); // stop when there are no jobs
142 }
143
144 /**
145 * Make sure that this script is not too close to the memory usage limit
146 * @throws MWException
147 */
148 private function assertMemoryOK() {
149 static $maxBytes = null;
150 if ( $maxBytes === null ) {
151 $m = array();
152 if ( preg_match( '!^(\d+)(k|m|g|)$!i', ini_get( 'memory_limit' ), $m ) ) {
153 list( , $num, $unit ) = $m;
154 $conv = array( 'g' => 1024 * 1024 * 1024, 'm' => 1024 * 1024, 'k' => 1024, '' => 1 );
155 $maxBytes = $num * $conv[strtolower( $unit )];
156 } else {
157 $maxBytes = 0;
158 }
159 }
160 $usedBytes = memory_get_usage();
161 if ( $maxBytes && $usedBytes >= 0.95 * $maxBytes ) {
162 throw new MWException( "Detected excessive memory usage ($usedBytes/$maxBytes)." );
163 }
164 }
165
166 /**
167 * Log the job message
168 * @param $msg String The message to log
169 */
170 private function runJobsLog( $msg ) {
171 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
172 wfDebugLog( 'runJobs', $msg );
173 }
174 }
175
176 $maintClass = "RunJobs";
177 require_once RUN_MAINTENANCE_IF_MAIN;