/** * @inheritdoc */ public function process(EnvelopeInterface $envelope) { try { $result = $this->processor->process($envelope); } catch (\Exception $exception) { $this->logger->error($exception->getMessage(), ['message' => $envelope->getDeliveryTag()]); $this->logger->debug($exception->getTraceAsString()); $result = $this->retryMessage($envelope, $exception); } return $result; }
/** * @param QueueInterface $queue * @param ProcessorInterface $processor * @param EventDispatcherInterface $dispatcher */ public function __construct(QueueInterface $queue, ProcessorInterface $processor, EventDispatcherInterface $dispatcher = null) { $this->queue = $queue; $this->processor = $processor; $this->dispatcher = $dispatcher ?: new EventDispatcher(); $this->setCallback(function (EnvelopeInterface $envelope) { try { $event = new ConsumeEvent($envelope); $this->dispatcher->dispatch(QueueEvents::CONSUME_MESSAGE, $event); $result = $this->processor->process($envelope); $event->setResult($result); $this->dispatcher->dispatch(QueueEvents::CONSUMED_MESSAGE, $event); $this->ack($envelope); return $event->shouldContinueConsuming(); } catch (\Exception $exception) { $this->dispatcher->dispatch(QueueEvents::CONSUME_EXCEPTION, new ConsumeExceptionEvent($envelope, $exception)); $this->nack($envelope, $this->nackRequeue); throw $exception; } }); }
/** * @test * @expectedException \RuntimeException */ public function it_can_nack_and_requeue_failed_message() { $tag = 'abc123'; /** @var MockInterface|EnvelopeInterface $envelope */ $envelope = Mock::mock(EnvelopeInterface::class); $envelope->shouldReceive('getDeliveryTag')->andReturn($tag); // assert that the queue is consumed and the callback is called $this->queue->shouldReceive('consume')->once()->andReturnUsing(function (callable $callback) use($envelope) { return $callback($envelope); }); // message should be ack-ed after processing $this->queue->shouldReceive('nack')->once()->with($tag, true); $this->processor->shouldReceive('process')->once()->with($envelope)->andThrow(new \RuntimeException()); $consumer = new Consumer($this->queue, $this->processor); $consumer->setNackRequeue(true); $consumer->consume(); }