/** * @param Envelope $envelope * @return DeliveryResult */ protected function handleInternalMessage(Envelope $envelope) : DeliveryResult { if ('shutdown' === $envelope->getType()) { $this->logger->info('Shutdown message received'); $this->shutdown(); $result = DeliveryResult::MSG_ACK(); } elseif ('reconfigure' === $envelope->getType()) { $this->logger->info('Reconfigure message received'); try { list($idleTimeout, $target, $prefetchSize, $prefetchCount) = json_decode($envelope->getBody()); if (is_numeric($idleTimeout)) { $idleTimeout = (double) $idleTimeout; } Assertion::float($idleTimeout); Assertion::min($target, 0); Assertion::min($prefetchSize, 0); Assertion::min($prefetchCount, 0); } catch (\Throwable $e) { $this->logger->error('Exception during reconfiguration: ' . $e->getMessage()); return DeliveryResult::MSG_REJECT(); } $this->idleTimeout = $idleTimeout; $this->target = $target; $this->queue->getChannel()->qos($prefetchSize, $prefetchCount); $this->blockSize = $prefetchCount; $result = DeliveryResult::MSG_ACK(); } elseif ('ping' === $envelope->getType()) { $this->logger->info('Ping message received'); $result = DeliveryResult::MSG_ACK(); } else { $this->logger->error('Invalid internal message: ' . $envelope->getType()); $result = DeliveryResult::MSG_REJECT(); } return $result; }
/** * Constructor * * @param Queue $queue * @param callable(JsonRpcRequest):JsonRpcResponse $deliveryCallback * @param LoggerInterface $logger * @param float $idleTimeout in seconds * @param string $consumerTag * @param string $appId */ public function __construct(Queue $queue, callable $deliveryCallback, LoggerInterface $logger, float $idleTimeout, string $consumerTag = '', string $appId = '') { if ('' === $consumerTag) { $consumerTag = bin2hex(random_bytes(24)); } if (extension_loaded('pcntl')) { declare (ticks=1); $this->usePcntlSignalDispatch = true; pcntl_signal(SIGTERM, [$this, 'shutdown']); pcntl_signal(SIGINT, [$this, 'shutdown']); pcntl_signal(SIGHUP, [$this, 'shutdown']); } $this->queue = $queue; $this->exchange = $queue->getChannel()->newExchange(); $this->exchange->setType('direct'); $this->deliveryCallback = $deliveryCallback; $this->logger = $logger; $this->idleTimeout = $idleTimeout; $this->consumerTag = $consumerTag; $this->appId = $appId; }