/** * {@inheritDoc} */ public function __invoke(\AMQPEnvelope $envelope, \AMQPQueue $queue, Context $context = null) { $currentStartTime = microtime(true); if ($context->getUseSigHandler()) { SignalHandler::start(); } try { $consumer = $this->consumer; $consumer($envelope, $queue, $context); $queue->ack($envelope->getDeliveryTag()); $context->output(sprintf('<comment>ACK [%s]. Duration <info>%.2fs</info>. Memory usage: <info>%.2f Mo</info></comment>', $envelope->getDeliveryTag(), microtime(true) - $currentStartTime, round(memory_get_usage() / 1024 / 1024, 2))); } catch (\Exception $e) { $queue->nack($envelope->getDeliveryTag(), $context->getRequeueOnError() ? AMQP_REQUEUE : null); $context->output(sprintf('<error>NACK [%s].</error>', $envelope->getDeliveryTag())); if (null !== $this->logger) { $this->logger->error(sprintf('Error occured with queue "%s". Message: %s. Exception: %s', $queue->getName(), $e->getMessage(), $e->getTraceAsString())); } if (null !== $context->getOutput() && $context->getOutput()->getVerbosity() >= 2) { throw $e; } } if (++$this->nbMessagesProcessed >= $context->getMaxMessages()) { $context->output(sprintf('<info>Max messages reached. Exiting after processing <comment>%d messages</comment>.</info>', $this->nbMessagesProcessed)); return false; } if ($context->getUseSigHandler() && SignalHandler::haveToStop()) { $context->output(sprintf('<info>Signal received. Exiting after processing <comment>%d messages</comment>.</info>', $this->nbMessagesProcessed)); return false; } return true; }
/** * getContext * * @param InputInterface $input * @param OutputInterface $output * * @return Context */ protected function getContext(InputInterface $input, OutputInterface $output) { $context = new Context($input->getOption('timeout'), $input->getOption('max-messages'), !$input->getOption('no-sighandler'), $input->getOption('requeue-on-error'), $input->getOption('poll-interval')); $context->setOutput($output); return $context; }