|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Mongodb; |
| 4 | + |
| 5 | +use Interop\Queue\InvalidMessageException; |
| 6 | +use Interop\Queue\PsrConsumer; |
| 7 | +use Interop\Queue\PsrMessage; |
| 8 | + |
| 9 | +class MongodbConsumer implements PsrConsumer |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var MongodbContext |
| 13 | + */ |
| 14 | + private $context; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var MongodbDestination |
| 18 | + */ |
| 19 | + private $queue; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var int microseconds |
| 23 | + */ |
| 24 | + private $pollingInterval = 1000000; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param MongodbContext $context |
| 28 | + * @param MongodbDestination $queue |
| 29 | + */ |
| 30 | + public function __construct(MongodbContext $context, MongodbDestination $queue) |
| 31 | + { |
| 32 | + $this->context = $context; |
| 33 | + $this->queue = $queue; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Set polling interval in milliseconds. |
| 38 | + * |
| 39 | + * @param int $msec |
| 40 | + */ |
| 41 | + public function setPollingInterval($msec) |
| 42 | + { |
| 43 | + $this->pollingInterval = $msec * 1000; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Get polling interval in milliseconds. |
| 48 | + * |
| 49 | + * @return int |
| 50 | + */ |
| 51 | + public function getPollingInterval() |
| 52 | + { |
| 53 | + return (int) $this->pollingInterval / 1000; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritdoc} |
| 58 | + * |
| 59 | + * @return MongodbDestination |
| 60 | + */ |
| 61 | + public function getQueue() |
| 62 | + { |
| 63 | + return $this->queue; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@inheritdoc} |
| 68 | + * |
| 69 | + * @return MongodbMessage|null |
| 70 | + */ |
| 71 | + public function receive($timeout = 0) |
| 72 | + { |
| 73 | + $timeout /= 1000; |
| 74 | + $startAt = microtime(true); |
| 75 | + |
| 76 | + while (true) { |
| 77 | + $message = $this->receiveMessage(); |
| 78 | + |
| 79 | + if ($message) { |
| 80 | + return $message; |
| 81 | + } |
| 82 | + |
| 83 | + if ($timeout && (microtime(true) - $startAt) >= $timeout) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + usleep($this->pollingInterval); |
| 88 | + |
| 89 | + if ($timeout && (microtime(true) - $startAt) >= $timeout) { |
| 90 | + return; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritdoc} |
| 97 | + * |
| 98 | + * @return MongodbMessage|null |
| 99 | + */ |
| 100 | + public function receiveNoWait() |
| 101 | + { |
| 102 | + return $this->receiveMessage(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * {@inheritdoc} |
| 107 | + * |
| 108 | + * @param MongodbMessage $message |
| 109 | + */ |
| 110 | + public function acknowledge(PsrMessage $message) |
| 111 | + { |
| 112 | + // does nothing |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * {@inheritdoc} |
| 117 | + * |
| 118 | + * @param MongodbMessage $message |
| 119 | + */ |
| 120 | + public function reject(PsrMessage $message, $requeue = false) |
| 121 | + { |
| 122 | + InvalidMessageException::assertMessageInstanceOf($message, MongodbMessage::class); |
| 123 | + |
| 124 | + if ($requeue) { |
| 125 | + $this->context->createProducer()->send($this->queue, $message); |
| 126 | + |
| 127 | + return; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @return MongodbMessage|null |
| 133 | + */ |
| 134 | + protected function receiveMessage() |
| 135 | + { |
| 136 | + $now = time(); |
| 137 | + $collection = $this->context->getCollection(); |
| 138 | + $message = $collection->findOneAndDelete( |
| 139 | + [ |
| 140 | + '$or' => [ |
| 141 | + ['delayed_until' => ['$exists' => false]], |
| 142 | + ['delayed_until' => ['$lte' => $now]], |
| 143 | + ], |
| 144 | + ], |
| 145 | + [ |
| 146 | + 'sort' => ['priority' => -1, 'published_at' => 1], |
| 147 | + 'typeMap' => ['root' => 'array', 'document' => 'array'], |
| 148 | + ] |
| 149 | + ); |
| 150 | + |
| 151 | + if (!$message) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + if (empty($message['time_to_live']) || $message['time_to_live'] > time()) { |
| 155 | + return $this->convertMessage($message); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @param array $dbalMessage |
| 161 | + * |
| 162 | + * @return MongodbMessage |
| 163 | + */ |
| 164 | + protected function convertMessage(array $mongodbMessage) |
| 165 | + { |
| 166 | + $message = $this->context->createMessage($mongodbMessage['body'], $mongodbMessage['properties'], $mongodbMessage['headers']); |
| 167 | + $message->setId((string) $mongodbMessage['_id']); |
| 168 | + $message->setPriority((int) $mongodbMessage['priority']); |
| 169 | + $message->setRedelivered((bool) $mongodbMessage['redelivered']); |
| 170 | + $message->setPublishedAt((int) $mongodbMessage['published_at']); |
| 171 | + |
| 172 | + return $message; |
| 173 | + } |
| 174 | +} |
0 commit comments