From: Cyperghost Date: Wed, 21 Feb 2024 10:18:52 +0000 (+0100) Subject: Create a unique background job X-Git-Tag: 6.1.0_Alpha_1~158^2~12 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=8e16faafd1d6a84f27c79c3cfed724b0868b022f;p=GitHub%2FWoltLab%2FWCF.git Create a unique background job --- 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()); + } + } +}