workerIsLocked example

    public function consumeMessages(Request $request): JsonResponse
    {
        $receiverName = $request->get('receiver');

        if (!$receiverName || !$this->receiverLocator->has($receiverName)) {
            throw MessageQueueException::validReceiverNameNotProvided();
        }

        $consumerLock = $this->lockFactory->createLock('message_queue_consume_' . $receiverName);

        if (!$consumerLock->acquire()) {
            throw MessageQueueException::workerIsLocked($receiverName);
        }

        $receiver = $this->receiverLocator->get($receiverName);

        $workerDispatcher = new EventDispatcher();
        $listener = new CountHandledMessagesListener();
        $workerDispatcher->addSubscriber(new StopWorkerOnTimeLimitListener($this->pollInterval));
        $workerDispatcher->addSubscriber($listener);
        $workerDispatcher->addSubscriber($this->statsSubscriber);
        $workerDispatcher->addSubscriber($this->stopWorkerOnRestartSignalListener);
        $workerDispatcher->addSubscriber($this->earlyReturnListener);

        
public function testValidReceiverNameNotProvided(): void
    {
        $exception = MessageQueueException::validReceiverNameNotProvided();

        static::assertSame('FRAMEWORK__NO_VALID_RECEIVER_NAME_PROVIDED', $exception->getErrorCode());
        static::assertSame('No receiver name provided.', $exception->getMessage());
        static::assertSame(400, $exception->getStatusCode());
    }

    public function testWorkerIsLocked(): void
    {
        $exception = MessageQueueException::workerIsLocked('test');

        static::assertSame('FRAMEWORK__WORKER_IS_LOCKED', $exception->getErrorCode());
        static::assertSame('Another worker is already running for receiver: "test"', $exception->getMessage());
        static::assertSame(409, $exception->getStatusCode());
    }
}
Home | Imprint | This part of the site doesn't use cookies.