Moved constant values from initialiseFromUser() to class definition
[lhc/web/wiklou.git] / includes / PoolCounter.php
index 825ae34..3851767 100644 (file)
@@ -9,8 +9,10 @@
  * of the cpu of the pool. This is also known as 'Michael Jackson effect'.
  *  The PoolCounter provides semaphore semantics for restricting the number
  * of workers that may be concurrently performing such single task.
+ *
+ *  By default PoolCounter_Stub is used, which provides no locking. You 
+ * can get a useful one in the PoolCounter extension.
  */
-
 abstract class PoolCounter {
        
        /* Return codes */
@@ -82,15 +84,15 @@ abstract class PoolCounter {
 
 class PoolCounter_Stub extends PoolCounter {
        function acquireForMe() {
-               return PoolCounter::LOCKED;
+               return Status::newGood( PoolCounter::LOCKED );
        }
 
        function acquireForAnyone() {
-               return PoolCounter::LOCKED;
+               return Status::newGood( PoolCounter::LOCKED );
        }
 
        function release() {
-               return PoolCounter::RELEASED;
+               return Status::newGood( PoolCounter::RELEASED );
        }
        
        public function __construct() {
@@ -143,36 +145,42 @@ abstract class PoolCounterWork {
                        $status = $this->poolCounter->acquireForMe();
                }
                
-               $result = false;
-               switch ( is_int( $status ) ? $status : PoolCounter::ERROR ) {
-                       case PoolCounter::LOCKED:
-                               $result = $this->doWork();
-                               $this->poolCounter->release();
-                               return $result;
-                       
-                       case PoolCounter::DONE:
-                               $result = $this->getCachedWork();
-                               if ( $result === false ) {
-                                       /* That someone else work didn't serve us.
-                                        * Acquire the lock for me
-                                        */
-                                       return $this->execute( true );
-                               }
-                               return $result;
-                               
-                       case PoolCounter::QUEUE_FULL:
-                       case PoolCounter::TIMEOUT:
-                               $result = $this->fallback();
+               if ( $status->isOK() ) {
+                       switch ( $status->value ) {
+                               case PoolCounter::LOCKED:
+                                       $result = $this->doWork();
+                                       $this->poolCounter->release();
+                                       return $result;
                                
-                               if ( $result !== false ) {
+                               case PoolCounter::DONE:
+                                       $result = $this->getCachedWork();
+                                       if ( $result === false ) {
+                                               /* That someone else work didn't serve us.
+                                                * Acquire the lock for me
+                                                */
+                                               return $this->execute( true );
+                                       }
                                        return $result;
-                               }
-                               /* no break */
-                       
-                       case PoolCounter::ERROR:
-                       default:
-                               return $this->error( $status );
+                                       
+                               case PoolCounter::QUEUE_FULL:
+                               case PoolCounter::TIMEOUT:
+                                       $result = $this->fallback();
+                                       
+                                       if ( $result !== false ) {
+                                               return $result;
+                                       }
+                                       /* no break */
+                               
+                               /* These two cases should never be hit... */
+                               case PoolCounter::ERROR:
+                               default:
+                                       $errors = array( PoolCounter::QUEUE_FULL => 'pool-queuefull', PoolCounter::TIMEOUT => 'pool-timeout' );
+                                       
+                                       $status = Status::newFatal( isset($errors[$status->value]) ? $errors[$status->value] : 'pool-errorunknown' );
+                                       /* continue to the error */
+                       }
                }
+               return $this->error( $status );
        }
        
        function __construct( $type, $key ) {