From 86543b0cd635ec0ae8411e47ea5f1566d21fb62e Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Wed, 21 Feb 2024 12:47:04 +0100 Subject: [PATCH] Create a new instance of this job to reset the failure counter by re-queuing the job. --- .../BackgroundQueueHandler.class.php | 9 +++-- .../job/AbstractUniqueBackgroundJob.class.php | 35 ++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php b/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php index 0b83c1ffd6..d08e9e7cc5 100644 --- a/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php +++ b/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php @@ -104,9 +104,9 @@ final class BackgroundQueueHandler extends SingletonFactory } $statement->execute([ - \serialize($job), - $time, - $identifier + \serialize($job), + $time, + $identifier ]); } WCF::getDB()->commitTransaction(); @@ -240,6 +240,9 @@ final class BackgroundQueueHandler extends SingletonFactory $statement = WCF::getDB()->prepare($sql); $statement->execute([$row['jobID']]); } + if ($job instanceof AbstractUniqueBackgroundJob && $job->queueAgain()) { + $this->enqueueIn($job->newInstance(), $job->retryAfter()); + } return true; } diff --git a/wcfsetup/install/files/lib/system/background/job/AbstractUniqueBackgroundJob.class.php b/wcfsetup/install/files/lib/system/background/job/AbstractUniqueBackgroundJob.class.php index 61f79de333..73f7873e38 100644 --- a/wcfsetup/install/files/lib/system/background/job/AbstractUniqueBackgroundJob.class.php +++ b/wcfsetup/install/files/lib/system/background/job/AbstractUniqueBackgroundJob.class.php @@ -2,8 +2,6 @@ namespace wcf\system\background\job; -use wcf\system\background\BackgroundQueueHandler; - /** * @author Olaf Braun * @copyright 2001-2024 WoltLab GmbH @@ -27,32 +25,35 @@ abstract class AbstractUniqueBackgroundJob extends AbstractBackgroundJob return static::class; } - #[\Override] - final public function perform() - { - $this->run(); - if ($this->requeue()) { - BackgroundQueueHandler::getInstance()->enqueueIn($this); - } - } - /** - * Runs the job. + * Returns a new instance of this job to be queued again. + * This will reset the fail counter. + * + * @return AbstractUniqueBackgroundJob */ - abstract protected function run(): void; + public function newInstance(): AbstractUniqueBackgroundJob + { + return new static(); + } /** * Returns whether this job should be queued again because it has more to do. * * @return bool */ - abstract protected function requeue(): bool; + abstract public function queueAgain(): bool; #[\Override] final public function onFinalFailure() { - if ($this->requeue()) { - BackgroundQueueHandler::getInstance()->enqueueIn($this, $this->retryAfter()); - } + // onFailure() and onFinalFailure() are called at the same time. + // Do your stuff in onFailure(). + } + + #[\Override] + public function retryAfter() + { + // change the default value to 60 seconds + return 60; } } -- 2.20.1