From: Alexander Ebert Date: Mon, 14 Nov 2022 15:06:56 +0000 (+0100) Subject: Conditionally add `woltlab-background-queue-check: yes` to the response X-Git-Tag: 6.0.0_Alpha_1~721^2~8 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=4051e45663f0aaa1ccbfd788aa67d70f6ca15bd8;p=GitHub%2FWoltLab%2FWCF.git Conditionally add `woltlab-background-queue-check: yes` to the response This header is intended to signal the client that an async check for pending jobs in the background queue should be dispatched. --- diff --git a/wcfsetup/install/files/lib/http/middleware/TriggerBackgroundQueue.class.php b/wcfsetup/install/files/lib/http/middleware/TriggerBackgroundQueue.class.php new file mode 100644 index 0000000000..3e93059974 --- /dev/null +++ b/wcfsetup/install/files/lib/http/middleware/TriggerBackgroundQueue.class.php @@ -0,0 +1,54 @@ + + * @package WoltLabSuite\Core\Http\Middleware + * @since 6.0 + */ +final class TriggerBackgroundQueue implements MiddlewareInterface +{ + private const HEADER_NAME = 'woltlab-background-queue-check'; + private const HEADER_VALUE = 'yes'; + + private readonly BackgroundQueueHandler $backgroundQueueHandler; + + public function __construct() + { + $this->backgroundQueueHandler = BackgroundQueueHandler::getInstance(); + } + + /** + * @inheritDoc + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $response = $handler->handle($request); + if (!$this->backgroundQueueHandler->hasPendingCheck()) { + return $response; + } + + if ($response instanceof LegacyPlaceholderResponse) { + \header( + \sprintf('%s: %s', self::HEADER_NAME, self::HEADER_VALUE) + ); + + return $response; + } + + return $response->withHeader(self::HEADER_NAME, self::HEADER_VALUE); + } +} diff --git a/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php b/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php index 19c1686511..58bf713f92 100644 --- a/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php +++ b/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php @@ -20,6 +20,12 @@ use wcf\system\WCF; */ final class BackgroundQueueHandler extends SingletonFactory { + /** + * Indicates that the client should trigger a check for + * pending jobs in the background queue. + */ + private bool $hasPendingCheck = false; + /** * Forces checking whether a background queue item is due. * This means that the AJAX request to BackgroundQueuePerformAction is triggered. @@ -31,6 +37,8 @@ final class BackgroundQueueHandler extends SingletonFactory WCF::getTPL()->assign([ 'forceBackgroundQueuePerform' => true, ]); + + $this->hasPendingCheck = true; } /** @@ -227,4 +235,12 @@ final class BackgroundQueueHandler extends SingletonFactory return $statement->fetchSingleColumn(); } + + /** + * @since 6.0 + */ + public function hasPendingCheck(): bool + { + return $this->hasPendingCheck; + } } diff --git a/wcfsetup/install/files/lib/system/request/RequestHandler.class.php b/wcfsetup/install/files/lib/system/request/RequestHandler.class.php index bf11b184c8..5299cdf67d 100644 --- a/wcfsetup/install/files/lib/system/request/RequestHandler.class.php +++ b/wcfsetup/install/files/lib/system/request/RequestHandler.class.php @@ -21,6 +21,7 @@ use wcf\http\middleware\EnforceFrameOptions; use wcf\http\middleware\HandleStartupErrors; use wcf\http\middleware\JsonBody; use wcf\http\middleware\PreventMimeSniffing; +use wcf\http\middleware\TriggerBackgroundQueue; use wcf\http\middleware\Xsrf; use wcf\http\Pipeline; use wcf\system\application\ApplicationHandler; @@ -110,6 +111,7 @@ final class RequestHandler extends SingletonFactory new CheckForExpiredAppEvaluation(), new CheckForOfflineMode(), new JsonBody(), + new TriggerBackgroundQueue(), ]); $response = $pipeline->process($psrRequest, $this->getActiveRequest());