* Fix r74790 by actually calling setup (using parent::setup)
[lhc/web/wiklou.git] / maintenance / runJobs.php
index 70228ff..d6d2538 100644 (file)
@@ -2,10 +2,6 @@
 /**
  * This script starts pending jobs.
  *
- * Usage:
- *  --maxjobs <num> (default 10000)
- *  --type <job_cmd>
- *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * @ingroup Maintenance
  */
 
-require_once( dirname(__FILE__) . '/Maintenance.php' );
+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 
 class RunJobs extends Maintenance {
        public function __construct() {
-               global $wgUseNormalUser;
                parent::__construct();
                $this->mDescription = "Run pending jobs";
                $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
                $this->addOption( 'type', 'Type of job to run', false, true );
                $this->addOption( 'procs', 'Number of processes to use', false, true );
-               $wgUseNormalUser = true;
+               $this->addOption( 'exclusive', 'Run only one exclusive runJobs script at a time. Timeout is 1800 seconds. Useful for cron scripts.', false );
        }
-       
+
        public function memoryLimit() {
                // Don't eat all memory on the machine if we get a bad job.
                return "150M";
        }
 
        public function execute() {
+               if ( $this->lock() === false ) {
+                       exit( 0 );
+               }
+
                global $wgTitle;
                if ( $this->hasOption( 'procs' ) ) {
-                       $procs = intval( $this->getOption('procs') );
+                       $procs = intval( $this->getOption( 'procs' ) );
                        if ( $procs < 1 || $procs > 1000 ) {
                                $this->error( "Invalid argument to --procs", true );
                        }
                        $fc = new ForkController( $procs );
                        if ( $fc->start( $procs ) != 'child' ) {
+                               $this->unlock();
                                exit( 0 );
                        }
                }
@@ -60,22 +60,20 @@ class RunJobs extends Maintenance {
                $dbw = wfGetDB( DB_MASTER );
                $n = 0;
                $conds = '';
-               if ($type !== false)
-                       $conds = "job_cmd = " . $dbw->addQuotes($type);
+               if ( $type !== false )
+                       $conds = "job_cmd = " . $dbw->addQuotes( $type );
 
                while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
-                       $offset=0;
-                       for (;;) {
-                               $job = ($type == false) ?
-                                               Job::pop($offset)
-                                               : Job::pop_type($type);
-       
-                               if ($job == false)
+                       $offset = 0;
+                       for ( ; ; ) {
+                               $job = !$type ? Job::pop( $offset ) : Job::pop_type( $type );
+
+                               if ( !$job )
                                        break;
-       
+
                                wfWaitForSlaves( 5 );
                                $t = microtime( true );
-                               $offset=$job->id;
+                               $offset = $job->id;
                                $status = $job->run();
                                $t = microtime( true ) - $t;
                                $timeMs = intval( $t * 1000 );
@@ -89,6 +87,9 @@ class RunJobs extends Maintenance {
                                }
                        }
                }
+               if ( !$this->hasOption( 'procs' ) ) {
+                       $this->unlock();
+               }
        }
 
        /**
@@ -99,6 +100,25 @@ class RunJobs extends Maintenance {
                $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
                wfDebugLog( 'runJobs', $msg );
        }
+
+       protected function lock() {
+               if ( $this->hasOption( 'exclusive' ) ) {
+                       $cache = wfGetCache( CACHE_ANYTHING );
+                       $running = $cache->get( wfMemcKey( 'runjobs' ) );
+                       if ( $running ) {
+                               return false;
+                       } else {
+                               $cache->set( wfMemcKey( 'runjobs' ), '1', 1800 );
+                               return true;
+                       }
+               }
+               return true;
+       }
+
+       protected function unlock() {
+               wfGetCache( CACHE_ANYTHING )->delete( wfMemcKey( 'runjobs' ) );
+       }
+
 }
 
 $maintClass = "RunJobs";