Check whether the background job should be queued
authorCyperghost <olaf_schmitz_1@t-online.de>
Wed, 21 Feb 2024 10:48:58 +0000 (11:48 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Wed, 21 Feb 2024 10:48:58 +0000 (11:48 +0100)
wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php

index 992d53ea17edf9167c657cc72548a9f8bb80159f..0888fc6b29b13aa85af235b38ad8857729308ca9 100644 (file)
@@ -4,6 +4,7 @@ namespace wcf\system\background;
 
 use wcf\data\user\User;
 use wcf\system\background\job\AbstractBackgroundJob;
+use wcf\system\background\job\AbstractUniqueBackgroundJob;
 use wcf\system\exception\ParentClassException;
 use wcf\system\session\SessionHandler;
 use wcf\system\SingletonFactory;
@@ -70,6 +71,7 @@ final class BackgroundQueueHandler extends SingletonFactory
         if (!\is_array($jobs)) {
             $jobs = [$jobs];
         }
+
         foreach ($jobs as $job) {
             if (!($job instanceof AbstractBackgroundJob)) {
                 throw new ParentClassException(\get_class($job), AbstractBackgroundJob::class);
@@ -81,7 +83,22 @@ final class BackgroundQueueHandler extends SingletonFactory
                             (job, time)
                 VALUES      (?, ?)";
         $statement = WCF::getDB()->prepare($sql);
+        $sql = "SELECT jobID
+                FROM   wcf1_background_job
+                WHERE  identifier = ?
+                FOR UPDATE";
+        $selectJobStatement = WCF::getDB()->prepare($sql);
+
         foreach ($jobs as $job) {
+            if ($job instanceof AbstractUniqueBackgroundJob) {
+                // Check if the job is already in the queue
+                $selectJobStatement->execute([$job->identifier()]);
+                $jobID = $selectJobStatement->fetchSingleColumn();
+                if ($jobID !== null) {
+                    continue;
+                }
+            }
+
             $statement->execute([
                 \serialize($job),
                 $time,