split general cache helper functionality to its own class, so we can also easily...
[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 parent::__construct();
32 $this->mDescription = "Run pending jobs";
33 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
34 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', 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 }
38
39 public function memoryLimit() {
40 // Don't eat all memory on the machine if we get a bad job.
41 return "150M";
42 }
43
44 public function execute() {
45 global $wgTitle;
46 if ( $this->hasOption( 'procs' ) ) {
47 $procs = intval( $this->getOption( 'procs' ) );
48 if ( $procs < 1 || $procs > 1000 ) {
49 $this->error( "Invalid argument to --procs", true );
50 }
51 $fc = new ForkController( $procs );
52 if ( $fc->start() != 'child' ) {
53 exit( 0 );
54 }
55 }
56 $maxJobs = $this->getOption( 'maxjobs', false );
57 $maxTime = $this->getOption( 'maxtime', false );
58 $startTime = time();
59 $type = $this->getOption( 'type', false );
60 $wgTitle = Title::newFromText( 'RunJobs.php' );
61 $dbw = wfGetDB( DB_MASTER );
62 $n = 0;
63 $conds = '';
64 if ( $type !== false ) {
65 $conds = "job_cmd = " . $dbw->addQuotes( $type );
66 }
67
68 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
69 $offset = 0;
70 for ( ; ; ) {
71 $job = !$type ? Job::pop( $offset ) : Job::pop_type( $type );
72
73 if ( !$job ) {
74 break;
75 }
76
77 wfWaitForSlaves();
78 $t = microtime( true );
79 $offset = $job->id;
80 $status = $job->run();
81 $t = microtime( true ) - $t;
82 $timeMs = intval( $t * 1000 );
83 if ( !$status ) {
84 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
85 } else {
86 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
87 }
88
89 if ( $maxJobs && ++$n > $maxJobs ) {
90 break 2;
91 }
92 if ( $maxTime && time() - $startTime > $maxTime ) {
93 break 2;
94 }
95 }
96 }
97 }
98
99 /**
100 * Log the job message
101 * @param $msg String The message to log
102 */
103 private function runJobsLog( $msg ) {
104 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
105 wfDebugLog( 'runJobs', $msg );
106 }
107 }
108
109 $maintClass = "RunJobs";
110 require_once( RUN_MAINTENANCE_IF_MAIN );