|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Enqueue\Mongodb; |
| 6 | + |
| 7 | +use Interop\Queue\Consumer; |
| 8 | +use Interop\Queue\SubscriptionConsumer; |
| 9 | + |
| 10 | +class MongodbSubscriptionConsumer implements SubscriptionConsumer |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var MongodbContext |
| 14 | + */ |
| 15 | + private $context; |
| 16 | + |
| 17 | + /** |
| 18 | + * an item contains an array: [MongodbConsumer $consumer, callable $callback];. |
| 19 | + * |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + private $subscribers; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param MongodbContext $context |
| 26 | + */ |
| 27 | + public function __construct(MongodbContext $context) |
| 28 | + { |
| 29 | + $this->context = $context; |
| 30 | + $this->subscribers = []; |
| 31 | + } |
| 32 | + |
| 33 | + public function consume(int $timeout = 0): void |
| 34 | + { |
| 35 | + if (empty($this->subscribers)) { |
| 36 | + throw new \LogicException('No subscribers'); |
| 37 | + } |
| 38 | + |
| 39 | + $timeout = (int) ceil($timeout / 1000); |
| 40 | + $endAt = time() + $timeout; |
| 41 | + |
| 42 | + $queueNames = []; |
| 43 | + foreach (array_keys($this->subscribers) as $queueName) { |
| 44 | + $queueNames[$queueName] = $queueName; |
| 45 | + } |
| 46 | + |
| 47 | + $currentQueueNames = []; |
| 48 | + while (true) { |
| 49 | + if (empty($currentQueueNames)) { |
| 50 | + $currentQueueNames = $queueNames; |
| 51 | + } |
| 52 | + |
| 53 | + $result = $this->context->getCollection()->findOneAndDelete( |
| 54 | + [ |
| 55 | + 'queue' => ['$in' => array_keys($currentQueueNames)], |
| 56 | + '$or' => [ |
| 57 | + ['delayed_until' => ['$exists' => false]], |
| 58 | + ['delayed_until' => ['$lte' => time()]], |
| 59 | + ], |
| 60 | + ], |
| 61 | + [ |
| 62 | + 'sort' => ['priority' => -1, 'published_at' => 1], |
| 63 | + 'typeMap' => ['root' => 'array', 'document' => 'array'], |
| 64 | + ] |
| 65 | + ); |
| 66 | + |
| 67 | + if ($result) { |
| 68 | + list($consumer, $callback) = $this->subscribers[$result['queue']]; |
| 69 | + |
| 70 | + $message = $this->context->convertMessage($result); |
| 71 | + |
| 72 | + if (false === call_user_func($callback, $message, $consumer)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + unset($currentQueueNames[$result['queue']]); |
| 77 | + } else { |
| 78 | + $currentQueueNames = []; |
| 79 | + |
| 80 | + usleep(200000); // 200ms |
| 81 | + } |
| 82 | + |
| 83 | + if ($timeout && microtime(true) >= $endAt) { |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param MongodbConsumer $consumer |
| 91 | + */ |
| 92 | + public function subscribe(Consumer $consumer, callable $callback): void |
| 93 | + { |
| 94 | + if (false == $consumer instanceof MongodbConsumer) { |
| 95 | + throw new \InvalidArgumentException(sprintf('The consumer must be instance of "%s" got "%s"', MongodbConsumer::class, get_class($consumer))); |
| 96 | + } |
| 97 | + |
| 98 | + $queueName = $consumer->getQueue()->getQueueName(); |
| 99 | + if (array_key_exists($queueName, $this->subscribers)) { |
| 100 | + if ($this->subscribers[$queueName][0] === $consumer && $this->subscribers[$queueName][1] === $callback) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + throw new \InvalidArgumentException(sprintf('There is a consumer subscribed to queue: "%s"', $queueName)); |
| 105 | + } |
| 106 | + |
| 107 | + $this->subscribers[$queueName] = [$consumer, $callback]; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param MongodbConsumer $consumer |
| 112 | + */ |
| 113 | + public function unsubscribe(Consumer $consumer): void |
| 114 | + { |
| 115 | + if (false == $consumer instanceof MongodbConsumer) { |
| 116 | + throw new \InvalidArgumentException(sprintf('The consumer must be instance of "%s" got "%s"', MongodbConsumer::class, get_class($consumer))); |
| 117 | + } |
| 118 | + |
| 119 | + $queueName = $consumer->getQueue()->getQueueName(); |
| 120 | + |
| 121 | + if (false == array_key_exists($queueName, $this->subscribers)) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if ($this->subscribers[$queueName][0] !== $consumer) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + unset($this->subscribers[$queueName]); |
| 130 | + } |
| 131 | + |
| 132 | + public function unsubscribeAll(): void |
| 133 | + { |
| 134 | + $this->subscribers = []; |
| 135 | + } |
| 136 | +} |
0 commit comments