Exemplo n.º 1
0
 /**
  * {@inheritDoc}
  */
 public function process(Message $message, array $options)
 {
     try {
         return $this->processor->process($message, $options);
     } catch (\Exception $e) {
         $properties = $message->getProperties();
         $attempts = 0;
         if (isset($properties['headers']['swarrot_retry_attempts'])) {
             $attempts = $properties['headers']['swarrot_retry_attempts'];
         }
         $attempts++;
         if ($attempts > $options['retry_attempts']) {
             $this->logger and $this->logger->warning(sprintf('[Retry] Stop attempting to process message after %d attempts', $attempts), ['swarrot_processor' => 'retry']);
             throw $e;
         }
         $properties['headers'] = array('swarrot_retry_attempts' => $attempts);
         // workaround for https://github.com/pdezwart/php-amqp/issues/170. See https://github.com/swarrot/swarrot/issues/103
         if (isset($properties['delivery_mode']) && 0 === $properties['delivery_mode']) {
             unset($properties['delivery_mode']);
         }
         $message = new Message($message->getBody(), $properties);
         $key = str_replace('%attempt%', $attempts, $options['retry_key_pattern']);
         $this->logger and $this->logger->warning(sprintf('[Retry] An exception occurred. Republish message for the %d times (key: %s)', $attempts, $key), ['swarrot_processor' => 'retry', 'exception' => $e]);
         $this->publisher->publish($message, $key);
     }
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function process(Message $message, array $options)
 {
     try {
         return $this->processor->process($message, $options);
     } catch (\Exception $e) {
         $properties = $message->getProperties();
         $attempts = 0;
         if (isset($properties['headers']['swarrot_retry_attempts'])) {
             $attempts = $properties['headers']['swarrot_retry_attempts'];
         }
         ++$attempts;
         if ($attempts > $options['retry_attempts']) {
             $this->logger and $this->logger->warning(sprintf('[Retry] Stop attempting to process message after %d attempts', $attempts), ['swarrot_processor' => 'retry']);
             throw $e;
         }
         if (!isset($properties['headers'])) {
             $properties['headers'] = array();
         }
         $properties['headers']['swarrot_retry_attempts'] = $attempts;
         $message = new Message($message->getBody(), $properties);
         $key = str_replace('%attempt%', $attempts, $options['retry_key_pattern']);
         $this->logger and $this->logger->warning(sprintf('[Retry] An exception occurred. Republish message for the %d times (key: %s)', $attempts, $key), ['swarrot_processor' => 'retry', 'exception' => $e]);
         $this->publisher->publish($message, $key);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function publish(Message $message, $key = null)
 {
     $properties = $message->getProperties();
     if (isset($properties['application_headers'])) {
         if (!isset($properties['headers'])) {
             $properties['headers'] = array();
         }
         foreach ($properties['application_headers'] as $header => $value) {
             if (!is_array($value) || 2 !== count($value)) {
                 throw new \InvalidArgumentException('Unexpected value for application_headers "' . $header . '"');
             }
             $properties['headers'][$header] = $value[1];
         }
         unset($properties['application_headers']);
     }
     $this->exchange->publish($message->getBody(), $key, $this->flags, $properties);
 }
Exemplo n.º 4
0
 /** {@inheritDoc} */
 public function process(Message $message, array $options)
 {
     $properties = $message->getProperties();
     // check for invalid correlation_id properties (not set, or invalid)
     if (!isset($properties['correlation_id'])) {
         return;
     }
     if ($properties['correlation_id'] !== $options['rpc_client_correlation_id']) {
         return;
     }
     $result = null;
     $this->logger and $this->logger->info('Message received from the RPC Server ; terminating consumer', array('correlation_id' => $properties['correlation_id']));
     $this->awoken = true;
     if (null !== $this->processor) {
         $this->logger and $this->logger->info('Sending message to sub-processor');
         $result = $this->processor->process($message, $options);
     }
     return $result;
 }
 /** {@inheritdoc} */
 public function publish(Message $message, $key = null)
 {
     $properties = $message->getProperties();
     if (isset($properties['headers'])) {
         if (!isset($properties['application_headers'])) {
             $properties['application_headers'] = [];
         }
         foreach ($properties['headers'] as $header => $value) {
             if (is_array($value)) {
                 $type = 'A';
             } elseif (is_int($value)) {
                 $type = 'I';
             } else {
                 $type = 'S';
             }
             $properties['application_headers'][$header] = [$type, $value];
         }
     }
     $amqpMessage = new AMQPMessage($message->getBody(), $properties);
     $this->channel->basic_publish($amqpMessage, $this->exchange, (string) $key);
 }
 /**
  * {@inheritdoc}
  */
 public function publish(Message $message, $key = null)
 {
     $properties = $message->getProperties();
     if (isset($properties['application_headers'])) {
         if (!isset($properties['headers'])) {
             $properties['headers'] = [];
         }
         foreach ($properties['application_headers'] as $header => $value) {
             if (!is_array($value) || 2 !== count($value)) {
                 throw new \InvalidArgumentException('Unexpected value for application_headers "' . $header . '"');
             }
             $properties['headers'][$header] = $value[1];
         }
         unset($properties['application_headers']);
     }
     $body = $message->getBody();
     if (empty($body)) {
         $this->logger->notice('Publishing empty message.', ['message' => $body, 'exchange' => $this->exchange->getName(), 'key' => $key]);
     }
     $this->exchange->publish($message->getBody(), $key, $this->flags, $this->sanitizeProperties($properties));
 }
 public function process(Message $message, array $options)
 {
     $data = json_decode($message->getBody(), true);
     $manifest = $this->manager->getRepository('AppBundle:Manifest')->find($data['id']);
     $webhooks = $this->manager->getRepository('AppBundle:Webhook')->findByRepository($manifest->getRepository());
     $payload = $this->getPayload($manifest);
     /** @var Webhook $webhook */
     foreach ($webhooks as $webhook) {
         $options = ['http' => ['method' => 'POST', 'header' => "Content-Type: application/json\r\n", 'content' => $payload, 'timeout' => 2, 'ignore_errors' => true]];
         $context = stream_context_create($options);
         $response = @file_get_contents($webhook->getUrl(), null, $context);
         if (isset($http_response_header) && is_array($http_response_header)) {
             $status = $http_response_header[0];
         } else {
             $status = 'Error';
         }
         unset($http_response_header);
         $webhook->setLastStatus($status);
         $webhook->setLastCall(new \DateTime());
     }
     $this->manager->flush();
 }
Exemplo n.º 8
0
 /**
  * @param \Exception|\Throwable $exception
  * @param Message               $message
  * @param array                 $options
  */
 private function handleException($exception, Message $message, array $options)
 {
     $this->logger and $this->logger->warning(sprintf('[InstantRetry] An exception occurred. Message #%d will be processed again in %d ms', $message->getId(), $options['instant_retry_delay'] / 1000), ['swarrot_processor' => 'instant_retry', 'exception' => $exception]);
     usleep($options['instant_retry_delay']);
 }
 public function process(Message $message, array $options)
 {
     $this->messagePublisher->publish(new Message($message->getBody()), $this->routingKey);
 }
Exemplo n.º 10
0
 /**
  * {@inheritDoc}
  */
 public function nack(Message $message, $requeue = false)
 {
     $this->channel->basic_nack($message->getId(), false, $requeue);
 }
 /**
  * @param Message $message
  * @param array   $options
  *
  * @return bool|void
  */
 public function process(Message $message, array $options)
 {
     $certification = $this->serializer->deserialize($message->getBody(), 'Certificationy\\Component\\Certy\\Model\\Certification', 'json');
     $this->calculator->compute($certification);
 }
Exemplo n.º 12
0
 /**
  * {@inheritDoc}
  */
 public function nack(Message $message, $requeue = false)
 {
     $this->queue->nack($message->getId(), $requeue ? AMQP_REQUEUE : null);
 }
Exemplo n.º 13
0
 public function process(Message $message, array $options)
 {
     printf("Consume message #%d\n", $message->getId());
 }
Exemplo n.º 14
0
 public function process(Message $message, array $options)
 {
     $properties = $message->getProperties();
     $body = json_decode($message->getBody(), true);
     if (!isset($properties['wisembly_attempts'])) {
         $properties['wisembly_attempts'] = 0;
     }
     if (!isset($body['arguments'])) {
         $body['arguments'] = [];
     }
     // add environment
     $body['arguments'][] = '--env';
     $body['arguments'][] = $this->environment;
     // add verbosity
     switch ($this->verbosity) {
         case OutputInterface::VERBOSITY_DEBUG:
             $body['arguments'][] = '-vvv';
             break;
         case OutputInterface::VERBOSITY_VERY_VERBOSE:
             $body['arguments'][] = '-vv';
             break;
         case OutputInterface::VERBOSITY_VERBOSE:
             $body['arguments'][] = '--verbose';
             break;
         case OutputInterface::VERBOSITY_QUIET:
             $body['arguments'][] = '--quiet';
             break;
         case OutputInterface::VERBOSITY_NORMAL:
             break;
     }
     ++$properties['wisembly_attempts'];
     $this->logger->info('Dispatching command', $body);
     // if no proper command given, log it and nack
     if (!isset($body['command'])) {
         $this->logger->critical('No proper command found in message', ['body' => $body]);
         $this->provider->nack($message, false);
         return;
     }
     $this->builder->setPrefix([PHP_BINARY, $this->commandPath, $body['command']]);
     $this->builder->setArguments($body['arguments']);
     $process = $this->builder->getProcess();
     $process->run(function ($type, $data) {
         switch ($type) {
             case Process::OUT:
                 $this->logger->info($data);
                 break;
             case Process::ERR:
                 $this->logger->error($data);
                 break;
         }
     });
     // reset the builder
     $this->builder->setArguments([]);
     $this->builder->setPrefix([]);
     if ($process->isSuccessful()) {
         $this->logger->info('The process was successful', $body);
         $this->provider->ack($message);
         return;
     }
     $code = $process->getExitCode();
     $this->logger->error('The command failed ; aborting', ['body' => $body, 'code' => $code]);
     $this->provider->nack($message, false);
     // should we requeue it ?
     if (static::REQUEUE === $code && $properties['wisembly_attempts'] < static::MAX_ATTEMPTS) {
         $this->logger->notice('Retrying...', $body);
         $message = new Message($message->getBody(), $properties, $message->getId());
         $this->publisher->publish($message);
     }
 }
Exemplo n.º 15
0
 /**
  * {@inheritDoc}
  */
 public function publish(Message $message, $key = null)
 {
     $this->exchange->publish($message->getBody(), $key, $this->flags, $message->getProperties());
 }
Exemplo n.º 16
0
 /**
  * {@inheritdoc}
  */
 public function ack(Message $message)
 {
     $this->channel->deleteMessage(['QueueUrl' => $this->getQueueName(), 'ReceiptHandle' => $message->getId()]);
 }
 /**
  * {@inheritDoc}
  */
 public function process(Message $message, array $options)
 {
     $properties = $message->getProperties();
     $exchange = $this->exchange ?: $properties['exchange_name'];
     $routingKey = $this->routingKey ?: $properties['routing_key'];
     $this->getMessagePublisher($exchange)->publish(new Message($message->getBody()), $routingKey);
 }