MediaWiki  master
JobQueue.php
Go to the documentation of this file.
00001 <?php
00031 abstract class JobQueue {
00032         protected $wiki; // string; wiki ID
00033         protected $type; // string; job type
00034         protected $order; // string; job priority for pop()
00035         protected $claimTTL; // integer; seconds
00036 
00037         const QoS_Atomic = 1; // integer; "all-or-nothing" job insertions
00038 
00042         protected function __construct( array $params ) {
00043                 $this->wiki     = $params['wiki'];
00044                 $this->type     = $params['type'];
00045                 $this->order    = isset( $params['order'] ) ? $params['order'] : 'random';
00046                 $this->claimTTL = isset( $params['claimTTL'] ) ? $params['claimTTL'] : 0;
00047         }
00048 
00070         final public static function factory( array $params ) {
00071                 $class = $params['class'];
00072                 if ( !MWInit::classExists( $class ) ) {
00073                         throw new MWException( "Invalid job queue class '$class'." );
00074                 }
00075                 $obj = new $class( $params );
00076                 if ( !( $obj instanceof self ) ) {
00077                         throw new MWException( "Class '$class' is not a " . __CLASS__ . " class." );
00078                 }
00079                 return $obj;
00080         }
00081 
00085         final public function getWiki() {
00086                 return $this->wiki;
00087         }
00088 
00092         final public function getType() {
00093                 return $this->type;
00094         }
00095 
00102         final public function isEmpty() {
00103                 wfProfileIn( __METHOD__ );
00104                 $res = $this->doIsEmpty();
00105                 wfProfileOut( __METHOD__ );
00106                 return $res;
00107         }
00108 
00113         abstract protected function doIsEmpty();
00114 
00122         final public function batchPush( array $jobs, $flags = 0 ) {
00123                 foreach ( $jobs as $job ) {
00124                         if ( $job->getType() !== $this->type ) {
00125                                 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
00126                         }
00127                 }
00128                 wfProfileIn( __METHOD__ );
00129                 $ok = $this->doBatchPush( $jobs, $flags );
00130                 if ( $ok ) {
00131                         wfIncrStats( 'job-insert', count( $jobs ) );
00132                 }
00133                 wfProfileOut( __METHOD__ );
00134                 return $ok;
00135         }
00136 
00141         abstract protected function doBatchPush( array $jobs, $flags );
00142 
00148         final public function pop() {
00149                 wfProfileIn( __METHOD__ );
00150                 $job = $this->doPop();
00151                 if ( $job ) {
00152                         wfIncrStats( 'job-pop' );
00153                 }
00154                 wfProfileOut( __METHOD__ );
00155                 return $job;
00156         }
00157 
00162         abstract protected function doPop();
00163 
00170         final public function ack( Job $job ) {
00171                 if ( $job->getType() !== $this->type ) {
00172                         throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
00173                 }
00174                 wfProfileIn( __METHOD__ );
00175                 $ok = $this->doAck( $job );
00176                 wfProfileOut( __METHOD__ );
00177                 return $ok;
00178         }
00179 
00184         abstract protected function doAck( Job $job );
00185 
00191         final public function waitForBackups() {
00192                 wfProfileIn( __METHOD__ );
00193                 $this->doWaitForBackups();
00194                 wfProfileOut( __METHOD__ );
00195         }
00196 
00201         protected function doWaitForBackups() {}
00202 }