Allow cleanupSpam.php optionally delete offending pages
[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::defaultQueueConditions( );
66 } else {
67 $conds = "job_cmd = " . $dbw->addQuotes( $type );
68 }
69
70 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
71 $offset = 0;
72 for ( ; ; ) {
73 $job = !$type ? Job::pop( $offset ) : Job::pop_type( $type );
74
75 if ( !$job ) {
76 break;
77 }
78
79 wfWaitForSlaves();
80 $t = microtime( true );
81 $offset = $job->id;
82 $status = $job->run();
83 $t = microtime( true ) - $t;
84 $timeMs = intval( $t * 1000 );
85 if ( !$status ) {
86 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
87 } else {
88 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
89 }
90
91 if ( $maxJobs && ++$n > $maxJobs ) {
92 break 2;
93 }
94 if ( $maxTime && time() - $startTime > $maxTime ) {
95 break 2;
96 }
97 }
98 }
99 }
100
101 /**
102 * Log the job message
103 * @param $msg String The message to log
104 */
105 private function runJobsLog( $msg ) {
106 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
107 wfDebugLog( 'runJobs', $msg );
108 }
109 }
110
111 $maintClass = "RunJobs";
112 require_once( RUN_MAINTENANCE_IF_MAIN );