/RabbitMq/Consumer.php

https://github.com/belaka/RabbitMqBundle · PHP · 104 lines · 59 code · 15 blank · 30 comment · 12 complexity · 348f448ddebbd4bf28c78c75934ca81d MD5 · raw file

  1. <?php
  2. namespace OldSound\RabbitMqBundle\RabbitMq;
  3. use OldSound\RabbitMqBundle\RabbitMq\BaseConsumer;
  4. use PhpAmqpLib\Message\AMQPMessage;
  5. class Consumer extends BaseConsumer
  6. {
  7. /**
  8. * @var int $memoryLimit
  9. */
  10. protected $memoryLimit = null;
  11. /**
  12. * Set the memory limit
  13. *
  14. * @param int $memoryLimit
  15. */
  16. public function setMemoryLimit($memoryLimit)
  17. {
  18. $this->memoryLimit = $memoryLimit;
  19. }
  20. /**
  21. * Get the memory limit
  22. *
  23. * @return int
  24. */
  25. public function getMemoryLimit()
  26. {
  27. return $this->memoryLimit;
  28. }
  29. /**
  30. * Consume the message
  31. *
  32. * @param int $msgAmount
  33. */
  34. public function consume($msgAmount)
  35. {
  36. $this->target = $msgAmount;
  37. $this->setupConsumer();
  38. while (count($this->getChannel()->callbacks)) {
  39. $this->maybeStopConsumer();
  40. $this->getChannel()->wait(null, false, $this->getIdleTimeout());
  41. }
  42. }
  43. /**
  44. * Purge the queue
  45. */
  46. public function purge()
  47. {
  48. $this->getChannel()->queue_purge($this->queueOptions['name'], true);
  49. }
  50. public function processMessage(AMQPMessage $msg)
  51. {
  52. $processFlag = call_user_func($this->callback, $msg);
  53. $this->handleProcessMessage($msg, $processFlag);
  54. }
  55. protected function handleProcessMessage(AMQPMessage $msg, $processFlag)
  56. {
  57. if ($processFlag === ConsumerInterface::MSG_REJECT_REQUEUE || false === $processFlag) {
  58. // Reject and requeue message to RabbitMQ
  59. $msg->delivery_info['channel']->basic_reject($msg->delivery_info['delivery_tag'], true);
  60. } else if ($processFlag === ConsumerInterface::MSG_SINGLE_NACK_REQUEUE) {
  61. // NACK and requeue message to RabbitMQ
  62. $msg->delivery_info['channel']->basic_nack($msg->delivery_info['delivery_tag'], false, true);
  63. } else if ($processFlag === ConsumerInterface::MSG_REJECT) {
  64. // Reject and drop
  65. $msg->delivery_info['channel']->basic_reject($msg->delivery_info['delivery_tag'], false);
  66. } else {
  67. // Remove message from queue only if callback return not false
  68. $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
  69. }
  70. $this->consumed++;
  71. $this->maybeStopConsumer();
  72. if (!is_null($this->getMemoryLimit()) && $this->isRamAlmostOverloaded()) {
  73. $this->stopConsuming();
  74. }
  75. }
  76. /**
  77. * Checks if memory in use is greater or equal than memory allowed for this process
  78. *
  79. * @return boolean
  80. */
  81. protected function isRamAlmostOverloaded()
  82. {
  83. if (memory_get_usage(true) >= ($this->getMemoryLimit() * 1024 * 1024)) {
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. }
  89. }