cl_sortkey should be 230 bytes long, not 255
[lhc/web/wiklou.git] / maintenance / runJobs.php
1 <?php
2 /**
3 * This script starts 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 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class RunJobs extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Run pending jobs";
29 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
30 $this->addOption( 'type', 'Type of job to run', false, true );
31 $this->addOption( 'procs', 'Number of processes to use', false, true );
32 $this->addOption( 'exclusive', 'Run only one exclusive runJobs script at a time. Timeout is 1800 seconds. Useful for cron scripts.', false );
33 }
34
35 public function memoryLimit() {
36 // Don't eat all memory on the machine if we get a bad job.
37 return "150M";
38 }
39
40 public function execute() {
41 if ( $this->lock() === false ) {
42 exit( 0 );
43 }
44
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( $procs ) != 'child' ) {
53 $this->unlock();
54 exit( 0 );
55 }
56 }
57 $maxJobs = $this->getOption( 'maxjobs', 10000 );
58 $type = $this->getOption( 'type', false );
59 $wgTitle = Title::newFromText( 'RunJobs.php' );
60 $dbw = wfGetDB( DB_MASTER );
61 $n = 0;
62 $conds = '';
63 if ( $type !== false )
64 $conds = "job_cmd = " . $dbw->addQuotes( $type );
65
66 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
67 $offset = 0;
68 for ( ; ; ) {
69 $job = !$type ? Job::pop( $offset ) : Job::pop_type( $type );
70
71 if ( !$job )
72 break;
73
74 wfWaitForSlaves( 5 );
75 $t = microtime( true );
76 $offset = $job->id;
77 $status = $job->run();
78 $t = microtime( true ) - $t;
79 $timeMs = intval( $t * 1000 );
80 if ( !$status ) {
81 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
82 } else {
83 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
84 }
85 if ( $maxJobs && ++$n > $maxJobs ) {
86 break 2;
87 }
88 }
89 }
90 if ( !$this->hasOption( 'procs' ) ) {
91 $this->unlock();
92 }
93 }
94
95 /**
96 * Log the job message
97 * @param $msg String The message to log
98 */
99 private function runJobsLog( $msg ) {
100 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
101 wfDebugLog( 'runJobs', $msg );
102 }
103
104 protected function lock() {
105 if ( $this->hasOption( 'exclusive' ) ) {
106 $cache = wfGetCache( CACHE_ANYTHING );
107 $running = $cache->get( wfMemcKey( 'runjobs' ) );
108 if ( $running ) {
109 return false;
110 } else {
111 $cache->set( wfMemcKey( 'runjobs' ), '1', 1800 );
112 return true;
113 }
114 }
115 return true;
116 }
117
118 protected function unlock() {
119 wfGetCache( CACHE_ANYTHING )->delete( wfMemcKey( 'runjobs' ) );
120 }
121
122 }
123
124 $maintClass = "RunJobs";
125 require_once( DO_MAINTENANCE );