Example #1
0
 /**
  * @param Envelope $envelope
  * @param Queue $queue
  * @return DeliveryResult
  */
 protected function handleDelivery(Envelope $envelope, Queue $queue) : DeliveryResult
 {
     $this->countMessagesConsumed++;
     $this->countMessagesUnacked++;
     $this->lastDeliveryTag = $envelope->getDeliveryTag();
     $this->timestampLastMessage = microtime(true);
     $this->ack();
     $this->logger->debug('Handling delivery of message', $this->extractMessageInformation($envelope));
     if ($envelope->getAppId() === 'Humus\\Amqp') {
         $this->handleInternalMessage($envelope);
         return DeliveryResult::MSG_ACK();
     }
     try {
         $request = $this->requestFromEnvelope($envelope);
         $callback = $this->deliveryCallback;
         $response = $callback($request);
         if ('' === $request->id()) {
             // notifications have no reply
             return DeliveryResult::MSG_ACK();
         }
         if (!$response instanceof JsonRpcResponse) {
             $response = JsonRpcResponse::withResult($envelope->getCorrelationId(), $response);
         }
     } catch (Exception\InvalidJsonRpcVersion $e) {
         $this->logger->error('Invalid json rpc version', $this->extractMessageInformation($envelope));
         $response = JsonRpcResponse::withError($envelope->getCorrelationId(), new JsonRpcError(JsonRpcError::ERROR_CODE_32600));
     } catch (Exception\InvalidJsonRpcRequest $e) {
         $this->logger->error('Invalid json rpc request', $this->extractMessageInformation($envelope));
         $response = JsonRpcResponse::withError($envelope->getCorrelationId(), new JsonRpcError(JsonRpcError::ERROR_CODE_32600));
     } catch (Exception\JsonParseError $e) {
         $this->logger->error('Json parse error', $this->extractMessageInformation($envelope));
         $response = JsonRpcResponse::withError($envelope->getCorrelationId(), new JsonRpcError(JsonRpcError::ERROR_CODE_32700));
     } catch (\Throwable $e) {
         $extra = $this->extractMessageInformation($envelope);
         $extra['exception_class'] = get_class($e);
         $extra['exception_message'] = $e->getMessage();
         $extra['exception_trace'] = $e->getTraceAsString();
         $this->logger->error('Exception occurred', $extra);
         $response = JsonRpcResponse::withError($envelope->getCorrelationId(), new JsonRpcError(JsonRpcError::ERROR_CODE_32603));
     }
     $this->sendReply($response, $envelope);
     return DeliveryResult::MSG_ACK();
 }
Example #2
0
 /**
  * Handle process flag
  *
  * @param Envelope $envelope
  * @param $flag
  * @return void
  */
 protected function handleProcessFlag(Envelope $envelope, DeliveryResult $flag)
 {
     $this->countMessagesConsumed++;
     switch ($flag) {
         case DeliveryResult::MSG_REJECT():
             $this->queue->nack($envelope->getDeliveryTag(), Constants::AMQP_NOPARAM);
             $this->logger->debug('Rejected message', $this->extractMessageInformation($envelope));
             break;
         case DeliveryResult::MSG_REJECT_REQUEUE():
             $this->queue->nack($envelope->getDeliveryTag(), Constants::AMQP_REQUEUE);
             $this->logger->debug('Rejected and requeued message', $this->extractMessageInformation($envelope));
             break;
         case DeliveryResult::MSG_ACK():
             $this->countMessagesUnacked++;
             $this->lastDeliveryTag = $envelope->getDeliveryTag();
             $this->timestampLastMessage = microtime(true);
             $this->ack();
             break;
         case DeliveryResult::MSG_DEFER():
             $this->countMessagesUnacked++;
             $this->lastDeliveryTag = $envelope->getDeliveryTag();
             $this->timestampLastMessage = microtime(true);
             break;
     }
 }