Re-queue stuck unique background jobs
authorCyperghost <olaf_schmitz_1@t-online.de>
Wed, 21 Feb 2024 11:54:06 +0000 (12:54 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Wed, 21 Feb 2024 11:54:06 +0000 (12:54 +0100)
wcfsetup/install/files/lib/system/cronjob/BackgroundQueueCleanUpCronjob.class.php

index eeda3710d2940e6c9403706c4cb5d7792d4e1a54..60a750f616138e61cdca06613292a83e8723d8a2 100644 (file)
@@ -4,6 +4,7 @@ namespace wcf\system\cronjob;
 
 use wcf\data\cronjob\Cronjob;
 use wcf\system\background\BackgroundQueueHandler;
+use wcf\system\background\job\AbstractUniqueBackgroundJob;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\WCF;
 
@@ -27,6 +28,8 @@ class BackgroundQueueCleanUpCronjob extends AbstractCronjob
         WCF::getDB()->beginTransaction();
         /** @noinspection PhpUnusedLocalVariableInspection */
         $committed = false;
+        /** @var AbstractUniqueBackgroundJob[] $uniqueJobs */
+        $uniqueJobs = [];
         try {
             $sql = "SELECT      jobID, job
                     FROM        wcf" . WCF_N . "_background_job
@@ -54,6 +57,10 @@ class BackgroundQueueCleanUpCronjob extends AbstractCronjob
                             BackgroundQueueHandler::getInstance()->enqueueIn($job, $job->retryAfter());
                         } else {
                             $job->onFinalFailure();
+
+                            if ($job instanceof AbstractUniqueBackgroundJob) {
+                                $uniqueJobs[] = $job;
+                            }
                         }
                     }
                 } catch (\Exception $e) {
@@ -84,5 +91,12 @@ class BackgroundQueueCleanUpCronjob extends AbstractCronjob
                 WCF::getDB()->rollBackTransaction();
             }
         }
+
+        // Requeue unique jobs if needed
+        foreach ($uniqueJobs as $job) {
+            if ($job->queueAgain()) {
+                BackgroundQueueHandler::getInstance()->enqueueIn($job->newInstance(), $job->retryAfter());
+            }
+        }
     }
 }