示例#1
0
文件: Consumer.php 项目: kyar/swarrot
 /**
  * consume.
  *
  * @param array $options Parameters sent to the processor
  */
 public function consume(array $options = array())
 {
     if (null !== $this->logger) {
         $this->logger->debug(sprintf('Start consuming queue %s.', $this->messageProvider->getQueueName()));
     }
     $this->optionsResolver->setDefaults(array('poll_interval' => 50000));
     if ($this->processor instanceof ConfigurableInterface) {
         $this->processor->setDefaultOptions($this->optionsResolver);
     }
     $options = $this->optionsResolver->resolve($options);
     if ($this->processor instanceof InitializableInterface) {
         $this->processor->initialize($options);
     }
     while (true) {
         while (null !== ($message = $this->messageProvider->get())) {
             if (false === $this->processor->process($message, $options)) {
                 break 2;
             }
         }
         if ($this->processor instanceof SleepyInterface) {
             if (false === $this->processor->sleep($options)) {
                 break;
             }
         }
         usleep($options['poll_interval']);
     }
     if ($this->processor instanceof TerminableInterface) {
         $this->processor->terminate($options);
     }
 }
示例#2
0
 /**
  * @param \Exception|\Throwable $exception
  * @param Message               $message
  * @param array                 $options
  */
 private function handleException($exception, Message $message, array $options)
 {
     $requeue = isset($options['requeue_on_error']) ? (bool) $options['requeue_on_error'] : false;
     $this->messageProvider->nack($message, $requeue);
     $this->logger and $this->logger->error(sprintf('[Ack] An exception occurred. Message #%d has been %s.', $message->getId(), $requeue ? 'requeued' : 'nack\'ed'), ['swarrot_processor' => 'ack', 'exception' => $exception]);
     throw $exception;
 }
示例#3
0
 /**
  * {@inheritDoc}
  */
 public function process(Message $message, array $options)
 {
     try {
         $return = $this->processor->process($message, $options);
         $this->messageProvider->ack($message);
         $this->logger and $this->logger->info('[Ack] Message #' . $message->getId() . ' have been correctly ack\'ed', array('swarrot_processor' => 'ack'));
         return $return;
     } catch (\Exception $e) {
         $requeue = isset($options['requeue_on_error']) ? (bool) $options['requeue_on_error'] : false;
         $this->messageProvider->nack($message, $requeue);
         $this->logger and $this->logger->warning(sprintf('[Ack] An exception occurred. Message #%d have been %s.', $message->getId(), $requeue ? 'requeued' : 'nack\'ed'), array('swarrot_processor' => 'ack', 'exception' => $e));
         throw $e;
     }
 }
示例#4
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);
     }
 }