Example #1
0
 /**
  * @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;
 }
Example #2
0
 /**
  * @param Request $request
  * @return array
  */
 private function createAttributes(Request $request) : array
 {
     $attributes = ['content_type' => 'application/json', 'content_encoding' => 'UTF-8', 'delivery_mode' => 2, 'type' => $request->method(), 'timestamp' => $request->timestamp(), 'reply_to' => $this->queue->getName(), 'app_id' => $this->appId, 'user_id' => $this->queue->getConnection()->getOptions()->getLogin(), 'headers' => ['jsonrpc' => JsonRpcRequest::JSONRPC_VERSION]];
     if (null !== $request->id()) {
         $attributes['correlation_id'] = $request->id();
     }
     if (0 < $request->expiration()) {
         $attributes['expiration'] = $request->expiration();
     }
     return $attributes;
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * @param Queue $queue
  * @param string $exchange
  * @param array $routingKeys
  * @param array $bindArguments
  */
 private function bindQueue(Queue $queue, string $exchange, array $routingKeys, array $bindArguments)
 {
     if (empty($routingKeys)) {
         $queue->bind($exchange, '', $bindArguments);
     } else {
         foreach ($routingKeys as $routingKey) {
             $queue->bind($exchange, $routingKey, $bindArguments);
         }
     }
 }