Add BackgroundQueueHandler::enqueueIn() and rename enqueue() to enqueueAt()
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 17 Jun 2015 19:53:09 +0000 (21:53 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Sun, 21 Jun 2015 12:59:26 +0000 (14:59 +0200)
This also disallows scheduling jobs for execution in the past to prevent
starving of jobs with a non-immediate execution (previous default parameter
of zero).

wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php
wcfsetup/install/files/lib/system/cronjob/BackgroundQueueCleanUpCronjob.class.php

index a1beb8adb7433de79f13fe663d89a11fb4f4c143..00519189dcc0206a2a619953913cee94c8710c88 100644 (file)
@@ -2,6 +2,7 @@
 namespace wcf\system\background;
 use wcf\system\background\job\AbstractBackgroundJob;
 use wcf\system\exception\LoggedException;
+use wcf\system\exception\SystemException;
 use wcf\system\SingletonFactory;
 use wcf\system\WCF;
 
@@ -16,6 +17,18 @@ use wcf\system\WCF;
  * @category   Community Framework
  */
 class BackgroundQueueHandler extends SingletonFactory {
+       /**
+        * Enqueues the given job for execution in the specified number of
+        * seconds. Defaults to "as soon as possible" (0 seconds).
+        * 
+        * @param       \wcf\system\background\job\AbstractBackgroundJob        $job    The job to enqueue.
+        * @param       int                                                     $time   Minimum number of seconds to wait before performing the job.
+        * @see \wcf\system\background\BackgroundQueueHandler::enqueueAt()
+        */
+       public function enqueueIn(AbstractBackgroundJob $job, $time = 0) {
+               return self::enqueueAt($job, TIME_NOW + $time);
+       }
+       
        /**
         * Enqueues the given job for execution at the given time.
         * Note: The time is a minimum time. Depending on the size of
@@ -24,7 +37,11 @@ class BackgroundQueueHandler extends SingletonFactory {
         * @param       \wcf\system\background\job\AbstractBackgroundJob        $job    The job to enqueue.
         * @param       int                                                     $time   Earliest time to consider the job for execution.
         */
-       public function enqueue(AbstractBackgroundJob $job, $time = 0) {
+       public function enqueueAt(AbstractBackgroundJob $job, $time) {
+               if ($time < TIME_NOW) {
+                       throw new SystemException("You may not schedule a job in the past (".$time." is smaller than the current timestamp ".TIME_NOW.").");
+               }
+               
                $sql = "INSERT INTO     wcf".WCF_N."_background_job
                                        (job, time)
                        VALUES          (?, ?)";
@@ -55,7 +72,7 @@ class BackgroundQueueHandler extends SingletonFactory {
                        $job->fail();
                        
                        if ($job->getFailures() <= $job::MAX_FAILURES) {
-                               $this->enqueue($job, TIME_NOW + $job->retryAfter());
+                               $this->enqueueIn($job, $job->retryAfter());
                        }
                        else {
                                // job failed too often: log
index 657ff2befc4f48143a27788ebeafc42f8af3693a..d19dfd34af032b3a81b21c21108bb32eac55a6b3 100644 (file)
@@ -48,7 +48,7 @@ class BackgroundQueueCleanUpCronjob extends AbstractCronjob {
                                                $job->fail();
                                                
                                                if ($job->getFailures() <= $job::MAX_FAILURES) {
-                                                       BackgroundQueueHandler::getInstance()->enqueue($job, TIME_NOW + $job->retryAfter());
+                                                       BackgroundQueueHandler::getInstance()->enqueueIn($job, $job->retryAfter());
                                                }
                                        }
                                }