Add BackgroundQueueHandler::performJob(AbstractBackgroundJob)…
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 15 Jun 2015 17:11:43 +0000 (19:11 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Sun, 21 Jun 2015 12:59:26 +0000 (14:59 +0200)
… and rename performJob(void) to performNextJob(void)

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

index b8db0b3956d2edeab4ed5067f803b03131e65a76..a1beb8adb7433de79f13fe663d89a11fb4f4c143 100644 (file)
@@ -34,12 +34,41 @@ class BackgroundQueueHandler extends SingletonFactory {
                        $time
                ]);
        }
-
+       
+       /**
+        * Immediatly performs the given job.
+        * This method automatically handles requeuing in case of failure.
+        * 
+        * This method is used internally by performNextJob(), but it can
+        * be useful if you wish immediate execution of a certain job, but
+        * don't want to miss the automated error handling mechanism of the
+        * queue.
+        * 
+        * @param       \wcf\system\background\job\AbstractBackgroundJob        $job    The job to perform.
+        */
+       public function performJob(AbstractBackgroundJob $job) {
+               try {
+                       $job->perform();
+               }
+               catch (\Exception $e) {
+                       // gotta catch 'em all
+                       $job->fail();
+                       
+                       if ($job->getFailures() <= $job::MAX_FAILURES) {
+                               $this->enqueue($job, TIME_NOW + $job->retryAfter());
+                       }
+                       else {
+                               // job failed too often: log
+                               if ($e instanceof LoggedException) $e->getExceptionID();
+                       }
+               }
+       }
+       
        /**
         * Performs the (single) job that is due next.
         * This method automatically handles requeuing in case of failure.
         */
-       public function performJob() {
+       public function performNextJob() {
                WCF::getDB()->beginTransaction();
                $commited = false;
                try {
@@ -91,26 +120,12 @@ class BackgroundQueueHandler extends SingletonFactory {
                        // no shut up operator, exception will be caught
                        $job = unserialize($row['job']);
                        if ($job) {
-                               $job->perform();
+                               $this->performJob($job);
                        }
                }
                catch (\Exception $e) {
-                       // gotta catch 'em all
-                       if ($job) {
-                               $job->fail();
-                               
-                               if ($job->getFailures() <= $job::MAX_FAILURES) {
-                                       $this->enqueue($job, TIME_NOW + $job->retryAfter());
-                               }
-                               else {
-                                       // job failed too often: log
-                                       if ($e instanceof LoggedException) $e->getExceptionID();
-                               }
-                       }
-                       else {
-                               // job is completely broken: log
-                               if ($e instanceof LoggedException) $e->getExceptionID();
-                       }
+                       // job is completely broken: log
+                       if ($e instanceof LoggedException) $e->getExceptionID();
                }
                finally {
                        // remove entry of processed job