Execute up to 5 jobs per background queue invocation
authorAlexander Ebert <ebert@woltlab.com>
Tue, 2 Jan 2018 11:12:24 +0000 (12:12 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 2 Jan 2018 11:12:24 +0000 (12:12 +0100)
wcfsetup/install/files/lib/action/BackgroundQueuePerformAction.class.php
wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php

index ff27fb836d3e53edd39198d27ec4099846a0d717..08cc882a131a0582b03e13c65f041c8c9a64f03b 100644 (file)
@@ -12,6 +12,12 @@ use wcf\system\background\BackgroundQueueHandler;
  * @since      3.0
  */
 class BackgroundQueuePerformAction extends AbstractAction {
+       /**
+        * number of jobs that will be processed per invocation
+        * @var integer
+        */
+       public static $jobsPerRun = 5;
+       
        /**
         * @inheritDoc
         */
@@ -19,7 +25,12 @@ class BackgroundQueuePerformAction extends AbstractAction {
                parent::execute();
                
                header('Content-type: application/json');
-               BackgroundQueueHandler::getInstance()->performNextJob();
+               for ($i = 0; $i < self::$jobsPerRun; $i++) {
+                       if (BackgroundQueueHandler::getInstance()->performNextJob() === false) {
+                               // there were no more jobs
+                               break;
+                       }
+               }
                echo BackgroundQueueHandler::getInstance()->getRunnableCount();
                exit;
        }
index 6ae3f32f1b3d3a78640a5ede66ba8698793cd124..54bc32461b7b3852e7ed802dac9937763c194181 100644 (file)
@@ -139,6 +139,8 @@ class BackgroundQueueHandler extends SingletonFactory {
        /**
         * Performs the (single) job that is due next.
         * This method automatically handles requeuing in case of failure.
+        * 
+        * @return      boolean         true if this call attempted to execute a job regardless of its result
         */
        public function performNextJob() {
                WCF::getDB()->beginTransaction();
@@ -158,7 +160,7 @@ class BackgroundQueueHandler extends SingletonFactory {
                        $row = $statement->fetchSingleRow();
                        if (!$row) {
                                // nothing to do here
-                               return;
+                               return false;
                        }
                        
                        // lock job
@@ -178,7 +180,7 @@ class BackgroundQueueHandler extends SingletonFactory {
                                // somebody stole the job
                                // this cannot happen unless MySQL violates it's contract to lock the row
                                // -> silently ignore, there will be plenty of other opportunities to perform a job
-                               return;
+                               return true;
                        }
                        WCF::getDB()->commitTransaction();
                        $committed = true;
@@ -210,6 +212,8 @@ class BackgroundQueueHandler extends SingletonFactory {
                        $statement = WCF::getDB()->prepareStatement($sql);
                        $statement->execute([$row['jobID']]);
                }
+               
+               return true;
        }
        
        /**