From 8e16faafd1d6a84f27c79c3cfed724b0868b022f Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Wed, 21 Feb 2024 11:18:52 +0100 Subject: [PATCH] Create a unique background job --- .../job/AbstractUniqueBackgroundJob.class.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/background/job/AbstractUniqueBackgroundJob.class.php diff --git a/wcfsetup/install/files/lib/system/background/job/AbstractUniqueBackgroundJob.class.php b/wcfsetup/install/files/lib/system/background/job/AbstractUniqueBackgroundJob.class.php new file mode 100644 index 0000000000..5c60ba3b5a --- /dev/null +++ b/wcfsetup/install/files/lib/system/background/job/AbstractUniqueBackgroundJob.class.php @@ -0,0 +1,58 @@ + + * @since 6.1 + */ +abstract class AbstractUniqueBackgroundJob extends AbstractBackgroundJob +{ + /** + * @inheritDoc + */ + final public const MAX_FAILURES = 1; + + /** + * Returns a unique identifier for this job. + * + * @return string + */ + public function identifier(): string + { + return static::class; + } + + #[\Override] + final public function perform() + { + $this->run(); + if ($this->requeue()) { + BackgroundQueueHandler::getInstance()->enqueueIn($this); + } + } + + /** + * Runs the job. + */ + abstract protected function run(): void; + + /** + * Returns whether this job should be queued again because it has more to do. + * + * @return bool + */ + abstract protected function requeue(): bool; + + #[\Override] + final public function onFinalFailure() + { + if ($this->requeue()) { + BackgroundQueueHandler::getInstance()->enqueueIn($this, $this->retryAfter()); + } + } +} -- 2.20.1